diff --git a/src/Plugins/Tia/SourceScope.php b/src/Plugins/Tia/SourceScope.php index b6c2220c..ab4f4234 100644 --- a/src/Plugins/Tia/SourceScope.php +++ b/src/Plugins/Tia/SourceScope.php @@ -35,7 +35,7 @@ final class SourceScope /** * @param list $includes Absolute, normalised directory paths. - * @param list $excludes Absolute, normalised directory paths. + * @param list $excludes Absolute, normalised directory paths with the file name prefix and suffix they apply to. */ public function __construct( private readonly array $includes, @@ -55,7 +55,11 @@ final class SourceScope } foreach ($source->excludeDirectories() as $dir) { - $phpunitExcludes[] = self::normalise($dir->path()); + $phpunitExcludes[] = [ + 'path' => self::normalise(@realpath($dir->path()) ?: $dir->path()), + 'prefix' => $dir->prefix(), + 'suffix' => $dir->suffix(), + ]; } } catch (Throwable) { // @@ -64,10 +68,10 @@ final class SourceScope $rootIncludes = self::topLevelProjectDirs($projectRoot); $includes = array_values(array_unique([...$phpunitIncludes, ...$rootIncludes])); - $excludes = array_values(array_unique([ + $excludes = [ ...$phpunitExcludes, ...self::nestedNoiseDirs($projectRoot), - ])); + ]; if ($includes === []) { $includes = [self::normalise($projectRoot)]; @@ -111,7 +115,9 @@ final class SourceScope $candidate = self::normalise($candidate); foreach ($this->excludes as $excluded) { - if ($this->startsWithDir($candidate, $excluded)) { + if ($this->startsWithDir($candidate, $excluded['path']) + && str_starts_with(basename($candidate), $excluded['prefix']) + && str_ends_with(basename($candidate), $excluded['suffix'])) { return $this->containsCache[$absoluteFile] = false; } } @@ -166,7 +172,7 @@ final class SourceScope } /** - * @return list + * @return list */ private static function nestedNoiseDirs(string $projectRoot): array { @@ -174,7 +180,7 @@ final class SourceScope foreach (self::NESTED_NOISE as $relative) { $abs = $projectRoot.DIRECTORY_SEPARATOR.str_replace('/', DIRECTORY_SEPARATOR, $relative); - $out[] = self::normalise(@realpath($abs) ?: $abs); + $out[] = ['path' => self::normalise(@realpath($abs) ?: $abs), 'prefix' => '', 'suffix' => '']; } return $out; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index d3ca94dc..b7d75383 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2061,6 +2061,11 @@ ✓ it records assertions against the same per-dataset key ✓ it leaves a test without a dataset keyed by class and method + PASS Tests\Unit\Plugins\Tia\SourceScope + ✓ contains() → it excludes only the files matching the suffix of an excluded directory + ✓ contains() → it excludes only the files matching the prefix of an excluded directory + ✓ contains() → it excludes every file of an excluded directory without a prefix or suffix + WARN Tests\Unit\Plugins\Tia\StaleCoverageLines - a remeasured file drops line numbers that the current file no longer has → Coverage is not available @@ -2516,4 +2521,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, 38 skipped, 1826 passed (4140 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1829 passed (4146 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Tia/SourceScope.php b/tests/Unit/Plugins/Tia/SourceScope.php new file mode 100644 index 00000000..d2077c81 --- /dev/null +++ b/tests/Unit/Plugins/Tia/SourceScope.php @@ -0,0 +1,37 @@ + '/project/src', 'prefix' => '', 'suffix' => 'Test.php']], + ); + + expect($scope->contains('/project/src/Calculator.php'))->toBeTrue() + ->and($scope->contains('/project/src/CalculatorTest.php'))->toBeFalse(); + }); + + it('excludes only the files matching the prefix of an excluded directory', function (): void { + $scope = new SourceScope( + ['/project/src'], + [['path' => '/project/src', 'prefix' => 'Legacy', 'suffix' => '.php']], + ); + + expect($scope->contains('/project/src/Calculator.php'))->toBeTrue() + ->and($scope->contains('/project/src/LegacyCalculator.php'))->toBeFalse(); + }); + + it('excludes every file of an excluded directory without a prefix or suffix', function (): void { + $scope = new SourceScope( + ['/project'], + [['path' => '/project/storage/framework', 'prefix' => '', 'suffix' => '']], + ); + + expect($scope->contains('/project/app/Calculator.php'))->toBeTrue() + ->and($scope->contains('/project/storage/framework/views/compiled.php'))->toBeFalse(); + }); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 6ae4e077..90e952ec 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, 30 skipped, 1731 passed (3744 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1734 passed (3750 assertions)'; expect($output) ->toContain("Tests: {$expected}")