fix(tia): do not stamp a commit that lands during the run as the baseline (#1925)

Co-authored-by: nunomaduro <enunomaduro@gmail.com>
This commit is contained in:
cyppe
2026-10-31 15:07:10 +01:00
committed by GitHub
parent 159514bace
commit 7cff286ac8
2 changed files with 96 additions and 0 deletions
+38
View File
@@ -178,6 +178,8 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
/** @var array{structural: array<string, mixed>, environmental: array<string, mixed>}|null */
private ?array $startFingerprint = null;
private ?string $startSha = null;
private bool $piggybackCoverage = false;
private bool $recordingActive = false;
@@ -238,6 +240,17 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
return ! Fingerprint::structuralMatches($this->startFingerprint, $current);
}
private function headMovedDuringRun(?string $currentSha): bool
{
return $this->startFingerprint !== null && $this->startSha !== $currentSha;
}
private function renderHeadMovedDuringRun(string $consequence): void
{
$this->renderBadge('WARN', 'HEAD moved during the run — '.$consequence.'.');
$this->renderChild('Re-run --tia to test the new HEAD.');
}
private function loadGraph(string $projectRoot): ?Graph
{
$json = $this->state->read(self::KEY_GRAPH);
@@ -625,6 +638,14 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
return;
}
if ($this->headMovedDuringRun($currentSha)) {
$this->renderHeadMovedDuringRun('discarding recorded edges');
$recorder->reset();
$this->coverageCollector->reset();
return;
}
$graph = $this->loadGraph($projectRoot) ?? new Graph($projectRoot);
$graph->setFingerprint($currentFingerprint);
$graph->setRecordedAtSha($this->branch, $currentSha);
@@ -713,6 +734,16 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
return $exitCode;
}
if ($this->headMovedDuringRun($currentSha)) {
$this->renderHeadMovedDuringRun('discarding recorded edges');
foreach ($partialKeys as $key) {
$this->state->delete($key);
}
return $exitCode;
}
$graph = $this->loadGraph($projectRoot) ?? new Graph($projectRoot);
$graph->setFingerprint($currentFingerprint);
$graph->setRecordedAtSha($this->branch, $currentSha);
@@ -842,6 +873,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
$fingerprint = Fingerprint::compute($projectRoot);
$this->startFingerprint = $fingerprint;
$this->startSha = new ChangedFiles($projectRoot)->currentSha();
if ($forceRebuild && $this->canRebuildGraph()) {
$this->purgeState();
@@ -1674,6 +1706,12 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
$changedFiles = new ChangedFiles($projectRoot);
$currentSha = $changedFiles->currentSha();
if ($this->headMovedDuringRun($currentSha)) {
$this->renderHeadMovedDuringRun('keeping the baseline from before the run');
return;
}
if ($currentSha !== null) {
$graph->setRecordedAtSha($this->branch, $currentSha);
}
+58
View File
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('a commit that lands during a run is not stamped as the recorded baseline', function (array $arguments): void {
$project = Project::make('master');
$project->seed('master');
$project->write('tests/Unit/GreeterTest.php', <<<'PHP'
<?php
declare(strict_types=1);
use Fixture\App\Greeter;
test('greets a person', function (): void {
$root = dirname(__DIR__, 2);
$marker = $root.'/commit-during-run';
if (is_file($marker)) {
unlink($marker);
$calculator = $root.'/app/Calculator.php';
file_put_contents($calculator, str_replace('return $a + $b;', 'return $a + $b + 1000;', (string) file_get_contents($calculator)));
exec('git -C '.escapeshellarg($root).' commit --quiet --all --message "break add() during the run"', result_code: $exitCode);
expect($exitCode)->toBe(0);
}
expect((new Greeter)->greet('Nuno'))->toBe('Hello, Nuno!');
});
test('greets the world', function (): void {
expect((new Greeter)->greet('world'))->toBe('Hello, world!');
});
PHP);
$project->git()->commit('a greeter test that commits while the run is in flight');
$project->write('commit-during-run', '');
$during = $project->pest('--tia', ...$arguments);
expect($during->exitCode)->toBe(0, $during->describe())
->and($project->path('commit-during-run'))->not->toBeFile()
->and($during->output)->toContain('HEAD moved during the run');
$after = $project->pest('--tia', ...$arguments);
expect($after->exitCode)->toBe(1, $after->describe())
->and($after->tally())->toContain('failed')
->and($after->replayed())->toBeLessThan(Project::TOTAL_TESTS, $after->describe());
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();