From fa6e96486ee63aa5c58588b1ae4b3cd7a727badd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Gajda?= Date: Fri, 27 Nov 2026 07:24:04 +0100 Subject: [PATCH] fix(sharding): strip every paratest-only option from the `--list-tests` sub-process (#2076) * fix(sharding): strip every paratest-only option from the `--list-tests` sub-process `--update-shards` (and `--shard` without a shards.json) discovers the test classes by re-running Pest with `--list-tests`. That sub-process is a plain PHPUnit run, but only `--parallel`, `-p`, `--processes` and `--runner` were removed from its arguments, so any other ParaTest option killed the run before a single test: pest --parallel --update-shards --max-batch-size=100 Unknown option "--max-batch-size". Strip every option ParaTest defines that PHPUnit does not (`--max-batch-size`/`-m`, `--max-processes`, `--passthru-php`, `--tmp-dir`, `--functional`, `--no-test-tokens`, plus the existing `--processes` and `--runner`), in both `--option=value` and `--option value` form. * fix(sharding): strip ParaTest's `-m` with its value attached (`-m100`) ParaTest reads `-m100` as `--max-batch-size=100`, but the option check split arguments on `=` only, so the attached form reached PHPUnit's `--list-tests` sub-process, which rejects `-m`. `-p` is left alone: Pest reads it as `--parallel`. --------- Co-authored-by: Punyapal Shah --- src/Plugins/Shard.php | 51 +++++++++++++++++++++++++++++------- tests/.snapshots/success.txt | 13 ++++++++- tests/Unit/Plugins/Shard.php | 31 ++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/Plugins/Shard.php b/src/Plugins/Shard.php index 76070065..47257189 100644 --- a/src/Plugins/Shard.php +++ b/src/Plugins/Shard.php @@ -31,6 +31,26 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable private const string CLASS_END = "\0"; + /** + * @var array + */ + private const array PARATEST_ONLY_OPTIONS = [ + '--processes' => true, + '--runner' => true, + '--max-batch-size' => true, + '-m' => true, + '--max-processes' => true, + '--passthru-php' => true, + '--tmp-dir' => true, + '--functional' => false, + '--no-test-tokens' => false, + ]; + + /** + * @var list + */ + private const array PARATEST_ONLY_SHORT_OPTIONS_WITH_VALUE = ['-m']; + /** * @var array{ * index: int, @@ -270,24 +290,37 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable } } - if (in_array($argument, ['--runner', '--processes'], strict: true)) { - $skipNext = true; + if (in_array($argument, ['--parallel', '-p'], strict: true)) { + continue; + } + + if ($this->isAttachedParatestShortOption($argument)) { + continue; + } + + [$option] = explode('=', $argument, 2); + + if (! array_key_exists($option, self::PARATEST_ONLY_OPTIONS)) { + $filtered[] = $argument; continue; } - if (in_array($argument, ['--parallel', '-p'], strict: true) - || str_starts_with($argument, '--processes') - || str_starts_with($argument, '--runner=')) { - continue; - } - - $filtered[] = $argument; + $skipNext = self::PARATEST_ONLY_OPTIONS[$option] && ! str_contains($argument, '='); } return $filtered; } + private function isAttachedParatestShortOption(string $argument): bool + { + if (strlen($argument) <= 2 || str_starts_with($argument, '--')) { + return false; + } + + return in_array(substr($argument, 0, 2), self::PARATEST_ONLY_SHORT_OPTIONS_WITH_VALUE, strict: true); + } + /** * @param list $arguments * @return list diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index b15e01b5..e1faa3af 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1939,6 +1939,17 @@ ✓ buildListTestsCommand → it strips --processes N (space-separated) when building the list-tests command ✓ buildListTestsCommand → it strips --runner when building the list-tests command ✓ buildListTestsCommand → it strips a space-separated --runner and its value when building the list-tests command + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--max-batch-size=N" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--max-batch-size N" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "-m N" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "-mN" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "-m=N" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--processes N" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--max-processes=N" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--passthru-php" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--tmp-dir" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--functional" + ✓ buildListTestsCommand → it strips paratest-only options when building the list-tests command with dataset "--no-test-tokens" ✓ addOutput → it displays shard information after test execution ✓ addOutput → it uses singular form for single test file ✓ addOutput → it returns original exit code when shard is not set @@ -2693,4 +2704,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, 39 skipped, 1978 passed (4476 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1989 passed (4487 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Shard.php b/tests/Unit/Plugins/Shard.php index a6feec35..b71a0b2e 100644 --- a/tests/Unit/Plugins/Shard.php +++ b/tests/Unit/Plugins/Shard.php @@ -565,6 +565,37 @@ describe('buildListTestsCommand', function (): void { '--list-tests', ]); }); + + it('strips paratest-only options when building the list-tests command', function (array $arguments): void { + $output = new BufferedOutput; + $shard = new Shard($output); + + $reflection = new ReflectionClass($shard); + $method = $reflection->getMethod('buildListTestsCommand'); + + $command = $method->invoke($shard, ['vendor/bin/pest', '--parallel', ...$arguments, '--stop-on-failure', '--update-shards'], 'tests'); + + expect($command)->toBe([ + PHP_BINARY, + 'vendor/bin/pest', + '--stop-on-failure', + '--update-shards', + '--test-directory=tests', + '--list-tests', + ]); + })->with([ + '--max-batch-size=N' => [['--max-batch-size=100']], + '--max-batch-size N' => [['--max-batch-size', '100']], + '-m N' => [['-m', '100']], + '-mN' => [['-m100']], + '-m=N' => [['-m=100']], + '--processes N' => [['--processes', '8']], + '--max-processes=N' => [['--max-processes=8']], + '--passthru-php' => [['--passthru-php=-d memory_limit=1G']], + '--tmp-dir' => [['--tmp-dir', '/tmp/paratest']], + '--functional' => [['--functional']], + '--no-test-tokens' => [['--no-test-tokens']], + ]); }); describe('addOutput', function (): void { diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index a0299c48..1be1b367 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, 31 skipped, 1850 passed (3949 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1861 passed (3960 assertions)'; expect($output) ->toContain("Tests: {$expected}")