diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 742200c0..058b7f64 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -178,6 +178,8 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument /** @var array{structural: array, environmental: array}|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); } diff --git a/tests/Features/Tia/HeadMovedDuringRun.php b/tests/Features/Tia/HeadMovedDuringRun.php new file mode 100644 index 00000000..1b183432 --- /dev/null +++ b/tests/Features/Tia/HeadMovedDuringRun.php @@ -0,0 +1,58 @@ +seed('master'); + + $project->write('tests/Unit/GreeterTest.php', <<<'PHP' + 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();