mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
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 <mrpunyapal@gmail.com>
This commit is contained in:
@@ -102,4 +102,17 @@ final class Configuration
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string>|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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ final class Recorder
|
||||
$out = [];
|
||||
|
||||
foreach ($data as $file => $lines) {
|
||||
if (! is_array($lines)) {
|
||||
if (! is_array($lines) || $lines === []) {
|
||||
continue;
|
||||
}
|
||||
$covered = [];
|
||||
|
||||
@@ -31,6 +31,18 @@ final class WatchPatterns
|
||||
*/
|
||||
private array $patterns = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
private array $defaultPatterns = [];
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private array $excludedDefaultPatterns = [];
|
||||
|
||||
private bool $defaultsEnabled = true;
|
||||
|
||||
/**
|
||||
* @var array<string, array{include: string, excludes: array<int, string>, 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<string>|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<int, string> $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<string, array<int, string>> $patterns
|
||||
* @return list<string>
|
||||
*/
|
||||
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<int, string> $directories Project-relative dirs/files.
|
||||
* @param array<int, string> $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;
|
||||
|
||||
@@ -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)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1703 passed (3758 assertions)
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
test('excludes a default fallback 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(['.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();
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Tia\Configuration;
|
||||
use Pest\Plugins\Tia\Graph;
|
||||
use Pest\Plugins\Tia\WatchDefaults\Inertia;
|
||||
use Pest\Plugins\Tia\WatchDefaults\Livewire;
|
||||
use Pest\Plugins\Tia\WatchDefaults\Php;
|
||||
use Pest\Plugins\Tia\WatchPatterns;
|
||||
use Pest\Support\Container;
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->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', '<template>Enroll</template>');
|
||||
$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', '<template>Enroll</template>');
|
||||
$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', '<?php Schema::create("orders", function () {});');
|
||||
$graph = new Graph($project->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', '<template>Enroll</template>');
|
||||
$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']);
|
||||
});
|
||||
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user