Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/Plugins/Shard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -239,17 +241,35 @@ private function buildFilterArgument(array $testsToRun): string
}
$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);
Expand Down
49 changes: 44 additions & 5 deletions tests/Unit/Plugins/Shard.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

$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 {
Expand All @@ -75,7 +75,7 @@
'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 {
Expand All @@ -90,7 +90,7 @@
'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 {
Expand Down Expand Up @@ -118,7 +118,7 @@
'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 {
Expand All @@ -138,7 +138,46 @@
$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');
});
});

Expand Down
Loading