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 <mrpunyapal@gmail.com>
This commit is contained in:
Bartłomiej Gajda
2026-11-27 07:24:04 +01:00
committed by GitHub
parent 249a120d89
commit fa6e96486e
4 changed files with 86 additions and 11 deletions
+42 -9
View File
@@ -31,6 +31,26 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable
private const string CLASS_END = "\0";
/**
* @var array<string, bool>
*/
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<string>
*/
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<string> $arguments
* @return list<string>
+12 -1
View File
@@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1989 passed (4487 assertions)
+31
View File
@@ -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 {
+1 -1
View File
@@ -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}")