Skip to content

Commit

Permalink
Fixed input command args parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DonCallisto committed Oct 2, 2020
1 parent 95ffb81 commit 7c052de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
11 changes: 7 additions & 4 deletions src/Process/ProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ private function replaceParameters($cmd, $suite, $processNumber, $currentProcess
$commandToExecute = str_replace('{p}', $processNumber, $commandToExecute);
$commandToExecute = str_replace('{n}', $currentProcessCounter, $commandToExecute);

preg_match_all('#(?<!\\\\)("|\')(?:[^\\\\]|\\\\.)*?\1|\S+#s', $commandToExecute, $parsedCommand);

return $parsedCommand[0];
return $commandToExecute;
}

private function createProcess($executeCommand, $arrayEnv)
{
$process = new Process($executeCommand, null, $arrayEnv);
if (method_exists(Process::class, 'fromShellCommandline')) {
$process = Process::fromShellCommandline($executeCommand, null, $arrayEnv);
} else {
// Drop when sf 3.4 supports ends
$process = new Process($executeCommand, null, $arrayEnv);
}

$process->setTimeout(null);
$process->setIdleTimeout(null);
Expand Down
19 changes: 7 additions & 12 deletions src/Process/ProcessorCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,13 @@ private function readFromProcCPUInfo()
}
}
} elseif ('\\' === DIRECTORY_SEPARATOR) {
$command = [
'for',
'/F',
'"tokens=2 delims=="',
'%C',
'in',
"('wmic cpu get NumberOfLogicalProcessors /value ^| findstr NumberOfLogicalProcessors')",
'do',
'@echo',
'%C'
];
$process = new Process($command);
$executeCommand = 'for /F "tokens=2 delims==" %C in (\'wmic cpu get NumberOfLogicalProcessors /value ^| findstr NumberOfLogicalProcessors\') do @echo %C';
if (method_exists(Process::class, 'fromShellCommandline')) {
$process = Process::fromShellCommandline($executeCommand);
} else {
// Drop when sf 3.4 supports ends
$process = new Process($executeCommand);
}
$process->run();

if ($process->isSuccessful() && ($numProc = (int) ($process->getOutput())) > 0) {
Expand Down
50 changes: 25 additions & 25 deletions tests/Process/ProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public function shouldCreateACommandUsingParallelTests()
$serverEnvs = EnvCommandCreator::cleanEnvVariables($_SERVER);
unset($serverEnvs['argv']);

$this->assertEquals("'bin".DIRECTORY_SEPARATOR."phpunit' 'fileA'", $process->getCommandLine());
$this->assertEquals('bin'.DIRECTORY_SEPARATOR.'phpunit fileA', $process->getCommandLine());
$this->assertEquals(
$this->castValues(array_change_key_case($serverEnvs + $_ENV + [
'ENV_TEST_CHANNEL' => 2,
'ENV_TEST_CHANNEL_READABLE' => 'test_2',
'ENV_TEST_CHANNELS_NUMBER' => 10,
'ENV_TEST_ARGUMENT' => 'fileA',
'ENV_TEST_INC_NUMBER' => 10,
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1,
], CASE_UPPER)),
'ENV_TEST_CHANNEL' => 2,
'ENV_TEST_CHANNEL_READABLE' => 'test_2',
'ENV_TEST_CHANNELS_NUMBER' => 10,
'ENV_TEST_ARGUMENT' => 'fileA',
'ENV_TEST_INC_NUMBER' => 10,
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1,
], CASE_UPPER)),
$this->castValues($process->getenv())
);
}
Expand All @@ -38,7 +38,7 @@ public function shouldCreateACommandUsingParallelTestsWithFilteredVariables()
$factory = new ProcessFactory(10);
$process = $factory->createAProcess('fileA', 2, 10, true);

$this->assertEquals("'bin".DIRECTORY_SEPARATOR."phpunit' 'fileA'", $process->getCommandLine());
$this->assertEquals('bin'.DIRECTORY_SEPARATOR.'phpunit fileA', $process->getCommandLine());

$processEnv = $process->getEnv();
$envTestVars = $this->filterEnvTestVariables($processEnv);
Expand All @@ -63,16 +63,16 @@ public function shouldCreateACommandUsingParallelTestsWithOptions()
$serverEnvs = EnvCommandCreator::cleanEnvVariables($_SERVER);
unset($serverEnvs['argv']);

$this->assertEquals("'execute'", $process->getCommandLine());
$this->assertEquals('execute', $process->getCommandLine());
$this->assertEquals(
$this->castValues(array_change_key_case($serverEnvs + $_ENV + [
'ENV_TEST_CHANNEL' => 2,
'ENV_TEST_CHANNEL_READABLE' => 'test_2',
'ENV_TEST_CHANNELS_NUMBER' => 11,
'ENV_TEST_ARGUMENT' => 'fileA',
'ENV_TEST_INC_NUMBER' => 12,
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 0,
], CASE_UPPER)),
'ENV_TEST_CHANNEL' => 2,
'ENV_TEST_CHANNEL_READABLE' => 'test_2',
'ENV_TEST_CHANNELS_NUMBER' => 11,
'ENV_TEST_ARGUMENT' => 'fileA',
'ENV_TEST_INC_NUMBER' => 12,
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 0,
], CASE_UPPER)),
$this->castValues($process->getenv())
);
}
Expand All @@ -87,16 +87,16 @@ public function shouldReplaceThePlaceholder()
$serverEnvs = EnvCommandCreator::cleanEnvVariables($_SERVER);
unset($serverEnvs['argv']);

$this->assertEquals("'execute' '1' 'fileA' '13'", $process->getCommandLine());
$this->assertEquals('execute 1 fileA 13', $process->getCommandLine());
$this->assertEquals(
$this->castValues(array_change_key_case($serverEnvs + $_ENV + [
'ENV_TEST_CHANNEL' => 1,
'ENV_TEST_CHANNEL_READABLE' => 'test_1',
'ENV_TEST_CHANNELS_NUMBER' => 12,
'ENV_TEST_ARGUMENT' => 'fileA',
'ENV_TEST_INC_NUMBER' => 13,
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1,
], CASE_UPPER)),
'ENV_TEST_CHANNEL' => 1,
'ENV_TEST_CHANNEL_READABLE' => 'test_1',
'ENV_TEST_CHANNELS_NUMBER' => 12,
'ENV_TEST_ARGUMENT' => 'fileA',
'ENV_TEST_INC_NUMBER' => 13,
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1,
], CASE_UPPER)),
$this->castValues($process->getenv())
);
}
Expand Down

0 comments on commit 7c052de

Please sign in to comment.