From ad6704736e2022335648c05133d20ca25502155b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 18 Aug 2023 22:22:07 +0700 Subject: [PATCH 1/5] [Parallel] Handle Spaced root project main script on parallel process --- .../Command/WorkerCommandLineFactoryTest.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php index c564cd28497..a8a987c28c8 100644 --- a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php +++ b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php @@ -27,6 +27,11 @@ final class WorkerCommandLineFactoryTest extends AbstractLazyTestCase */ private const DUMMY_MAIN_SCRIPT = 'main_script'; + /** + * @var string + */ + private const SPACED_DUMMY_MAIN_SCRIPT = 'C:\Users\P\Desktop\Web Dev\CodeIgniter4\rector.php'; + private WorkerCommandLineFactory $workerCommandLineFactory; private ProcessCommand $processCommand; @@ -37,6 +42,65 @@ protected function setUp(): void $this->processCommand = $this->make(ProcessCommand::class); } + /** + * @param array $inputParameters + */ + #[DataProvider('provideDataSpacedMainScript')] + public function testSpacedMainScript(array $inputParameters, string $expectedCommand): void + { + $inputDefinition = $this->prepareProcessCommandDefinition(); + $arrayInput = new ArrayInput($inputParameters, $inputDefinition); + + $workerCommandLine = $this->workerCommandLineFactory->create( + self::SPACED_DUMMY_MAIN_SCRIPT, + ProcessCommand::class, + 'worker', + $arrayInput, + 'identifier', + 2000 + ); + + echo $workerCommandLine . PHP_EOL; + + $this->assertSame($expectedCommand, $workerCommandLine); + } + + /** + * @return Iterator>|string[]> + */ + public static function provideDataSpacedMainScript(): Iterator + { + $cliInputOptions = array_slice($_SERVER['argv'], 1); + $cliInputOptionsAsString = implode("' '", $cliInputOptions); + + yield [ + [ + self::COMMAND => 'process', + Option::SOURCE => ['src'], + ], + "'" . PHP_BINARY . "' '" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + ]; + + yield [ + [ + self::COMMAND => 'process', + Option::SOURCE => ['src'], + '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, + ], + "'" . PHP_BINARY . "' '" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + ]; + + yield [ + [ + self::COMMAND => 'process', + Option::SOURCE => ['src'], + '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, + '--' . Option::DEBUG => true, + ], + "'" . PHP_BINARY . "' '" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + ]; + } + /** * @param array $inputParameters */ From ae24d731eb8d3352a15af40a9e095dc4d2c4f919 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 18 Aug 2023 22:34:14 +0700 Subject: [PATCH 2/5] update main script example --- .../Parallel/Command/WorkerCommandLineFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php index a8a987c28c8..d780a0f6858 100644 --- a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php +++ b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php @@ -30,7 +30,7 @@ final class WorkerCommandLineFactoryTest extends AbstractLazyTestCase /** * @var string */ - private const SPACED_DUMMY_MAIN_SCRIPT = 'C:\Users\P\Desktop\Web Dev\CodeIgniter4\rector.php'; + private const SPACED_DUMMY_MAIN_SCRIPT = 'C:\Users\P\Desktop\Web Dev\vendor\bin\rector'; private WorkerCommandLineFactory $workerCommandLineFactory; From db366c1f0c249843e582afae08dde2ec1253e3cf Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 18 Aug 2023 22:46:23 +0700 Subject: [PATCH 3/5] try using double quoted for main script --- .../Parallel/Command/WorkerCommandLineFactoryTest.php | 2 -- packages/Parallel/Command/WorkerCommandLineFactory.php | 5 +++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php index d780a0f6858..53e929d098e 100644 --- a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php +++ b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php @@ -60,8 +60,6 @@ public function testSpacedMainScript(array $inputParameters, string $expectedCom 2000 ); - echo $workerCommandLine . PHP_EOL; - $this->assertSame($expectedCommand, $workerCommandLine); } diff --git a/packages/Parallel/Command/WorkerCommandLineFactory.php b/packages/Parallel/Command/WorkerCommandLineFactory.php index 2278fedd335..e390d0c15e4 100644 --- a/packages/Parallel/Command/WorkerCommandLineFactory.php +++ b/packages/Parallel/Command/WorkerCommandLineFactory.php @@ -61,6 +61,11 @@ public function create( break; } + if ($arg === $mainScript) { + $workerCommandArray[] = '"' . $arg . '"'; + continue; + } + $workerCommandArray[] = escapeshellarg((string) $arg); } From 41fd69e3eb6b311fd09e7da3b8cdc8e1c5838ba5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 14 Sep 2023 15:11:50 +0700 Subject: [PATCH 4/5] use relative path for --config --- .../Parallel/Command/WorkerCommandLineFactory.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/Parallel/Command/WorkerCommandLineFactory.php b/packages/Parallel/Command/WorkerCommandLineFactory.php index e390d0c15e4..d0deca81ef1 100644 --- a/packages/Parallel/Command/WorkerCommandLineFactory.php +++ b/packages/Parallel/Command/WorkerCommandLineFactory.php @@ -6,6 +6,7 @@ use Rector\ChangesReporting\Output\JsonOutputFormatter; use Rector\Core\Configuration\Option; +use Rector\Core\FileSystem\FilePathHelper; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symplify\EasyParallel\Exception\ParallelShouldNotHappenException; @@ -22,11 +23,11 @@ final class WorkerCommandLineFactory */ private const OPTION_DASHES = '--'; - private readonly CommandFromReflectionFactory $commandFromReflectionFactory; - - public function __construct() + public function __construct( + private readonly CommandFromReflectionFactory $commandFromReflectionFactory, + private readonly FilePathHelper $filePathHelper + ) { - $this->commandFromReflectionFactory = new CommandFromReflectionFactory(); } /** @@ -115,7 +116,8 @@ public function create( * * tested in macOS and Ubuntu (github action) */ - $workerCommandArray[] = escapeshellarg((string) $input->getOption(Option::CONFIG)); + $config = (string) $input->getOption(Option::CONFIG); + $workerCommandArray[] = escapeshellarg($this->filePathHelper->relativePath($config)); } return implode(' ', $workerCommandArray); From 95e0765a3d3bb24d53c3bb04b342468cdb67e0d6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 14 Sep 2023 15:12:31 +0700 Subject: [PATCH 5/5] use relative path for --config --- packages/Parallel/Command/WorkerCommandLineFactory.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/Parallel/Command/WorkerCommandLineFactory.php b/packages/Parallel/Command/WorkerCommandLineFactory.php index d0deca81ef1..ee7644c1131 100644 --- a/packages/Parallel/Command/WorkerCommandLineFactory.php +++ b/packages/Parallel/Command/WorkerCommandLineFactory.php @@ -62,11 +62,6 @@ public function create( break; } - if ($arg === $mainScript) { - $workerCommandArray[] = '"' . $arg . '"'; - continue; - } - $workerCommandArray[] = escapeshellarg((string) $arg); }