From 28c9dc277d4cbb88ddea7124837a7b3313017840 Mon Sep 17 00:00:00 2001 From: Daniel Polito Date: Sat, 14 Nov 2026 17:46:36 -0300 Subject: [PATCH] feat(tia): make default watch fallbacks configurable (#1950) * feat(tia): make default watch fallbacks configurable * test(tia): cover driverless fallback and update suite snapshots * fix: align help types and coverage guards with PHPStan --------- Co-authored-by: Punyapal Shah --- src/Plugins/Tia/Configuration.php | 13 ++ src/Plugins/Tia/Recorder.php | 2 +- src/Plugins/Tia/WatchPatterns.php | 85 +++++++++-- tests/.snapshots/success.txt | 18 ++- tests/Features/Tia/WatchFallbacks.php | 65 ++++++++ tests/Unit/Plugins/Tia/WatchPatterns.php | 183 +++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 7 files changed, 352 insertions(+), 16 deletions(-) create mode 100644 tests/Features/Tia/WatchFallbacks.php create mode 100644 tests/Unit/Plugins/Tia/WatchPatterns.php diff --git a/src/Plugins/Tia/Configuration.php b/src/Plugins/Tia/Configuration.php index f84ba983..ec32ac20 100644 --- a/src/Plugins/Tia/Configuration.php +++ b/src/Plugins/Tia/Configuration.php @@ -102,4 +102,17 @@ final class Configuration return $this; } + + /** + * @param list|null $patterns changed-file globs; null disables all default watches + * @return $this + */ + public function withoutDefaultWatchPatterns(?array $patterns = null): self + { + /** @var WatchPatterns $watchPatterns */ + $watchPatterns = Container::getInstance()->get(WatchPatterns::class); + $watchPatterns->excludeDefaults($patterns); + + return $this; + } } diff --git a/src/Plugins/Tia/Recorder.php b/src/Plugins/Tia/Recorder.php index 2d13a421..47750dc2 100644 --- a/src/Plugins/Tia/Recorder.php +++ b/src/Plugins/Tia/Recorder.php @@ -355,7 +355,7 @@ final class Recorder $out = []; foreach ($data as $file => $lines) { - if (! is_array($lines)) { + if (! is_array($lines) || $lines === []) { continue; } $covered = []; diff --git a/src/Plugins/Tia/WatchPatterns.php b/src/Plugins/Tia/WatchPatterns.php index 92d94534..fc1b9e29 100644 --- a/src/Plugins/Tia/WatchPatterns.php +++ b/src/Plugins/Tia/WatchPatterns.php @@ -31,6 +31,18 @@ final class WatchPatterns */ private array $patterns = []; + /** + * @var array> + */ + private array $defaultPatterns = []; + + /** + * @var list + */ + private array $excludedDefaultPatterns = []; + + private bool $defaultsEnabled = true; + /** * @var array, allowDotfiles: bool}> */ @@ -62,8 +74,8 @@ final class WatchPatterns } foreach ($default->defaults($projectRoot, $testPath) as $key => $dirs) { - $this->patterns[$key] = array_values(array_unique( - array_merge($this->patterns[$key] ?? [], $dirs), + $this->defaultPatterns[$key] = array_values(array_unique( + array_merge($this->defaultPatterns[$key] ?? [], $dirs), )); } } @@ -81,6 +93,23 @@ final class WatchPatterns } } + /** + * @param list|null $patterns + */ + public function excludeDefaults(?array $patterns = null): void + { + if ($patterns === null) { + $this->defaultsEnabled = false; + + return; + } + + $this->excludedDefaultPatterns = array_values(array_unique([ + ...$this->excludedDefaultPatterns, + ...$patterns, + ])); + } + /** * @param string $projectRoot Absolute path. * @param array $changedFiles Project-relative paths. @@ -88,27 +117,54 @@ final class WatchPatterns */ public function matchedDirectories(string $projectRoot, array $changedFiles): array { - if ($this->patterns === []) { - return []; - } - $matched = []; foreach ($changedFiles as $file) { - foreach ($this->patterns as $key => $dirs) { - if (! $this->keyMatches($key, $file)) { - continue; - } + foreach ($this->matchingDirectories($this->patterns, $file) as $dir) { + $matched[$dir] = true; + } - foreach ($dirs as $dir) { - $matched[$dir] = true; - } + if (! $this->usesDefaultsFor($file)) { + continue; + } + + foreach ($this->matchingDirectories($this->defaultPatterns, $file) as $dir) { + $matched[$dir] = true; } } return array_keys($matched); } + /** + * @param array> $patterns + * @return list + */ + private function matchingDirectories(array $patterns, string $file): array + { + $matched = []; + + foreach ($patterns as $key => $dirs) { + if (! $this->keyMatches($key, $file)) { + continue; + } + + foreach ($dirs as $dir) { + $matched[$dir] = true; + } + } + + return array_keys($matched); + } + + private function usesDefaultsFor(string $file): bool + { + return $this->defaultsEnabled && ! array_any( + $this->excludedDefaultPatterns, + fn (string $pattern): bool => $this->keyMatches($pattern, $file), + ); + } + /** * @param array $directories Project-relative dirs/files. * @param array $allTestFiles Project-relative test files from graph. @@ -220,6 +276,9 @@ final class WatchPatterns public function reset(): void { $this->patterns = []; + $this->defaultPatterns = []; + $this->excludedDefaultPatterns = []; + $this->defaultsEnabled = true; $this->parsed = []; $this->enabled = false; $this->locally = false; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 26b6a2c4..3d20dbbf 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2159,6 +2159,22 @@ ✓ it accepts a page directory candidate only when it matches the casing on disk with ('wrong parent') ✓ it accepts a page directory candidate only when it matches the casing on disk with ('absent') + PASS Tests\Unit\Plugins\Tia\WatchPatterns + ✓ it keeps the built-in fallbacks enabled by default + ✓ it excludes every overlapping default for a matching changed path + ✓ it retains custom watches when their matching defaults are excluded + ✓ it keeps watch additive when defaults are enabled + ✓ it disables all defaults without disabling custom watches + ✓ it retains exclusions when defaults are loaded after configuration + ✓ it accumulates exclusions from repeated calls + ✓ it does not disable defaults for an empty exclusion list + ✓ it supports exclusions within a changed-path pattern + ✓ it restores default watches and removes custom watches on reset + ✓ it does not select unrelated tests for an excluded unknown Vue page + ✓ it retains recorded Inertia dependencies when default watches are disabled + ✓ it retains recorded migration tables when default watches are disabled + ✓ it uses custom mappings for an unknown Vue page with defaults disabled + PASS Tests\Unit\Plugins\Tia\WorkerToken ✓ prefers the per-process UNIQUE_TEST_TOKEN over the per-slot TEST_TOKEN, so two workers recycled into the same slot get different tokens ✓ falls back to TEST_TOKEN when UNIQUE_TEST_TOKEN is absent @@ -2372,4 +2388,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, 37 skipped, 1689 passed (3737 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1703 passed (3758 assertions) \ No newline at end of file diff --git a/tests/Features/Tia/WatchFallbacks.php b/tests/Features/Tia/WatchFallbacks.php new file mode 100644 index 00000000..aba4645e --- /dev/null +++ b/tests/Features/Tia/WatchFallbacks.php @@ -0,0 +1,65 @@ +write('tests/Pest.php', file_get_contents($project->path('tests/Pest.php')). + "\npest()->tia()->withoutDefaultWatchPatterns(['.env.testing']);\n"); + $project->seed('master'); + $project->write('.env.testing', 'APP_ENV=testing'); + + $result = $project->pest('--tia', '--filtered', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->output)->toContain('No affected tests found'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); + +test('retains custom mappings and PHP edges in sequential and parallel runs', function (array $arguments): void { + $project = Project::make('master'); + $project->write('tests/Pest.php', file_get_contents($project->path('tests/Pest.php')). + "\npest()->tia()->withoutDefaultWatchPatterns()->watch(['.env.testing' => 'tests/Unit/GreeterTest.php']);\n"); + $project->seed('master'); + $project->write('.env.testing', 'APP_ENV=testing'); + + $result = $project->pest('--tia', '--filtered', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->affected())->toBe(2, $result->describe()); + + $project->write('app/Calculator.php', str_replace( + 'return $a + $b;', + 'return ($a + $b);', + file_get_contents($project->path('app/Calculator.php')), + )); + + $result = $project->pest('--tia', '--filtered', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()); + + if (str_contains($result->output, 'no coverage driver is available')) { + expect($result->output)->toContain('Running the full suite to avoid using a stale dependency graph.') + ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); + + return; + } + + expect($result->affected())->toBe(4, $result->describe()); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); + +test('still runs every test without a baseline when defaults are disabled', function (array $arguments): void { + $project = Project::make('master'); + $project->write('tests/Pest.php', file_get_contents($project->path('tests/Pest.php')). + "\npest()->tia()->withoutDefaultWatchPatterns();\n"); + + $result = $project->pest('--tia', '--filtered', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); diff --git a/tests/Unit/Plugins/Tia/WatchPatterns.php b/tests/Unit/Plugins/Tia/WatchPatterns.php new file mode 100644 index 00000000..77fca052 --- /dev/null +++ b/tests/Unit/Plugins/Tia/WatchPatterns.php @@ -0,0 +1,183 @@ +watchPatterns = new WatchPatterns; + $defaults = []; + + foreach ([new Php, new Inertia, new Livewire] as $provider) { + foreach ($provider->defaults(getcwd(), 'tests') as $pattern => $directories) { + $defaults[$pattern] = array_values(array_unique([ + ...($defaults[$pattern] ?? []), + ...$directories, + ])); + } + } + + new ReflectionProperty(WatchPatterns::class, 'defaultPatterns')->setValue($this->watchPatterns, $defaults); + Container::getInstance()->add(WatchPatterns::class, $this->watchPatterns); +}); + +afterEach(function (): void { + Project::destroyAll(); + Container::getInstance()->add(WatchPatterns::class, new WatchPatterns); +}); + +it('keeps the built-in fallbacks enabled by default', function (): void { + expect($this->watchPatterns->matchedDirectories(getcwd(), [ + 'resources/js/Pages/Enroll.vue', + '.env.testing', + ]))->toBe(['tests']); +}); + +it('excludes every overlapping default for a matching changed path', function (): void { + (new Configuration)->withoutDefaultWatchPatterns(['resources/js/**']); + + expect($this->watchPatterns->matchedDirectories(getcwd(), [ + 'resources/js/Pages/Enroll.vue', + 'resources/js/app.js', + ]))->toBeEmpty() + ->and($this->watchPatterns->matchedDirectories(getcwd(), ['.env.testing']))->toBe(['tests']); +}); + +it('retains custom watches when their matching defaults are excluded', function (): void { + $configuration = new Configuration; + + expect($configuration->withoutDefaultWatchPatterns(['resources/js/**']))->toBe($configuration); + + $configuration->watch(['resources/js/Pages/Enroll.vue' => 'tests/Feature/Enrollment']); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['resources/js/Pages/Enroll.vue'])) + ->toBe(['tests/Feature/Enrollment']); +}); + +it('keeps watch additive when defaults are enabled', function (): void { + (new Configuration)->watch(['resources/js/** !*.php' => 'tests/Browser']); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['resources/js/app.js'])) + ->toEqualCanonicalizing(['tests', 'tests/Browser']); +}); + +it('disables all defaults without disabling custom watches', function (): void { + (new Configuration) + ->watch(['resources/js/**' => 'tests/Browser']) + ->withoutDefaultWatchPatterns(); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['.env.testing']))->toBeEmpty() + ->and($this->watchPatterns->matchedDirectories(getcwd(), ['resources/js/app.js'])) + ->toBe(['tests/Browser']); +}); + +it('retains exclusions when defaults are loaded after configuration', function (): void { + (new Configuration)->withoutDefaultWatchPatterns(); + $this->watchPatterns->useDefaults(getcwd()); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['.env.testing']))->toBeEmpty(); +}); + +it('accumulates exclusions from repeated calls', function (): void { + (new Configuration) + ->withoutDefaultWatchPatterns(['resources/js/**']) + ->withoutDefaultWatchPatterns(['.env.testing']); + + expect($this->watchPatterns->matchedDirectories(getcwd(), [ + 'resources/js/app.js', + '.env.testing', + ]))->toBeEmpty() + ->and($this->watchPatterns->matchedDirectories(getcwd(), ['docker-compose.yml']))->toBe(['tests']); +}); + +it('does not disable defaults for an empty exclusion list', function (): void { + (new Configuration)->withoutDefaultWatchPatterns([]); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['.env.testing']))->toBe(['tests']); +}); + +it('supports exclusions within a changed-path pattern', function (): void { + (new Configuration)->withoutDefaultWatchPatterns(['resources/js/** !resources/js/critical/**']); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['resources/js/Pages/Enroll.vue']))->toBeEmpty() + ->and($this->watchPatterns->matchedDirectories(getcwd(), ['resources/js/critical/auth.js']))->toBe(['tests']); +}); + +it('restores default watches and removes custom watches on reset', function (): void { + (new Configuration) + ->withoutDefaultWatchPatterns() + ->watch(['custom/**' => 'tests/Feature']); + + $this->watchPatterns->reset(); + $this->watchPatterns->useDefaults(getcwd()); + + expect($this->watchPatterns->matchedDirectories(getcwd(), ['.env.testing']))->toBe(['tests']) + ->and($this->watchPatterns->matchedDirectories(getcwd(), ['custom/file.txt']))->toBeEmpty(); +}); + +it('does not select unrelated tests for an excluded unknown Vue page', function (): void { + $project = Project::withoutGit(); + $project->write('resources/js/Pages/Enroll.vue', ''); + $graph = new Graph($project->path()); + $graph->replaceJsFileToComponents(['resources/js/Pages/Enroll.vue' => ['Enroll']]); + $graph->link('tests/Unit/CalculatorTest.php', 'app/Calculator.php'); + $graph->link('tests/Unit/GreeterTest.php', 'app/Greeter.php'); + + expect($graph->affected(['resources/js/Pages/Enroll.vue'])) + ->toBe(['tests/Unit/CalculatorTest.php', 'tests/Unit/GreeterTest.php']); + + (new Configuration)->withoutDefaultWatchPatterns(['resources/js/**']); + + expect($graph->affected(['resources/js/Pages/Enroll.vue']))->toBeEmpty(); +}); + +it('retains recorded Inertia dependencies when default watches are disabled', function (): void { + $project = Project::withoutGit(); + $project->write('resources/js/Pages/Enroll.vue', ''); + $graph = new Graph($project->path()); + $graph->link('tests/Unit/CalculatorTest.php', 'app/Calculator.php'); + $graph->link('tests/Unit/GreeterTest.php', 'app/Greeter.php'); + $graph->replaceTestInertiaComponents(['tests/Unit/CalculatorTest.php' => ['Enroll']]); + + (new Configuration)->withoutDefaultWatchPatterns(); + + expect($graph->affected(['resources/js/Pages/Enroll.vue']))->toBe(['tests/Unit/CalculatorTest.php']); +}); + +it('retains recorded migration tables when default watches are disabled', function (): void { + $project = Project::withoutGit(); + $project->write('database/migrations/create_orders.php', 'path()); + $graph->link('tests/Unit/CalculatorTest.php', 'app/Calculator.php'); + $graph->link('tests/Unit/GreeterTest.php', 'app/Greeter.php'); + $graph->replaceTestTables([ + 'tests/Unit/CalculatorTest.php' => ['orders'], + 'tests/Unit/GreeterTest.php' => ['users'], + ]); + + (new Configuration)->withoutDefaultWatchPatterns(); + + expect($graph->affected(['database/migrations/create_orders.php']))->toBe(['tests/Unit/CalculatorTest.php']); +}); + +it('uses custom mappings for an unknown Vue page with defaults disabled', function (): void { + $project = Project::withoutGit(); + $project->write('resources/js/Pages/Enroll.vue', ''); + $graph = new Graph($project->path()); + $graph->replaceJsFileToComponents(['resources/js/Pages/Enroll.vue' => ['Enroll']]); + $graph->link('tests/Unit/CalculatorTest.php', 'app/Calculator.php'); + $graph->link('tests/Unit/GreeterTest.php', 'app/Greeter.php'); + + (new Configuration) + ->withoutDefaultWatchPatterns(['resources/js/**']) + ->watch(['resources/js/Pages/Enroll.vue' => 'tests/Unit/CalculatorTest.php']); + + expect($graph->affected(['resources/js/Pages/Enroll.vue']))->toBe(['tests/Unit/CalculatorTest.php']); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 21436de7..0eeb25ca 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, 29 skipped, 1660 passed (3659 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 29 skipped, 1674 passed (3680 assertions)'; expect($output) ->toContain("Tests: {$expected}")