Skip to content

Commit

Permalink
Temporarily revert "Use os detection as fallback (#23)" until regress…
Browse files Browse the repository at this point in the history
…ions can be fixed.

This reverts commit c57159e.
  • Loading branch information
greg-1-anderson committed Jan 31, 2019
1 parent a2649a0 commit 9ee4603
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 87 deletions.
4 changes: 0 additions & 4 deletions src/ProcessBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ public function getOutputAsJson()
}
$output = preg_replace('#^[^{]*#', '', $output);
$output = preg_replace('#[^}]*$#', '', $output);
// Doubled double quotes were converted to \\". Revert to double quote.
$output = str_replace('\\"', '"', $output);
// Revert of doubled backslashes.
$output = preg_replace('#\\\\{2}#', '\\', $output);
if (!$json = json_decode($output, true)) {
throw new \InvalidArgumentException('Unable to decode output into JSON.');
}
Expand Down
28 changes: 8 additions & 20 deletions src/Util/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,9 @@ public static function shellArg($arg, $os = null)

/**
* isWindows determines whether the provided OS is Windows.
*
* @param string|null $os The OS to escape for.
*
* @return boolean
*/
public static function isWindows($os = null)
public static function isWindows($os)
{
// In most cases, $os will be NULL and PHP_OS will be returned. However,
// if an OS is specified in $os, return that instead.
$os = $os ?: PHP_OS;
return strtoupper(substr($os, 0, 3)) === 'WIN';
}

Expand Down Expand Up @@ -120,23 +113,18 @@ public static function linuxArg($arg)
*/
public static function windowsArg($arg)
{
if ('' === $arg || null === $arg) {
return '""';
}
if (false !== strpos($arg, "\0")) {
$arg = str_replace("\0", '?', $arg);
}
if (!preg_match('/[\/()%!^"<>&|\s]/', $arg)) {
return $arg;
}
// Double up existing backslashes
$arg = preg_replace('/(\\\\+)$/', '$1$1', $arg);
$arg = preg_replace('/\\\/', '\\\\\\\\', $arg);

// Double up double quotes
$arg = preg_replace('/"/', '""', $arg);

// Double up percents.
// $arg = preg_replace('/%/', '%%', $arg);

// Replacing whitespace for good measure (see comment above).
$arg = str_replace(["\t", "\n", "\r", "\0", "\x0B"], ' ', $arg);

$arg = str_replace(['"', '^', '%', '!'], ['""', '"^^"', '"^%"', '"^!"'], $arg);

// Add surrounding quotes.
$arg = '"' . $arg . '"';

Expand Down
16 changes: 1 addition & 15 deletions tests/RealtimeOutputHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Consolidation\SiteProcess;

use Consolidation\SiteProcess\Util\Escape;
use PHPUnit\Framework\TestCase;
use Consolidation\SiteProcess\Util\ArgumentProcessor;
use Consolidation\SiteAlias\AliasRecord;
Expand All @@ -22,28 +21,18 @@ public function realtimeOutputHandlerTestValues()
'hello, world',
'',
['echo', 'hello, world'],
'LINUX',
],

[
'"hello, world"',
'',
['echo', 'hello, world'],
'WIN'
],

[
'README.md',
'',
['ls', 'README.md'],
'LINUX',
],

[
'',
'no/such/file: No such file or directory',
['ls', 'no/such/file'],
'LINUX',
],
];
}
Expand All @@ -53,11 +42,8 @@ public function realtimeOutputHandlerTestValues()
*
* @dataProvider realtimeOutputHandlerTestValues
*/
public function testRealtimeOutputHandler($expectedStdout, $expectedStderr, $args, $os)
public function testRealtimeOutputHandler($expectedStdout, $expectedStderr, $args)
{
if (Escape::isWindows() != Escape::isWindows($os)) {
$this->markTestSkipped("OS isn't supported");
}
$stdin = new ArrayInput([]);
$stdout = new BufferedOutput();
$stderr = new BufferedOutput();
Expand Down
50 changes: 2 additions & 48 deletions tests/SiteProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Consolidation\SiteProcess\Util\ArgumentProcessor;
use Consolidation\SiteProcess\Util\Escape;
use Consolidation\SiteAlias\AliasRecord;

class SiteProcessTest extends TestCase
Expand All @@ -23,7 +22,6 @@ public function siteProcessTestValues()
['ls', '-al'],
[],
[],
NULL,
],

[
Expand All @@ -34,7 +32,6 @@ public function siteProcessTestValues()
['ls', '-al'],
[],
[],
NULL,
],

[
Expand All @@ -45,7 +42,6 @@ public function siteProcessTestValues()
['ls', '-al', '/path1', '/path2'],
[],
[],
NULL,
],

[
Expand All @@ -56,7 +52,6 @@ public function siteProcessTestValues()
['ls', '-al'],
[],
[],
NULL,
],

[
Expand All @@ -67,7 +62,6 @@ public function siteProcessTestValues()
['ls', '-al'],
[],
[],
NULL,
],

[
Expand All @@ -78,7 +72,6 @@ public function siteProcessTestValues()
['ls', '-al'],
[],
[],
NULL,
],

[
Expand All @@ -89,7 +82,6 @@ public function siteProcessTestValues()
['ls', '-al'],
[],
[],
NULL,
],

[
Expand All @@ -100,7 +92,6 @@ public function siteProcessTestValues()
['ls', '-al', '/path1', '/path2'],
[],
[],
NULL,
],

[
Expand All @@ -111,7 +102,6 @@ public function siteProcessTestValues()
['ls', '-al', '/path1', '/path2'],
[],
[],
NULL,
],

[
Expand All @@ -122,7 +112,6 @@ public function siteProcessTestValues()
['ls', '-al', '/path1', '/path2'],
[],
[],
NULL,
],

[
Expand All @@ -133,18 +122,6 @@ public function siteProcessTestValues()
['drush', 'status'],
['fields' => 'root,uri'],
[],
'LINUX',
],

[
'drush status --fields=root,uri',
false,
false,
[],
['drush', 'status'],
['fields' => 'root,uri'],
[],
'WIN',
],

[
Expand All @@ -155,7 +132,6 @@ public function siteProcessTestValues()
['drush', 'rsync', 'a', 'b',],
[],
['exclude' => 'vendor'],
NULL,
],

[
Expand All @@ -166,7 +142,6 @@ public function siteProcessTestValues()
['drush', 'rsync', 'a', 'b', '--', '--include=vendor/autoload.php'],
[],
['exclude' => 'vendor'],
NULL,
],
];
}
Expand All @@ -183,15 +158,8 @@ public function testSiteProcess(
$siteAliasData,
$args,
$options,
$optionsPassedAsArgs,
$os)
$optionsPassedAsArgs)
{
if (Escape::isWindows() != Escape::isWindows($os)) {
$this->markTestSkipped("OS isn't supported");
}
if ($useTty && Escape::isWindows($os)) {
$this->markTestSkipped('Windows doesn\'t have /dev/tty support');
}
$processManager = ProcessManager::createDefault();
$siteAlias = new AliasRecord($siteAliasData, '@alias.dev');
$siteProcess = $processManager->siteProcess($siteAlias, $args, $options, $optionsPassedAsArgs);
Expand All @@ -216,27 +184,18 @@ public function siteProcessJsonTestValues()
[
'Output is empty.',
'',
'LINUX',
],
[
'Unable to decode output into JSON.',
'No json data here',
NULL,
],
[
'{"foo":"bar"}',
'{"foo":"bar"}',
NULL,
],
[
'{"foo":"b\'ar"}',
'{"foo":"b\'ar"}',
NULL,
],
[
'{"foo":"bar"}',
'Ignored leading data {"foo":"bar"} Ignored trailing data',
NULL,
],
];
}
Expand All @@ -248,16 +207,11 @@ public function siteProcessJsonTestValues()
*/
public function testSiteProcessJson(
$expected,
$data,
$os)
$data)
{
if (Escape::isWindows() != Escape::isWindows($os)) {
$this->markTestSkipped("OS isn't supported");
}
$args = ['echo', $data];
$processManager = ProcessManager::createDefault();
$siteAlias = new AliasRecord([], '@alias.dev');
$siteAlias->set('os', $os);
$siteProcess = $processManager->siteProcess($siteAlias, $args);
$siteProcess->mustRun();

Expand Down

0 comments on commit 9ee4603

Please sign in to comment.