From 12bddc01370a3e0126827b697369fdc9be431525 Mon Sep 17 00:00:00 2001 From: Jhon Farias Date: Thu, 3 Dec 2026 21:18:02 +0100 Subject: [PATCH] fix(parallel): support --include-path (#2075) * fix(parallel): support --include-path * fix(parallel): keep an include path with an equals sign in the parent process * fix(parallel): apply the include path before the bootstrap file * chore(rector): skip the parallel include path fixture * fix(parallel): apply the command line include path before the XML one in the bootstrap file Register `--include-path` on the ParaTest command, so ParaTest passes it to the PHPUnit configuration of the parent process. PHPUnit then puts the command line paths before the XML `` before it includes the `--bootstrap` file. Read the value and the arguments the option consumes through PHPUnit's `Builder`, and write the value back in a form that PHPUnit reads unchanged, for the parent process and for each worker. Run the include path handler first, so it reads only the arguments of the user. Co-Authored-By: Claude Opus 5.5 * test(parallel): declare strict types and refresh the success snapshot Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Punyapal Shah --- phpunit.xml | 1 + rector.php | 1 + src/Plugins/Parallel.php | 2 + src/Plugins/Parallel/Handlers/IncludePath.php | 142 ++++++++++++++++++ ...EnsureCommandLineIncludePathComesFirst.php | 43 ++++++ tests/.snapshots/success.txt | 30 +++- tests/Fixtures/ParallelIncludePath.xml | 6 + .../RequiresThroughIncludePathTest.php | 11 ++ .../ParallelIncludePath/bootstrap.inc | 7 + .../include=path/parallel-include-path.inc | 3 + .../lib/parallel-include-path.inc | 3 + .../xml-lib/parallel-include-path.inc | 3 + tests/Visual/ParallelIncludePath.php | 53 +++++++ .../ParallelIncludePathLeadingSpace.php | 27 ++++ ...allelIncludePathOptionWithLeadingSpace.php | 27 ++++ .../ParallelIncludePathTrailingSpace.php | 27 ++++ .../Visual/ParallelIncludePathWhitespace.php | 20 +++ .../ParallelIncludePathWithoutValue.php | 24 +++ 18 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 src/Plugins/Parallel/Handlers/IncludePath.php create mode 100644 src/Subscribers/EnsureCommandLineIncludePathComesFirst.php create mode 100644 tests/Fixtures/ParallelIncludePath.xml create mode 100644 tests/Fixtures/ParallelIncludePath/RequiresThroughIncludePathTest.php create mode 100644 tests/Fixtures/ParallelIncludePath/bootstrap.inc create mode 100644 tests/Fixtures/ParallelIncludePath/include=path/parallel-include-path.inc create mode 100644 tests/Fixtures/ParallelIncludePath/lib/parallel-include-path.inc create mode 100644 tests/Fixtures/ParallelIncludePath/xml-lib/parallel-include-path.inc create mode 100644 tests/Visual/ParallelIncludePath.php create mode 100644 tests/Visual/ParallelIncludePathLeadingSpace.php create mode 100644 tests/Visual/ParallelIncludePathOptionWithLeadingSpace.php create mode 100644 tests/Visual/ParallelIncludePathTrailingSpace.php create mode 100644 tests/Visual/ParallelIncludePathWhitespace.php create mode 100644 tests/Visual/ParallelIncludePathWithoutValue.php diff --git a/phpunit.xml b/phpunit.xml index 864fc579..d164c017 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,6 +19,7 @@ ./tests-external ./tests/.snapshots ./tests/Fixtures/Inheritance + ./tests/Fixtures/ParallelIncludePath ./tests/Fixtures/Suites ./tests/Fixtures/Tia diff --git a/rector.php b/rector.php index c4fe1fd0..e9aec2e7 100644 --- a/rector.php +++ b/rector.php @@ -28,6 +28,7 @@ return RectorConfig::configure() __DIR__.'/src/Plugins/Parallel/Paratest/WrapperRunner.php', __DIR__.'/tests/Fixtures/Arch', __DIR__.'/tests/Fixtures/Suites', + __DIR__.'/tests/Fixtures/ParallelIncludePath', ReturnNeverTypeRector::class, ArrowFunctionDelegatingCallToFirstClassCallableRector::class, NarrowObjectReturnTypeRector::class, diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index b2a5d1fb..106aec9c 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -26,6 +26,7 @@ final class Parallel implements HandlesArguments private const string GLOBAL_PREFIX = 'PEST_PARALLEL_GLOBAL_'; private const array HANDLERS = [ + Parallel\Handlers\IncludePath::class, Parallel\Handlers\Parallel::class, Parallel\Handlers\Pest::class, Parallel\Handlers\Laravel::class, @@ -150,6 +151,7 @@ final class Parallel implements HandlesArguments $command->setVersion(version()); Parallel\Handlers\StopOn::configureOptions($command->find('paratest')->getDefinition()); + Parallel\Handlers\IncludePath::configureOptions($command->find('paratest')->getDefinition()); return $command; } diff --git a/src/Plugins/Parallel/Handlers/IncludePath.php b/src/Plugins/Parallel/Handlers/IncludePath.php new file mode 100644 index 00000000..f6eaac58 --- /dev/null +++ b/src/Plugins/Parallel/Handlers/IncludePath.php @@ -0,0 +1,142 @@ +optionArgumentsPhpunitReads($arguments, $index); + $includePath = $this->includePathPhpunitReads($optionArguments); + $index += count($optionArguments) - 1; + + continue; + } + + $argumentsForParatest[] = $arguments[$index]; + } + + ParallelPlugin::setGlobal(self::GLOBAL_KEY, $includePath === null ? false : base64_encode($includePath)); + + if ($includePath !== null) { + $commandLineIncludePathComesFirst = new EnsureCommandLineIncludePathComesFirst($includePath); + $commandLineIncludePathComesFirst->apply(); + + EventFacade::instance()->registerSubscriber($commandLineIncludePathComesFirst); + } + + if ($includePath !== null && $this->survivesThePhpunitParser($includePath)) { + array_push($argumentsForParatest, ...$this->argumentsPhpunitReadsAs($includePath)); + } + + return [...$argumentsForParatest, ...$separatorAndArgumentsAfterIt]; + } + + public static function configureOptions(InputDefinition $definition): void + { + $definition->addOption(new InputOption(substr(self::OPTION, 2), null, InputOption::VALUE_REQUIRED)); + } + + public function handleWorkerArguments(array $arguments): array + { + $encodedIncludePath = ParallelPlugin::getGlobal(self::GLOBAL_KEY); + $includePath = is_string($encodedIncludePath) ? base64_decode($encodedIncludePath, true) : false; + + if (! is_string($includePath) || $includePath === '') { + return $arguments; + } + + return [...$arguments, ...$this->argumentsPhpunitReadsAs($includePath)]; + } + + /** + * @param list $arguments + * @return list + */ + private function optionArgumentsPhpunitReads(array $arguments, int $index): array + { + if ($this->phpunitConfiguration([$arguments[$index]]) instanceof Configuration) { + return [$arguments[$index]]; + } + + return array_slice($arguments, $index, 2); + } + + /** + * @param list $optionArguments + */ + private function includePathPhpunitReads(array $optionArguments): ?string + { + $configuration = $this->phpunitConfiguration($optionArguments); + + return $configuration instanceof Configuration && $configuration->hasIncludePath() ? $configuration->includePath() : null; + } + + /** + * @param list $parameters + */ + private function phpunitConfiguration(array $parameters): ?Configuration + { + try { + return new Builder(EventFacade::emitter())->fromParameters([self::PROGRAM, ...$parameters]); + } catch (Throwable) { + return null; + } + } + + /** + * @return list + */ + private function argumentsPhpunitReadsAs(string $includePath): array + { + if (! $this->survivesThePhpunitParser($includePath)) { + return [self::OPTION, $includePath]; + } + + return [self::OPTION.'='.$includePath.$this->closingEqualsSignThatStopsTheTrim($includePath)]; + } + + private function closingEqualsSignThatStopsTheTrim(string $includePath): string + { + return rtrim($includePath) === $includePath ? '' : '='; + } + + private function survivesThePhpunitParser(string $includePath): bool + { + return $includePath !== '' && ! str_contains($includePath, '='); + } +} diff --git a/src/Subscribers/EnsureCommandLineIncludePathComesFirst.php b/src/Subscribers/EnsureCommandLineIncludePathComesFirst.php new file mode 100644 index 00000000..92639ba6 --- /dev/null +++ b/src/Subscribers/EnsureCommandLineIncludePathComesFirst.php @@ -0,0 +1,43 @@ + + */ + private array $directories; + + public function __construct(string $includePath) + { + $this->directories = array_values(array_filter( + explode(PATH_SEPARATOR, $includePath), + fn (string $directory): bool => $directory !== '', + )); + } + + public function notify(EventFacadeSealed $event): void + { + $this->apply(); + } + + public function apply(): void + { + if ($this->directories === []) { + return; + } + + $otherDirectories = array_filter( + explode(PATH_SEPARATOR, (string) get_include_path()), + fn (string $directory): bool => ! in_array($directory, $this->directories, true), + ); + + ini_set('include_path', implode(PATH_SEPARATOR, [...$this->directories, ...$otherDirectories])); + } +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 7a1006c2..9ce98c93 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2736,6 +2736,34 @@ ✓ parallel group-filtered run is not considered empty when a worker ends on a fully filtered file ✓ parallel group-filtered run still fails when the selection matches no tests + PASS Tests\Visual\ParallelIncludePath + ✓ parallel resolves requires through the include path with dataset "value in the same argument" + ✓ parallel resolves requires through the include path with dataset "value as a separate argument" + ✓ parallel resolves requires through the include path with dataset "absolute path" + ✓ parallel resolves requires through the include path with dataset "several paths" + ✓ parallel resolves requires through the include path with dataset "directory name with an equals sign" + ✓ parallel resolves requires through the include path with dataset "absolute directory name with an equals sign" + ✓ parallel resolves requires through the include path with dataset "require in the bootstrap file" + ✓ parallel resolves requires through the include path with dataset "command line before the XML include path" + ✓ parallel resolves requires through the include path with dataset "command line before the XML include path in the bootstrap file" + ✓ parallel places the include path before the argument separator + ✓ parallel fails to resolve requires without the include path + + PASS Tests\Visual\ParallelIncludePathLeadingSpace + ✓ parallel keeps the leading space of an include path in the same argument as serial phpunit does + + PASS Tests\Visual\ParallelIncludePathOptionWithLeadingSpace + ✓ parallel reads an include path option with a leading space as serial phpunit does + + PASS Tests\Visual\ParallelIncludePathTrailingSpace + ✓ parallel workers keep the trailing space of an include path as serial phpunit does + + PASS Tests\Visual\ParallelIncludePathWhitespace + ✓ parallel accepts an include path of whitespace as serial phpunit does + + PASS Tests\Visual\ParallelIncludePathWithoutValue + ✓ parallel keeps the pest runner when the include path option has no value + PASS Tests\Visual\ParallelNestedDatasets ✓ parallel loads nested datasets from nested directories @@ -2854,4 +2882,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 44 skipped, 2120 passed (4671 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 44 skipped, 2136 passed (4703 assertions) \ No newline at end of file diff --git a/tests/Fixtures/ParallelIncludePath.xml b/tests/Fixtures/ParallelIncludePath.xml new file mode 100644 index 00000000..9566d246 --- /dev/null +++ b/tests/Fixtures/ParallelIncludePath.xml @@ -0,0 +1,6 @@ + + + + ParallelIncludePath/xml-lib + + diff --git a/tests/Fixtures/ParallelIncludePath/RequiresThroughIncludePathTest.php b/tests/Fixtures/ParallelIncludePath/RequiresThroughIncludePathTest.php new file mode 100644 index 00000000..e9fe2bb2 --- /dev/null +++ b/tests/Fixtures/ParallelIncludePath/RequiresThroughIncludePathTest.php @@ -0,0 +1,11 @@ +toBe('resolved through the include path'); +}); + +test('requires a file through the include path while running', function () { + expect(require 'parallel-include-path.inc')->toBe('resolved through the include path'); +}); diff --git a/tests/Fixtures/ParallelIncludePath/bootstrap.inc b/tests/Fixtures/ParallelIncludePath/bootstrap.inc new file mode 100644 index 00000000..769ed903 --- /dev/null +++ b/tests/Fixtures/ParallelIncludePath/bootstrap.inc @@ -0,0 +1,7 @@ + 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'], + ); + + $process->run(); + + return $process; +}; + +test('parallel resolves requires through the include path', function (array $arguments) use ($run): void { + $process = $run($arguments); + $output = (string) removeAnsiEscapeSequences($process->getOutput()); + + expect($process->getExitCode())->toBe(0, $process->getErrorOutput().$output) + ->and($output)->toContain('Tests: 2 passed'); +})->with([ + 'value in the same argument' => [['--include-path=tests/Fixtures/ParallelIncludePath/lib']], + 'value as a separate argument' => [['--include-path', 'tests/Fixtures/ParallelIncludePath/lib']], + 'absolute path' => [['--include-path='.__DIR__.'/../Fixtures/ParallelIncludePath/lib']], + 'several paths' => [['--include-path=tests/Fixtures/Inheritance'.PATH_SEPARATOR.'tests/Fixtures/ParallelIncludePath/lib']], + 'directory name with an equals sign' => [['--include-path', 'tests/Fixtures/ParallelIncludePath/include=path']], + 'absolute directory name with an equals sign' => [['--include-path', __DIR__.'/../Fixtures/ParallelIncludePath/include=path']], + 'require in the bootstrap file' => [['--bootstrap=tests/Fixtures/ParallelIncludePath/bootstrap.inc', '--include-path=tests/Fixtures/ParallelIncludePath/lib']], + 'command line before the XML include path' => [['--configuration=tests/Fixtures/ParallelIncludePath.xml', '--include-path=tests/Fixtures/ParallelIncludePath/lib']], + 'command line before the XML include path in the bootstrap file' => [['--configuration=tests/Fixtures/ParallelIncludePath.xml', '--bootstrap=tests/Fixtures/ParallelIncludePath/bootstrap.inc', '--include-path=tests/Fixtures/ParallelIncludePath/lib']], +])->skipOnWindows(); + +test('parallel places the include path before the argument separator', function (): void { + $process = new Process( + ['php', '-r', 'require "vendor/autoload.php"; echo json_encode((new Pest\\Plugins\\Parallel\\Handlers\\IncludePath)->handleArguments(["--include-path", "lib", "--", "tests/Foo"]));'], + dirname(__DIR__, 2), + ); + + $process->run(); + + expect($process->getExitCode())->toBe(0, $process->getErrorOutput()) + ->and(json_decode($process->getOutput(), true))->toBe(['--include-path=lib', '--', 'tests/Foo']); +}); + +test('parallel fails to resolve requires without the include path', function () use ($run): void { + $process = $run([]); + $output = (string) removeAnsiEscapeSequences($process->getOutput().$process->getErrorOutput()); + + expect($process->getExitCode())->not->toBe(0) + ->and($output)->toContain("Failed opening required 'parallel-include-path.inc'"); +})->skipOnWindows(); diff --git a/tests/Visual/ParallelIncludePathLeadingSpace.php b/tests/Visual/ParallelIncludePathLeadingSpace.php new file mode 100644 index 00000000..dfef8ce2 --- /dev/null +++ b/tests/Visual/ParallelIncludePathLeadingSpace.php @@ -0,0 +1,27 @@ +fromParameters(["--include-path= lib"])->includePath(); + $handler = new Pest\Plugins\Parallel\Handlers\IncludePath; + $forwarded = $handler->handleArguments(["--include-path= lib"]); + echo json_encode(["serial" => $serial, "forwarded" => $forwarded, "first" => explode(PATH_SEPARATOR, get_include_path())[0], "worker" => $handler->handleWorkerArguments([])]); + PHP_WRAP; + + $process = new Process(['php', '-r', $script], dirname(__DIR__, 2)); + + $process->run(); + + expect($process->getExitCode())->toBe(0, $process->getErrorOutput()) + ->and(json_decode($process->getOutput(), true))->toBe([ + 'serial' => ' lib', + 'forwarded' => ['--include-path= lib'], + 'first' => ' lib', + 'worker' => ['--include-path= lib'], + ]); +}); diff --git a/tests/Visual/ParallelIncludePathOptionWithLeadingSpace.php b/tests/Visual/ParallelIncludePathOptionWithLeadingSpace.php new file mode 100644 index 00000000..7b6e632e --- /dev/null +++ b/tests/Visual/ParallelIncludePathOptionWithLeadingSpace.php @@ -0,0 +1,27 @@ +fromParameters(["phpunit", ...$arguments])->includePath(); + $handler = new Pest\Plugins\Parallel\Handlers\IncludePath; + $forwarded = $handler->handleArguments($arguments); + echo json_encode(["serial" => $serial, "forwarded" => $forwarded, "worker" => $handler->handleWorkerArguments([])]); + PHP_WRAP; + + $process = new Process(['php', '-r', $script], dirname(__DIR__, 2)); + + $process->run(); + + expect($process->getExitCode())->toBe(0, $process->getErrorOutput()) + ->and(json_decode($process->getOutput(), true))->toBe([ + 'serial' => 'lib', + 'forwarded' => ['--include-path=lib'], + 'worker' => ['--include-path=lib'], + ]); +}); diff --git a/tests/Visual/ParallelIncludePathTrailingSpace.php b/tests/Visual/ParallelIncludePathTrailingSpace.php new file mode 100644 index 00000000..3266f7ce --- /dev/null +++ b/tests/Visual/ParallelIncludePathTrailingSpace.php @@ -0,0 +1,27 @@ + (new PHPUnit\TextUI\CliArguments\Builder(PHPUnit\Event\Facade::emitter()))->fromParameters(["phpunit", ...$arguments])->includePath(); + $arguments = ["--include-path=lib =extra"]; + $handler = new Pest\Plugins\Parallel\Handlers\IncludePath; + $forwarded = $handler->handleArguments($arguments); + echo json_encode(["serial" => $read($arguments), "forwarded" => $read($forwarded), "worker" => $read($handler->handleWorkerArguments([]))]); + PHP_WRAP; + + $process = new Process(['php', '-r', $script], dirname(__DIR__, 2)); + + $process->run(); + + expect($process->getExitCode())->toBe(0, $process->getErrorOutput()) + ->and(json_decode($process->getOutput(), true))->toBe([ + 'serial' => 'lib ', + 'forwarded' => 'lib ', + 'worker' => 'lib ', + ]); +}); diff --git a/tests/Visual/ParallelIncludePathWhitespace.php b/tests/Visual/ParallelIncludePathWhitespace.php new file mode 100644 index 00000000..3c13cf05 --- /dev/null +++ b/tests/Visual/ParallelIncludePathWhitespace.php @@ -0,0 +1,20 @@ + 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'], + ); + + $process->run(); + + $output = (string) removeAnsiEscapeSequences($process->getOutput().$process->getErrorOutput()); + + expect($output)->not->toContain('Required argument for option "--include-path" is missing') + ->and($process->getExitCode())->toBe(0, $output); +})->skipOnWindows(); diff --git a/tests/Visual/ParallelIncludePathWithoutValue.php b/tests/Visual/ParallelIncludePathWithoutValue.php new file mode 100644 index 00000000..d7f9bed7 --- /dev/null +++ b/tests/Visual/ParallelIncludePathWithoutValue.php @@ -0,0 +1,24 @@ +getValue(), + [Pest\Plugins\Parallel\Handlers\Parallel::class, Pest\Plugins\Parallel\Handlers\IncludePath::class], + )); + $forwarded = array_reduce($handlers, fn (array $arguments, string $handler): array => (new $handler)->handleArguments($arguments), ["--parallel", "--include-path="]); + echo json_encode(["runner" => in_array("--runner=".Pest\Plugins\Parallel\Paratest\WrapperRunner::class, $forwarded, true)]); + PHP_WRAP; + + $process = new Process(['php', '-r', $script], dirname(__DIR__, 2)); + + $process->run(); + + expect($process->getExitCode())->toBe(0, $process->getErrorOutput()) + ->and(json_decode($process->getOutput(), true))->toBe(['runner' => true]); +});