This commit is contained in:
nuno maduro
2026-08-07 01:11:53 +01:00
parent 7bfb2a6185
commit cc7acfe485
16 changed files with 900 additions and 8 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use NunoMaduro\Collision\Contracts\RenderlessEditor;
use NunoMaduro\Collision\Contracts\RenderlessTrace;
use Pest\Contracts\Panicable;
use RuntimeException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class TiaRequiresCommit extends RuntimeException implements ExceptionInterface, Panicable, RenderlessEditor, RenderlessTrace
{
public function __construct()
{
parent::__construct(
'Tia mode requires a repository with at least one commit, so the baseline it records can be anchored to a revision.',
);
}
public function render(OutputInterface $output): void
{
$output->writeln([
'',
' <fg=white;options=bold;bg=red> ERROR </> Tia mode requires at least one commit.',
'',
' A baseline is anchored to the revision it was recorded at, and this repository',
' has none yet, so there is nothing to record against and nothing to compare a',
' later run to.',
'',
' Commit once, then run again:',
'',
' <fg=yellow>git add . && git commit -m "Initial commit"</>',
'',
' Runs without <fg=yellow>--tia</> are unaffected.',
'',
]);
}
public function exitCode(): int
{
return 1;
}
}
+54 -4
View File
@@ -12,6 +12,7 @@ use Pest\Contracts\Plugins\Terminable;
use Pest\Exceptions\InvalidOption;
use Pest\Exceptions\MissingDependency;
use Pest\Exceptions\NoAffectedTestsFound;
use Pest\Exceptions\TiaRequiresCommit;
use Pest\Exceptions\TiaRequiresDefaultBranch;
use Pest\Exceptions\TiaRequiresRemote;
use Pest\Exceptions\TiaRequiresRepositoryRoot;
@@ -210,6 +211,8 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
private bool $detachedHead = false;
private bool $graphUnreachable = false;
/** @var array<int, string> */
private array $originalArguments = [];
@@ -692,7 +695,17 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
return $exitCode;
}
if ($this->replayRan) {
// Re-anchor the baseline. Reaching here means the run was complete —
// nothing suppressed, narrowed or truncated it — so its results are the
// truth at HEAD and the recorded revision may say so.
//
// That matters most when the recorded commit had become unreachable (a
// rebase, a force-push) and no coverage driver was available to rebuild:
// without this the stale revision survives, and every later run warns
// and re-runs the whole suite, for good. Stale edges are no objection —
// a complete run just re-recorded every result, and later changes are
// compared against the revision written here.
if ($this->replayRan || $this->graphUnreachable) {
$this->bumpRecordedSha();
}
@@ -836,7 +849,20 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
Panic::with(new TiaRequiresRepositoryRoot($subdirectoryPrefix));
}
$this->resolveBranch($projectRoot);
try {
$this->resolveBranch($projectRoot);
} catch (MissingDependency $missingGit) {
// Every git call TIA makes fails on `HEAD` in a repository that has
// no commits yet, which reads as "git is missing" when git is right
// there. Say what is actually wrong instead.
$repository = new ChangedFiles($projectRoot);
if ($repository->isRepository() && ! $repository->hasCommits()) {
Panic::with(new TiaRequiresCommit);
}
throw $missingGit;
}
if (! $this->fallbackBranchResolved) {
Panic::with(new ChangedFiles($projectRoot)->hasRemote()
@@ -865,6 +891,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
&& $changedFiles->since($branchSha) === null) {
$this->renderBadge('WARN', 'Recorded commit is no longer reachable — graph will be rebuilt.');
$graph = null;
$this->graphUnreachable = true;
}
}
@@ -886,8 +913,23 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
$this->state->write(self::KEY_COVERAGE_MARKER, '');
}
// An active coverage report owns the driver, so edges have to be
// piggybacked off its session — and that session is scoped to
// phpunit.xml's <source>, not to the whole project. Refreshing an
// existing graph that way is safe (`replaceEdges()` keeps what it
// already has), but *founding* one on it is not: every source file
// outside the coverage scope would be missing from the graph for good,
// and a change to one of them would select nothing and replay a pass.
if (! $graph instanceof Graph && $this->piggybackCoverage) {
$this->emitCoverageScopedRecordSkipped();
return $arguments;
}
// Past the guard above, a coverage-owned run always has a graph to
// refresh — a run without one never gets here.
if ($coverageCacheOwned && ! $this->state->exists(self::KEY_COVERAGE_CACHE)) {
if ($graph instanceof Graph && $this->driftLabel === null) {
if ($this->driftLabel === null) {
$this->freshGraphReason = 'recording a coverage baseline';
}
@@ -1042,7 +1084,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
return $arguments;
}
$affectedFromChanges = $changed === [] ? [] : $graph->affected($changed);
$affectedFromChanges = $changed === [] ? [] : $graph->testFilesOnDisk($graph->affected($changed));
$rerunFromCache = [];
if ($this->filteredMode && $graph->hasUnlocatedTestsToRerun($this->branch)) {
@@ -1295,6 +1337,14 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
$this->renderChild('Running in TIA mode, however TIA is skipped as it needs ext-pcov or Xdebug.');
}
private function emitCoverageScopedRecordSkipped(): void
{
$this->output->writeln('');
$this->renderChild('Running in TIA mode, however TIA is skipped as an active coverage report narrows the edges it could record.');
$this->renderChild('Record the baseline with a plain --tia run first; coverage runs then reuse it.');
}
/**
* @param array<string, array<int, string>> $perTestFiles
* @param array<string, array<int, string>> $perTestTables
+30 -1
View File
@@ -298,6 +298,31 @@ final readonly class ChangedFiles
return $this->gitOutput(['git', 'remote']) !== null;
}
public function isRepository(): bool
{
$process = new Process(['git', 'rev-parse', '--git-dir'], $this->projectRoot);
$process->setTimeout(5.0);
$process->run();
return $process->getExitCode() === 0;
}
/**
* Whether this repository has a revision to anchor a baseline to.
*
* A freshly initialised repository has none, and every other git call TIA
* makes — {@see self::currentBranch()}, {@see self::currentSha()} — fails on
* `HEAD` there and reports git as missing, which it is not.
*/
public function hasCommits(): bool
{
$process = new Process(['git', 'rev-parse', '--verify', '--quiet', 'HEAD'], $this->projectRoot);
$process->setTimeout(5.0);
$process->run();
return $process->getExitCode() === 0;
}
/**
* @param array<int, string> $command
*/
@@ -332,8 +357,12 @@ final readonly class ChangedFiles
*/
private function diffSinceSha(string $sha): array
{
// `--no-renames` matters: with rename detection on, git reports only the
// destination of a moved file, so the path the graph has edges for — the
// one that is gone — never reaches selection, and every test that
// depended on it replays its recorded pass.
$process = new Process(
['git', 'diff', '--name-only', $sha.'..HEAD'],
['git', 'diff', '--name-only', '--no-renames', $sha.'..HEAD'],
$this->projectRoot,
);
$process->run();
+26
View File
@@ -119,6 +119,32 @@ final class Graph
return array_keys($affectedSet);
}
/**
* Keep only the test files this checkout actually has.
*
* A stale edge key — a test file a fetched baseline knew, or one another
* branch deleted — cannot be run by anyone, and selecting it strands
* `--filtered` on a run that matches nothing and reports success on a
* change no test looked at. {@see self::testFilesToRerun()} has always
* dropped these; the change-driven half of selection must agree.
*
* @param array<int, string> $testFiles Project-relative paths.
* @return list<string>
*/
public function testFilesOnDisk(array $testFiles): array
{
$root = rtrim($this->projectRoot, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
$onDisk = [];
foreach ($testFiles as $testFile) {
if (is_file($root.$testFile)) {
$onDisk[] = $testFile;
}
}
return $onDisk;
}
/**
* @param array<int, string> $changedFiles
* @return array{0: list<string>, 1: list<string>}
+12 -1
View File
@@ -359,8 +359,19 @@ final class Recorder
continue;
}
// A file whose *only* executed line is its last one was included,
// not used — the trailing line of an include is all that ran.
//
// That reading only holds for a driver that reports unexecuted
// lines too: pcov returns every executable line (`-1` for the ones
// that did not run), so "the single covered line is the highest
// line reported" means something. Xdebug reports executed lines
// only, where it is true of *any* file that ran a single line —
// which is most of them, and dropping those loses the edge.
$lineKeys = array_keys($lines);
if ($lineKeys !== [] && count($covered) === 1 && $covered[0] === max($lineKeys)) {
$reportsUnexecutedLines = count($covered) < count($lines);
if ($reportsUnexecutedLines && $lineKeys !== [] && count($covered) === 1 && $covered[0] === max($lineKeys)) {
continue;
}