diff --git a/src/Exceptions/TiaBaselineCommitMissing.php b/src/Exceptions/TiaBaselineCommitMissing.php new file mode 100644 index 00000000..b640a74d --- /dev/null +++ b/src/Exceptions/TiaBaselineCommitMissing.php @@ -0,0 +1,50 @@ +writeln([ + '', + sprintf(' ERROR The Tia baseline commit %s cannot be reached from this branch.', substr($this->sha, 0, 12)), + '', + ' Either the commit is not in this clone, or this branch does not contain it.', + ' Without it the files changed since the baseline cannot be listed, and every', + ' test would be replayed as unchanged.', + '', + ' Fetch it, or merge the branch it was recorded on, then run again:', + '', + ' git fetch', + '', + ' Or use --fresh to record locally.', + '', + ]); + } + + public function exitCode(): int + { + return 1; + } +} diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 058b7f64..7c7b2234 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -12,6 +12,7 @@ use Pest\Contracts\Plugins\Terminable; use Pest\Exceptions\InvalidOption; use Pest\Exceptions\MissingDependency; use Pest\Exceptions\NoAffectedTestsFound; +use Pest\Exceptions\TiaBaselineCommitMissing; use Pest\Exceptions\TiaRequiresCommit; use Pest\Exceptions\TiaRequiresDefaultBranch; use Pest\Exceptions\TiaRequiresRemote; @@ -1082,7 +1083,11 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument $changedFiles = new ChangedFiles($projectRoot); $branchSha = $graph->recordedAtSha($this->branch); - $changed = $changedFiles->since($branchSha) ?? []; + $changed = $changedFiles->since($branchSha); + + if ($changed === null) { + Panic::with(new TiaBaselineCommitMissing((string) $branchSha)); + } $changed = $changedFiles->filterUnchangedSinceLastRun( $changed, diff --git a/tests/Features/Tia/RemoteBaseline.php b/tests/Features/Tia/RemoteBaseline.php index d47fb0a1..b0c543be 100644 --- a/tests/Features/Tia/RemoteBaseline.php +++ b/tests/Features/Tia/RemoteBaseline.php @@ -64,6 +64,22 @@ test('a fetched baseline recorded against another tree is not used', function () ->and($result->replayed())->toBe(0, $result->describe()); })->skipOnWindows(); +test('a fetched baseline recorded at a commit this clone lacks fails loudly', function (): void { + [$project, $environment] = tiaPublishedBaseline('ok', function (array $graph): array { + foreach (array_keys($graph['baselines']) as $branch) { + $graph['baselines'][$branch]['sha'] = str_repeat('1', 40); + } + + return $graph; + }); + + $result = $project->pestWithEnvironment($project->path(), $environment, '--tia', '--baselined'); + + expect($result->exitCode)->toBe(1, $result->describe()) + ->and($result->output)->toContain('cannot be reached from this branch') + ->and($result->replayed())->toBe(0, $result->describe()); +})->skipOnWindows(); + test('an artifact without a graph in it fails loudly', function (): void { [$project, $environment] = tiaPublishedBaseline('missing-asset');