Files
pest/tests/Features/Tia/ScopedRuns.php
T
Jhon Farias 59c1cb78f8 feat(tia): replay unaffected tests in a partial run (#2092)
* feat(tia): replay unaffected tests in a partial run

* fix(tia): run a partial selection directly where a complete run is refused

* fix(tia): leave a graph from another environment alone in a partial run
2026-12-04 14:23:50 +05:30

170 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
use Pest\Plugins\Tia;
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
function tiaEditGreeterTest(Project $project): void
{
$project->write('tests/Unit/GreeterTest.php', <<<'PHP'
<?php
declare(strict_types=1);
use Fixture\App\Greeter;
test('greets a person', function (): void {
expect((new Greeter)->greet('Nuno'))->toBe('Hello, Nuno!');
})->group('greeter');
test('greets the world', function (): void {
expect((new Greeter)->greet('world'))->toBe('Hello, world!');
})->group('greeter');
test('greets again', function (): void {
expect((new Greeter)->greet('again'))->toBe('Hello, again!');
})->group('greeter');
PHP);
}
test('a partial run replays the unaffected tests of its selection', function (array $selection, int $affected, int $replayed, array $mode): void {
$project = Project::make('master');
$project->seed('master');
tiaEditGreeterTest($project);
$result = $project->pest('--tia', ...$selection, ...$mode);
$delta = $project->delta();
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->output)->not->toContain('TIA does not apply to partial runs')
->and($result->tally())->toContain(($affected + $replayed).' passed')
->and($result->affected())->toBe($affected, $result->describe())
->and($result->replayed())->toBe($replayed, $result->describe())
->and($delta->structureMoved())->toBeFalse($delta->summary())
->and($delta->shaMoved())->toBeFalse($delta->summary())
->and($delta->treeMoved())->toBeFalse($delta->summary())
->and($delta->removed())->toBe(0, $delta->summary());
})->with([
'an explicit path' => [['tests/Unit'], 3, 2],
'--filter' => [['--filter=greets a person|adds two numbers'], 1, 1],
'--group' => [['--group=greeter'], 3, 0],
'--exclude-group' => [['--exclude-group=greeter'], 0, 4],
'--testsuite' => [['--testsuite=default'], 3, 4],
'--dirty' => [['--dirty'], 3, 0],
])->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
test('a partial run re-runs a cached failure in its selection', function (): void {
$project = Project::make('master');
$project->seed('master', failing: ['adds two numbers']);
$result = $project->pest('--tia', 'tests/Unit');
$delta = $project->delta();
expect($result->tally())->toContain('4 passed')
->and($result->replayed())->toBe(3, $result->describe())
->and($delta->writtenCount())->toBe(1, $delta->summary())
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
})->skipOnWindows();
test('a partial run leaves the affected tests to the next complete run', function (): void {
$project = Project::make('master');
$project->seed('master');
tiaEditGreeterTest($project);
$project->pest('--tia', '--exclude-group=greeter');
$result = $project->pest('--tia');
expect($result->affected())->toBe(3, $result->describe())
->and($result->replayed())->toBe(4, $result->describe());
})->skipOnWindows();
test('a partial run without a graph runs its selection and records nothing', function (): void {
$project = Project::make('master');
$result = $project->pest('--tia', 'tests/Unit');
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->output)->toContain('a partial run cannot record the dependency graph')
->and($result->tally())->toContain('4 passed')
->and($result->replayed())->toBe(0, $result->describe())
->and($project->graphExists())->toBeFalse();
})->skipOnWindows();
test('a partial run with a coverage report runs its selection directly', function (): void {
$project = Project::make('master');
$project->seed('master');
$result = $project->pest('--tia', 'tests/Unit', '--coverage-text');
$delta = $project->delta();
expect($result->output)->toContain('TIA does not apply to partial runs')
->and($result->replayed())->toBe(0, $result->describe())
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
})->skipOnWindows();
test('a partial run in a repository a complete run refuses runs its selection directly', function (Closure $prepare): void {
$project = $prepare();
$result = $project->pest('--tia', 'tests/Unit');
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->tally())->toContain('4 passed')
->and($result->output)->toContain('TIA does not apply to partial runs')
->and($project->graphExists())->toBeFalse();
})->with([
'no remote' => [function (): Project {
$project = Project::make('master');
$project->git()->removeOrigin();
$project->git()->switchTo('feature-x', new: true);
return $project;
}],
'no default branch' => [function (): Project {
$project = Project::make('master');
$project->git()->unsetOriginHead();
$project->git()->config('init.defaultBranch', 'main');
$project->git()->switchTo('feature-x', new: true);
return $project;
}],
'no commits' => [function (): Project {
$project = Project::withoutGit();
$project->git()->run(['init', '--quiet']);
$project->git()->run(['checkout', '--quiet', '-b', 'master']);
$project->git()->addOrigin();
return $project;
}],
'no git' => [fn (): Project => Project::withoutGit()],
])->skipOnWindows();
test('a partial run leaves a graph recorded in a different environment alone', function (string $section, string $key, string $value): void {
$project = Project::make('master');
$project->seed('master');
$project->mutateGraph(function (array $graph) use ($section, $key, $value): array {
$graph['fingerprint'][$section][$key] = $value;
return $graph;
});
file_put_contents($project->graphDir().'/'.Tia::KEY_COVERAGE_CACHE, 'cached coverage');
$project->snapshot();
$result = $project->pest('--tia', '--filter=adds two numbers');
$delta = $project->delta();
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->output)->toContain('The graph was recorded in a different environment ('.$key.')')
->and($result->tally())->toContain('1 passed')
->and($result->replayed())->toBe(0, $result->describe())
->and($delta->isHardSuppressed())->toBeTrue($delta->summary())
->and($project->graphDir().'/'.Tia::KEY_COVERAGE_CACHE)->toBeFile();
})->with([
'another PHP minor version' => ['environmental', 'php_minor', '7.4'],
'another lockfile' => ['structural', 'composer_lock', 'a-lockfile-this-project-never-had'],
])->skipOnWindows();