From d2c1b99236d32c0ab8963dc26d623eb99779d969 Mon Sep 17 00:00:00 2001 From: Jhon Farias Date: Fri, 4 Dec 2026 10:11:42 +0100 Subject: [PATCH] fix: keep the options that Pest adds before `--` (#2087) * fix: keep added options before the `--` separator Pest added its own options (`--runner`, `--cache-directory`, `--teamcity`, `--no-coverage`, ...) after the `--` that the user gave. PHPUnit and ParaTest then read them as paths, thus `pest -- ` and `pest --parallel -- ` failed. Co-Authored-By: Claude Opus 5.5 * fix: keep every option that Pest adds before `--` pushArgument renumbers the list before it searches `--`, because bin/pest unsets options without array_values. Coverage, Shard and the `--teamcity` handling of bin/pest now place their options before `--` too. The changed files lose their comments, as AGENTS.md section 4 asks. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Punyapal Shah --- bin/pest | 7 +------ src/Plugins/Concerns/HandleArguments.php | 11 ++++++++++- src/Plugins/Coverage.php | 15 +++------------ src/Plugins/Parallel.php | 5 +---- src/Plugins/Shard.php | 12 ++++-------- src/Plugins/Tia.php | 2 +- tests/.snapshots/success.txt | 8 +++++++- tests/Unit/Plugins/Concerns/PushArgument.php | 15 +++++++++++++++ tests/Visual/Parallel.php | 8 +++++++- tests/Visual/Shard.php | 12 ++++++++++++ tests/Visual/SingleTestOrDirectory.php | 12 ++++++++++++ 11 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 tests/Unit/Plugins/Concerns/PushArgument.php diff --git a/bin/pest b/bin/pest index 4d8f344f..c546cb0f 100755 --- a/bin/pest +++ b/bin/pest @@ -20,7 +20,6 @@ use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; (static function () { - // Ensures Collision's Printer is registered. $_SERVER['COLLISION_PRINTER'] = 'DefaultPrinter'; $arguments = $originalArguments = $_SERVER['argv']; @@ -123,8 +122,7 @@ use Symfony\Component\Console\Output\ConsoleOutput; } if ($value === '--teamcity') { - unset($arguments[$key]); - $arguments[] = '--no-output'; + $arguments[$key] = '--no-output'; unset($_SERVER['COLLISION_PRINTER']); } } @@ -143,7 +141,6 @@ use Symfony\Component\Console\Output\ConsoleOutput; $isOptionValue = false; - // Convert namespace-style arguments (e.g. Tests\Unit\TestName) to file paths. foreach ($arguments as $key => $value) { if ($isOptionValue) { $isOptionValue = false; @@ -173,7 +170,6 @@ use Symfony\Component\Console\Output\ConsoleOutput; continue; } - // Try lowercasing the first directory segment (e.g. Tests -> tests). $segments = explode(DIRECTORY_SEPARATOR, $path); $segments[0] = lcfirst($segments[0]); $lowercasedPath = implode(DIRECTORY_SEPARATOR, $segments); @@ -183,7 +179,6 @@ use Symfony\Component\Console\Output\ConsoleOutput; } } - // Used when Pest is required using composer. $autoloadPath = $GLOBALS['_composer_autoload_path'] ?? dirname(__DIR__, 3).'/autoload.php'; if (! is_file($autoloadPath)) { diff --git a/src/Plugins/Concerns/HandleArguments.php b/src/Plugins/Concerns/HandleArguments.php index d64708d9..b4d8008a 100644 --- a/src/Plugins/Concerns/HandleArguments.php +++ b/src/Plugins/Concerns/HandleArguments.php @@ -33,7 +33,16 @@ trait HandleArguments */ public function pushArgument(string $argument, array $arguments): array { - $arguments[] = $argument; + $arguments = array_values($arguments); + $endOfOptionsPosition = array_search('--', $arguments, true); + + if ($endOfOptionsPosition === false) { + $arguments[] = $argument; + + return $arguments; + } + + array_splice($arguments, $endOfOptionsPosition, 0, [$argument]); return $arguments; } diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index cd3b3759..1c807ffd 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -47,14 +47,8 @@ final class Coverage implements AddsOutput, HandlesArguments public bool $showOnlyCovered = false; - public function __construct(private readonly OutputInterface $output) - { - // - } + public function __construct(private readonly OutputInterface $output) {} - /** - * {@inheritdoc} - */ public function handleArguments(array $originals): array { [$arguments, $originals] = $this->extractOwnArguments($originals); @@ -68,8 +62,8 @@ final class Coverage implements AddsOutput, HandlesArguments $input = new ArgvInput($arguments, new InputDefinition($inputs)); if ((bool) $input->getOption(self::COVERAGE_OPTION)) { $this->coverage = true; - $originals[] = '--coverage-php'; - $originals[] = \Pest\Support\Coverage::getPath(); + $originals = $this->pushArgument('--coverage-php', $originals); + $originals = $this->pushArgument(\Pest\Support\Coverage::getPath(), $originals); if (! \Pest\Support\Coverage::isAvailable()) { if (\Pest\Support\Coverage::usingXdebug()) { @@ -158,9 +152,6 @@ final class Coverage implements AddsOutput, HandlesArguments return [['', ...$owned], $forwarded]; } - /** - * {@inheritdoc} - */ public function addOutput(int $exitCode): int { if (Parallel::isWorker()) { diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index 106aec9c..e37a23aa 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -79,9 +79,6 @@ final class Parallel implements HandlesArguments return null; } - /** - * {@inheritdoc} - */ public function handleArguments(array $arguments): array { if ($this->hasArgumentsThatWouldBeFasterWithoutParallel()) { @@ -182,7 +179,7 @@ final class Parallel implements HandlesArguments { $argv = new ArgvInput; if ($argv->hasParameterOption('--teamcity')) { - $arguments[] = '--teamcity'; + $arguments = $this->pushArgument('--teamcity', $arguments); } return $arguments; diff --git a/src/Plugins/Shard.php b/src/Plugins/Shard.php index 47257189..adc98f91 100644 --- a/src/Plugins/Shard.php +++ b/src/Plugins/Shard.php @@ -98,9 +98,7 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable public function __construct( private readonly OutputInterface $output, - ) { - // - } + ) {} public static function useTimingsFile(string $filename): void { @@ -144,9 +142,6 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable return self::$ownership[$unit]; } - /** - * {@inheritDoc} - */ public function handleArguments(array $arguments): array { if ($this->hasArgument('--update-shards', $arguments)) { @@ -220,7 +215,8 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable $this->ensureFilterLengthIsSafe($filter); - $arguments = [...$arguments, '--filter', $filter]; + $arguments = $this->pushArgument('--filter', $arguments); + $arguments = $this->pushArgument($filter, $arguments); if ($this->hasArgument('--do-not-fail-on-empty-test-suite', $arguments) || $this->hasArgument('--fail-on-empty-test-suite', $arguments)) { return $arguments; @@ -350,7 +346,7 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable } } - return [PHP_BINARY, ...$filtered, '--test-directory='.$testPath, '--list-tests']; + return [PHP_BINARY, ...$this->pushArgument('--list-tests', $this->pushArgument('--test-directory='.$testPath, $filtered))]; } /** diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 3fc36f95..a30f2d71 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -1734,7 +1734,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument } if (! $this->piggybackCoverage && ! in_array('--no-coverage', $arguments, true)) { - $arguments[] = '--no-coverage'; + $arguments = $this->pushArgument('--no-coverage', $arguments); } if (Parallel::isEnabled()) { diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 69a548b6..13d3f84e 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1893,6 +1893,9 @@ ✓ popArgument preserves duplicate values when removing a missing argument ✓ popArgument preserves duplicate values when removing an existing argument + PASS Tests\Unit\Plugins\Concerns\PushArgument + ✓ pushArgument inserts before the option separator when the keys have gaps + PASS Tests\Unit\Plugins\Environment ✓ environment is set to CI when --ci option is used ✓ environment is set to Local when --ci option is not used @@ -2751,6 +2754,7 @@ ✓ parallel does not record the test run history when asked not to with ('--do-not-record-test-run-history') ✓ 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 + ✓ parallel accepts a path after the option separator PASS Tests\Visual\ParallelIncludePath ✓ parallel resolves requires through the include path with dataset "value in the same argument" @@ -2861,6 +2865,7 @@ ✓ an empty shard runs no tests with dataset "parallel with fail-on-empty-test-suite" ✓ an empty shard lists no tests ✓ a non-empty shard runs its assigned test + ✓ shard accepts a path after the option separator PASS Tests\Visual\SingleTestOrDirectory ✓ allows to run a single test @@ -2869,6 +2874,7 @@ ✓ keeps namespace filters unchanged ✓ converts a namespace after the retry flag ✓ it disable decorating printer when colors is set to never + ✓ allows to run a single test after the option separator WARN Tests\Visual\Success - visual snapshot of test suite on success @@ -2898,4 +2904,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, 45 skipped, 2151 passed (4730 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 45 skipped, 2155 passed (4735 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Concerns/PushArgument.php b/tests/Unit/Plugins/Concerns/PushArgument.php new file mode 100644 index 00000000..1e0f8ff3 --- /dev/null +++ b/tests/Unit/Plugins/Concerns/PushArgument.php @@ -0,0 +1,15 @@ + 'bin/pest', 2 => '--', 3 => 'tests/Fixtures/ExampleTest.php']; + + expect($argumentHandler->pushArgument('--no-output', $arguments)) + ->toBe(['bin/pest', '--no-output', '--', 'tests/Fixtures/ExampleTest.php']); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 04eba2bc..b6a42d29 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 37 skipped, 2004 passed (4168 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 37 skipped, 2005 passed (4169 assertions)'; expect($output) ->toContain("Tests: {$expected}") @@ -162,3 +162,9 @@ test('parallel group-filtered run still fails when the selection matches no test expect(removeAnsiEscapeSequences($process->getOutput()))->toContain('No tests found.') ->and($process->getExitCode())->toBe(1); })->skipOnWindows(); + +test('parallel accepts a path after the option separator', function () use ($run): void { + expect($run('--', 'tests/Fixtures/ExampleTest.php')) + ->toContain('Tests: 1 passed (1 assertions)') + ->toContain('Parallel: 3 processes'); +})->skipOnWindows(); diff --git a/tests/Visual/Shard.php b/tests/Visual/Shard.php index b20ca196..03b06d90 100644 --- a/tests/Visual/Shard.php +++ b/tests/Visual/Shard.php @@ -47,3 +47,15 @@ test('a non-empty shard runs its assigned test', function (): void { expect($process->getExitCode())->toBe(0) ->and($process->getOutput())->toContain('1 passed', '1 of 2 — 1 file ran, out of 1.'); }); + +test('shard accepts a path after the option separator', function (): void { + $process = new Process( + ['php', 'bin/pest', '--shard=1/2', '--', 'tests/Fixtures/ExampleTest.php'], + dirname(__DIR__, 2), + ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'], + ); + + $process->run(); + + expect(removeAnsiEscapeSequences($process->getOutput()))->toContain('Tests: 1 passed (1 assertions)'); +})->skipOnWindows(); diff --git a/tests/Visual/SingleTestOrDirectory.php b/tests/Visual/SingleTestOrDirectory.php index a7db3431..66b4a632 100644 --- a/tests/Visual/SingleTestOrDirectory.php +++ b/tests/Visual/SingleTestOrDirectory.php @@ -79,3 +79,15 @@ it('disable decorating printer when colors is set to never', function () use ($s $output = $process->getOutput(); expect($output)->toContain($snapshot('disable-decorating-printer')); })->skipOnWindows(); + +test('allows to run a single test after the option separator', function (): void { + $process = new Process( + ['php', 'bin/pest', '--', 'tests/Fixtures/ExampleTest.php'], + dirname(__DIR__, 2), + ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'], + ); + + $process->run(); + + expect(removeAnsiEscapeSequences($process->getOutput()))->toContain('Tests: 1 passed (1 assertions)'); +})->skipOnWindows();