fix(sharding): prevent shard filters from matching classes outside their partition (#1884)

* fix(sharding): prevent shard filters from matching classes outside their partition

* lint

* test: update snapshots for the shard filter boundary cases
This commit is contained in:
Punyapal Shah
2026-10-12 00:09:04 +05:30
committed by GitHub
parent 68b4c9176d
commit 5b98dc7df2
4 changed files with 74 additions and 12 deletions
+25 -5
View File
@@ -29,6 +29,8 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable
private const int MAX_FILTER_LENGTH = 32768;
private const string CLASS_END = "\0";
/**
* @var array{
* index: int,
@@ -237,17 +239,35 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable
$current[$part] ??= [];
$current = &$current[$part];
}
$current[self::CLASS_END] = true;
}
$buildRegex = function (array $tree) use (&$buildRegex): string {
$parts = [];
foreach ($tree as $key => $sub) {
$subRegex = $buildRegex($sub);
if ($subRegex === '') {
$parts[] = preg_quote($key, '/');
} else {
$parts[] = preg_quote($key, '/').'\\\\'.(count($sub) > 1 ? '('.$subRegex.')' : $subRegex);
if ($key === self::CLASS_END) {
continue;
}
assert(is_array($sub));
$endsWithClass = isset($sub[self::CLASS_END]);
$sub = array_diff_key($sub, [self::CLASS_END => null]);
$subRegex = $sub === [] ? '' : $buildRegex($sub);
$segment = preg_quote($key, '/');
if ($subRegex !== '') {
$segment .= '\\\\'.(count($sub) > 1 ? '('.$subRegex.')' : $subRegex);
}
if ($endsWithClass && $subRegex !== '') {
$parts[] = '(?:'.preg_quote($key, '/').'(?=::)|'.$segment.')';
continue;
}
$parts[] = $endsWithClass ? $segment.'(?=::)' : $segment;
}
return implode('|', $parts);
+4 -1
View File
@@ -1839,6 +1839,9 @@
✓ buildFilterArgument → it returns empty string for empty test list
✓ buildFilterArgument → it generates compact filter for deeply nested namespaces
✓ buildFilterArgument → it handles mix of nested and flat namespaces
✓ buildFilterArgument → it does not match a class outside of the shard when another class name is its prefix
✓ buildFilterArgument → it still matches dataset variants after the class boundary
✓ buildFilterArgument → it keeps a class matchable when another class nests below its namespace
✓ ensureFilterLengthIsSafe → it accepts filter within length limit
✓ ensureFilterLengthIsSafe → it throws exception when filter exceeds default limit
✓ ensureFilterLengthIsSafe → it respects custom limit from environment variable
@@ -2259,4 +2262,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, 35 skipped, 1600 passed (3489 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1603 passed (3495 assertions)
+44 -5
View File
@@ -60,7 +60,7 @@ describe('buildFilterArgument', function (): void {
$filter = $method->invoke($shard, ['Tests\\Unit\\ExampleTest']);
expect($filter)->toBe('Tests\\\\Unit\\\\ExampleTest');
expect($filter)->toBe('Tests\\\\Unit\\\\ExampleTest(?=::)');
});
it('generates compact filter for multiple tests with common prefix', function (): void {
@@ -75,7 +75,7 @@ describe('buildFilterArgument', function (): void {
'Tests\\Unit\\Foo\\BazTest',
]);
expect($filter)->toBe('Tests\\\\Unit\\\\Foo\\\\(BarTest|BazTest)');
expect($filter)->toBe('Tests\\\\Unit\\\\Foo\\\\(BarTest(?=::)|BazTest(?=::))');
});
it('generates compact filter for tests with different namespaces', function (): void {
@@ -90,7 +90,7 @@ describe('buildFilterArgument', function (): void {
'Tests\\Feature\\BarTest',
]);
expect($filter)->toBe('Tests\\\\(Unit\\\\FooTest|Feature\\\\BarTest)');
expect($filter)->toBe('Tests\\\\(Unit\\\\FooTest(?=::)|Feature\\\\BarTest(?=::))');
});
it('returns empty string for empty test list', function (): void {
@@ -118,7 +118,7 @@ describe('buildFilterArgument', function (): void {
'Tests\\Unit\\Plugins\\Concerns\\Baz',
]);
expect($filter)->toBe('Tests\\\\Unit\\\\Plugins\\\\Concerns\\\\(Foo|Bar|Baz)');
expect($filter)->toBe('Tests\\\\Unit\\\\Plugins\\\\Concerns\\\\(Foo(?=::)|Bar(?=::)|Baz(?=::))');
});
it('handles mix of nested and flat namespaces', function (): void {
@@ -138,7 +138,46 @@ describe('buildFilterArgument', function (): void {
$filter = $method->invoke($shard, $tests);
expect($filter)
->toBe(addslashes('Tests\\Unit\\(SimpleTest|Plugins\\Concerns\\(HandleArguments|Validation)|Another\\Deep\\Nested\\Test)'));
->toBe(addslashes('Tests\\Unit\\(SimpleTest(?=::)|Plugins\\Concerns\\(HandleArguments(?=::)|Validation(?=::))|Another\\Deep\\Nested\\Test(?=::))'));
});
it('does not match a class outside of the shard when another class name is its prefix', function (): void {
$output = new BufferedOutput;
$shard = new Shard($output);
$reflection = new ReflectionClass($shard);
$method = $reflection->getMethod('buildFilterArgument');
$filter = $method->invoke($shard, ['Tests\\Feature\\After']);
expect(preg_match('{'.$filter.'}i', 'P\Tests\Feature\AfterAll::deletes file after all'))->toBe(0)
->and('P\Tests\Feature\After::it runs')->toMatch('{'.$filter.'}i');
});
it('still matches dataset variants after the class boundary', function (): void {
$output = new BufferedOutput;
$shard = new Shard($output);
$reflection = new ReflectionClass($shard);
$method = $reflection->getMethod('buildFilterArgument');
$filter = $method->invoke($shard, ['Tests\\Feature\\After']);
expect('P\Tests\Feature\After::it runs with data set "foo"')->toMatch('{'.$filter.'}i');
});
it('keeps a class matchable when another class nests below its namespace', function (): void {
$output = new BufferedOutput;
$shard = new Shard($output);
$reflection = new ReflectionClass($shard);
$method = $reflection->getMethod('buildFilterArgument');
$filter = $method->invoke($shard, ['Tests\\Unit\\After', 'Tests\\Unit\\After\\AfterAll']);
expect($filter)->toBe('Tests\\\\Unit\\\\(?:After(?=::)|After\\\\AfterAll(?=::))')
->and('P\Tests\Unit\After::x')->toMatch('{'.$filter.'}i')
->and('P\Tests\Unit\After\AfterAll::y')->toMatch('{'.$filter.'}i');
});
});
+1 -1
View File
@@ -20,7 +20,7 @@ test('parallel', function () use ($run): void {
$output = $run('--exclude-group=integration');
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1581 passed (3431 assertions)';
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1584 passed (3437 assertions)';
expect($output)
->toContain("Tests: {$expected}")