mirror of
https://github.com/pestphp/pest.git
synced 2026-04-22 06:57:28 +02:00
feat(tia): adds poc
This commit is contained in:
@ -10,11 +10,11 @@ use Pest\Contracts\Plugins\Terminable;
|
||||
use Pest\Exceptions\NoDirtyTestsFound;
|
||||
use Pest\Panic;
|
||||
use Pest\Support\Container;
|
||||
use Pest\Support\Tia\ChangedFiles;
|
||||
use Pest\Support\Tia\Fingerprint;
|
||||
use Pest\Support\Tia\Graph;
|
||||
use Pest\Support\Tia\Recorder;
|
||||
use Pest\TestCaseFilters\TiaTestCaseFilter;
|
||||
use Pest\Plugins\Tia\ChangedFiles;
|
||||
use Pest\Plugins\Tia\Fingerprint;
|
||||
use Pest\Plugins\Tia\Graph;
|
||||
use Pest\Plugins\Tia\Recorder;
|
||||
use Pest\Plugins\Tia\State;
|
||||
use Pest\TestSuite;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Throwable;
|
||||
@ -363,11 +363,73 @@ final class Tia implements AddsOutput, HandlesArguments, Terminable
|
||||
}
|
||||
}
|
||||
|
||||
TestSuite::getInstance()->tests->addTestCaseFilter(
|
||||
new TiaTestCaseFilter($projectRoot, $graph, $affectedSet),
|
||||
State::instance()->activate(
|
||||
$projectRoot,
|
||||
$graph,
|
||||
$affectedSet,
|
||||
$this->loadPreviousDefects($projectRoot),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads PHPUnit's own result cache and returns the test ids that failed
|
||||
* or errored in the previous run. These are excluded from replay so the
|
||||
* user sees current state rather than a stale pass.
|
||||
*
|
||||
* @return array<string, true>
|
||||
*/
|
||||
private function loadPreviousDefects(string $projectRoot): array
|
||||
{
|
||||
// PHPUnit writes the cache under either `<projectRoot>/.phpunit.result.cache`
|
||||
// (legacy) or `<cacheDirectory>/test-results`. Pest's Cache plugin
|
||||
// additionally defaults `cacheDirectory` to
|
||||
// `vendor/pestphp/pest/.temp` when the user hasn't configured one.
|
||||
// We probe the common locations; if we miss the file, replay falls
|
||||
// back to its safe default (still runs the test).
|
||||
$candidates = [
|
||||
$projectRoot.'/.phpunit.result.cache',
|
||||
$projectRoot.'/.phpunit.cache/test-results',
|
||||
$projectRoot.'/.pest/cache/test-results',
|
||||
$projectRoot.'/vendor/pestphp/pest/.temp/test-results',
|
||||
];
|
||||
|
||||
$path = null;
|
||||
|
||||
foreach ($candidates as $candidate) {
|
||||
if (is_file($candidate)) {
|
||||
$path = $candidate;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($path === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($path);
|
||||
|
||||
if ($raw === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
|
||||
if (! is_array($data) || ! isset($data['defects']) || ! is_array($data['defects'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$out = [];
|
||||
|
||||
foreach ($data['defects'] as $id => $_status) {
|
||||
if (is_string($id)) {
|
||||
$out[$id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $arguments
|
||||
* @return array<int, string>
|
||||
@ -386,28 +448,31 @@ final class Tia implements AddsOutput, HandlesArguments, Terminable
|
||||
|
||||
$changed = $changedFiles->since($graph->recordedAtSha()) ?? [];
|
||||
|
||||
if ($changed === []) {
|
||||
$this->output->writeln(' <fg=green>TIA</> no changes detected.');
|
||||
|
||||
Panic::with(new NoDirtyTestsFound);
|
||||
}
|
||||
|
||||
$affected = $graph->affected($changed);
|
||||
// Even with zero changes, we still run through the suite so the user
|
||||
// sees the previous results reflected (cached passes replay as
|
||||
// instant passes; failures re-run to surface current state). This
|
||||
// matches the UX of test runners like NCrunch where every run
|
||||
// produces a full report regardless of what actually executed.
|
||||
$affected = $changed === [] ? [] : $graph->affected($changed);
|
||||
|
||||
$testSuite = TestSuite::getInstance();
|
||||
|
||||
if (! Parallel::isEnabled()) {
|
||||
// Series mode: install the TestCaseFilter so Pest/PHPUnit skips
|
||||
// unaffected tests during discovery. Keep filter semantics
|
||||
// identical to parallel mode: unknown/new tests always pass.
|
||||
// Series mode: activate replay state. Tests still appear in the
|
||||
// run (correct counts, coverage aggregation, event timeline);
|
||||
// unaffected ones short-circuit inside `Testable::__runTest`
|
||||
// and replay their previous passing status.
|
||||
$affectedSet = array_fill_keys($affected, true);
|
||||
|
||||
$testSuite->tests->addTestCaseFilter(
|
||||
new TiaTestCaseFilter($projectRoot, $graph, $affectedSet),
|
||||
State::instance()->activate(
|
||||
$projectRoot,
|
||||
$graph,
|
||||
$affectedSet,
|
||||
$this->loadPreviousDefects($projectRoot),
|
||||
);
|
||||
|
||||
$this->output->writeln(sprintf(
|
||||
' <fg=green>TIA</> %d changed file(s) → %d known test file(s) + any new/unknown tests.',
|
||||
' <fg=green>TIA</> %d changed file(s) → %d affected, remaining tests replay cached result.',
|
||||
count($changed),
|
||||
count($affected),
|
||||
));
|
||||
@ -435,7 +500,7 @@ final class Tia implements AddsOutput, HandlesArguments, Terminable
|
||||
Parallel::setGlobal(self::REPLAYING_GLOBAL, '1');
|
||||
|
||||
$this->output->writeln(sprintf(
|
||||
' <fg=green>TIA</> %d changed file(s) → %d known test file(s) + any new/unknown tests (parallel).',
|
||||
' <fg=green>TIA</> %d changed file(s) → %d affected, remaining tests replay cached result (parallel).',
|
||||
count($changed),
|
||||
count($affected),
|
||||
));
|
||||
|
||||
219
src/Plugins/Tia/ChangedFiles.php
Normal file
219
src/Plugins/Tia/ChangedFiles.php
Normal file
@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* Detects files that changed between the last recorded TIA run and the
|
||||
* current working tree.
|
||||
*
|
||||
* Strategy:
|
||||
* 1. If we have a `recordedAtSha`, `git diff <sha>..HEAD` captures committed
|
||||
* changes on top of the recording point.
|
||||
* 2. `git status --short` captures unstaged + staged + untracked changes on
|
||||
* top of that.
|
||||
*
|
||||
* We return relative paths to the project root. Deletions are included so the
|
||||
* caller can decide whether to invalidate: a deleted source file may still
|
||||
* appear in the graph and should mark its dependents as affected.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class ChangedFiles
|
||||
{
|
||||
public function __construct(private string $projectRoot) {}
|
||||
|
||||
/**
|
||||
* @return array<int, string>|null `null` when git is unavailable, or when
|
||||
* the recorded SHA is no longer reachable
|
||||
* from HEAD (rebase / force-push) — in
|
||||
* that case the graph should be rebuilt.
|
||||
*/
|
||||
public function since(?string $sha): ?array
|
||||
{
|
||||
if (! $this->gitAvailable()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$files = [];
|
||||
|
||||
if ($sha !== null && $sha !== '') {
|
||||
if (! $this->shaIsReachable($sha)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$files = array_merge($files, $this->diffSinceSha($sha));
|
||||
}
|
||||
|
||||
$files = array_merge($files, $this->workingTreeChanges());
|
||||
|
||||
// Normalise + dedupe, filtering out paths that can never belong to the
|
||||
// graph: vendor (caught by the fingerprint instead), cache dirs, and
|
||||
// anything starting with a dot we don't care about.
|
||||
$unique = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file === '') {
|
||||
continue;
|
||||
}
|
||||
if ($this->shouldIgnore($file)) {
|
||||
continue;
|
||||
}
|
||||
$unique[$file] = true;
|
||||
}
|
||||
|
||||
return array_keys($unique);
|
||||
}
|
||||
|
||||
private function shouldIgnore(string $path): bool
|
||||
{
|
||||
static $prefixes = [
|
||||
'.pest/',
|
||||
'.phpunit.cache/',
|
||||
'.phpunit.result.cache',
|
||||
'vendor/',
|
||||
'node_modules/',
|
||||
];
|
||||
|
||||
foreach ($prefixes as $prefix) {
|
||||
if (str_starts_with($path, (string) $prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function gitAvailable(): bool
|
||||
{
|
||||
$process = new Process(['git', 'rev-parse', '--git-dir'], $this->projectRoot);
|
||||
$process->run();
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
private function shaIsReachable(string $sha): bool
|
||||
{
|
||||
$process = new Process(
|
||||
['git', 'merge-base', '--is-ancestor', $sha, 'HEAD'],
|
||||
$this->projectRoot,
|
||||
);
|
||||
$process->run();
|
||||
|
||||
// Exit 0 → ancestor; 1 → not ancestor; anything else → git error
|
||||
// (e.g. unknown commit after a rebase/gc). Treat non-zero as
|
||||
// "unreachable" and force a rebuild.
|
||||
return $process->getExitCode() === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function diffSinceSha(string $sha): array
|
||||
{
|
||||
$process = new Process(
|
||||
['git', 'diff', '--name-only', $sha.'..HEAD'],
|
||||
$this->projectRoot,
|
||||
);
|
||||
$process->run();
|
||||
|
||||
if (! $process->isSuccessful()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->splitLines($process->getOutput());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function workingTreeChanges(): array
|
||||
{
|
||||
// `-z` produces NUL-terminated records with no path quoting, so paths
|
||||
// that contain spaces, tabs, unicode or other special characters
|
||||
// are passed through verbatim. Without `-z`, git wraps such paths in
|
||||
// quotes with backslash escapes, which would corrupt our lookup keys.
|
||||
//
|
||||
// Record format: `XY <SP> <path> <NUL>` for most entries, and
|
||||
// `R <new> <NUL> <orig> <NUL>` for renames/copies (two NUL-separated
|
||||
// fields).
|
||||
$process = new Process(
|
||||
['git', 'status', '--porcelain', '-z', '--untracked-files=all'],
|
||||
$this->projectRoot,
|
||||
);
|
||||
$process->run();
|
||||
|
||||
if (! $process->isSuccessful()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$output = $process->getOutput();
|
||||
|
||||
if ($output === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$records = explode("\x00", rtrim($output, "\x00"));
|
||||
$files = [];
|
||||
$count = count($records);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$record = $records[$i];
|
||||
|
||||
if (strlen($record) < 4) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$status = substr($record, 0, 2);
|
||||
$path = substr($record, 3);
|
||||
|
||||
// Renames/copies emit two records: the new path first, then the
|
||||
// original. Consume both.
|
||||
if ($status[0] === 'R' || $status[0] === 'C') {
|
||||
$files[] = $path;
|
||||
|
||||
if (isset($records[$i + 1]) && $records[$i + 1] !== '') {
|
||||
$files[] = $records[$i + 1];
|
||||
$i++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$files[] = $path;
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
public function currentSha(): ?string
|
||||
{
|
||||
if (! $this->gitAvailable()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$process = new Process(['git', 'rev-parse', 'HEAD'], $this->projectRoot);
|
||||
$process->run();
|
||||
|
||||
if (! $process->isSuccessful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sha = trim($process->getOutput());
|
||||
|
||||
return $sha === '' ? null : $sha;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function splitLines(string $output): array
|
||||
{
|
||||
$lines = preg_split('/\R+/', trim($output), flags: PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
return $lines === false ? [] : $lines;
|
||||
}
|
||||
}
|
||||
95
src/Plugins/Tia/Fingerprint.php
Normal file
95
src/Plugins/Tia/Fingerprint.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
/**
|
||||
* Captures environmental inputs that, when changed, make the TIA graph stale.
|
||||
*
|
||||
* Any drift in PHP version, Composer lock, or Pest/PHPUnit config can change
|
||||
* what a test actually exercises, so the graph must be rebuilt in those cases.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class Fingerprint
|
||||
{
|
||||
// Bump this whenever the set of inputs or the hash algorithm changes, so
|
||||
// older graphs are invalidated automatically.
|
||||
private const int SCHEMA_VERSION = 2;
|
||||
|
||||
/**
|
||||
* @param non-empty-string $projectRoot
|
||||
*/
|
||||
public static function compute(string $projectRoot): array
|
||||
{
|
||||
return [
|
||||
'schema' => self::SCHEMA_VERSION,
|
||||
'php' => PHP_VERSION,
|
||||
'pest' => self::readPestVersion($projectRoot),
|
||||
'composer_lock' => self::hashIfExists($projectRoot.'/composer.lock'),
|
||||
'phpunit_xml' => self::hashIfExists($projectRoot.'/phpunit.xml'),
|
||||
'phpunit_xml_dist' => self::hashIfExists($projectRoot.'/phpunit.xml.dist'),
|
||||
'pest_php' => self::hashIfExists($projectRoot.'/tests/Pest.php'),
|
||||
// Pest's generated classes bake the code-generation logic in — if
|
||||
// TestCaseFactory changes (new attribute, different method
|
||||
// signature, etc.) every previously-recorded edge is stale.
|
||||
// Hashing the factory sources makes path-repo / dev-main installs
|
||||
// automatically rebuild their graphs when Pest itself is edited.
|
||||
'pest_factory' => self::hashIfExists(__DIR__.'/../../Factories/TestCaseFactory.php'),
|
||||
'pest_method_factory' => self::hashIfExists(__DIR__.'/../../Factories/TestCaseMethodFactory.php'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $a
|
||||
* @param array<string, mixed> $b
|
||||
*/
|
||||
public static function matches(array $a, array $b): bool
|
||||
{
|
||||
ksort($a);
|
||||
ksort($b);
|
||||
|
||||
return $a === $b;
|
||||
}
|
||||
|
||||
private static function hashIfExists(string $path): ?string
|
||||
{
|
||||
if (! is_file($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = @hash_file('xxh128', $path);
|
||||
|
||||
return $hash === false ? null : $hash;
|
||||
}
|
||||
|
||||
private static function readPestVersion(string $projectRoot): string
|
||||
{
|
||||
$installed = $projectRoot.'/vendor/composer/installed.json';
|
||||
|
||||
if (! is_file($installed)) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($installed);
|
||||
|
||||
if ($raw === false) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
|
||||
if (! is_array($data) || ! isset($data['packages']) || ! is_array($data['packages'])) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
foreach ($data['packages'] as $package) {
|
||||
if (is_array($package) && ($package['name'] ?? null) === 'pestphp/pest') {
|
||||
return (string) ($package['version'] ?? 'unknown');
|
||||
}
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
328
src/Plugins/Tia/Graph.php
Normal file
328
src/Plugins/Tia/Graph.php
Normal file
@ -0,0 +1,328 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
/**
|
||||
* File-level Test Impact Analysis graph.
|
||||
*
|
||||
* Persists the mapping `test_file → set<source_file>` so that subsequent runs
|
||||
* can skip tests whose dependencies have not changed. Paths are stored relative
|
||||
* to the project root and source files are deduplicated via an index so that
|
||||
* the on-disk JSON stays compact for large suites.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class Graph
|
||||
{
|
||||
/**
|
||||
* Relative path of each known source file, indexed by numeric id.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private array $files = [];
|
||||
|
||||
/**
|
||||
* Reverse lookup: source file → numeric id.
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
private array $fileIds = [];
|
||||
|
||||
/**
|
||||
* Edges: test file (relative) → list of source file ids.
|
||||
*
|
||||
* @var array<string, array<int, int>>
|
||||
*/
|
||||
private array $edges = [];
|
||||
|
||||
/**
|
||||
* Environment fingerprint captured at record time.
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
private array $fingerprint = [];
|
||||
|
||||
/**
|
||||
* Commit SHA the graph was recorded against (if in a git repo).
|
||||
*/
|
||||
private ?string $recordedAtSha = null;
|
||||
|
||||
/**
|
||||
* Canonicalised project root. Resolved through `realpath()` so paths
|
||||
* captured by coverage drivers (always real filesystem targets) match
|
||||
* regardless of whether the user's CWD is a symlink or has trailing
|
||||
* separators.
|
||||
*/
|
||||
private readonly string $projectRoot;
|
||||
|
||||
public function __construct(string $projectRoot)
|
||||
{
|
||||
$real = @realpath($projectRoot);
|
||||
|
||||
$this->projectRoot = $real !== false ? $real : $projectRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Records that a test file depends on the given source file.
|
||||
*/
|
||||
public function link(string $testFile, string $sourceFile): void
|
||||
{
|
||||
$testRel = $this->relative($testFile);
|
||||
$sourceRel = $this->relative($sourceFile);
|
||||
|
||||
if ($sourceRel === null || $testRel === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! isset($this->fileIds[$sourceRel])) {
|
||||
$id = count($this->files);
|
||||
$this->files[$id] = $sourceRel;
|
||||
$this->fileIds[$sourceRel] = $id;
|
||||
}
|
||||
|
||||
$this->edges[$testRel][] = $this->fileIds[$sourceRel];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the set of test files whose dependencies intersect $changedFiles.
|
||||
*
|
||||
* @param array<int, string> $changedFiles Absolute or relative paths.
|
||||
* @return array<int, string> Relative test file paths.
|
||||
*/
|
||||
public function affected(array $changedFiles): array
|
||||
{
|
||||
$changedIds = [];
|
||||
|
||||
foreach ($changedFiles as $file) {
|
||||
$rel = $this->relative($file);
|
||||
|
||||
if ($rel === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($this->fileIds[$rel])) {
|
||||
$changedIds[$this->fileIds[$rel]] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$affected = [];
|
||||
|
||||
foreach ($this->edges as $testFile => $ids) {
|
||||
foreach ($ids as $id) {
|
||||
if (isset($changedIds[$id])) {
|
||||
$affected[] = $testFile;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $affected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the given test file has any recorded dependencies.
|
||||
*/
|
||||
public function knowsTest(string $testFile): bool
|
||||
{
|
||||
$rel = $this->relative($testFile);
|
||||
|
||||
return $rel !== null && isset($this->edges[$rel]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string> All project-relative test files the graph knows.
|
||||
*/
|
||||
public function allTestFiles(): array
|
||||
{
|
||||
return array_keys($this->edges);
|
||||
}
|
||||
|
||||
public function setFingerprint(array $fingerprint): void
|
||||
{
|
||||
$this->fingerprint = $fingerprint;
|
||||
}
|
||||
|
||||
public function fingerprint(): array
|
||||
{
|
||||
return $this->fingerprint;
|
||||
}
|
||||
|
||||
public function setRecordedAtSha(?string $sha): void
|
||||
{
|
||||
$this->recordedAtSha = $sha;
|
||||
}
|
||||
|
||||
public function recordedAtSha(): ?string
|
||||
{
|
||||
return $this->recordedAtSha;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces edges for the given test files. Used during a partial record
|
||||
* run so that existing edges for other tests are preserved.
|
||||
*
|
||||
* @param array<string, array<int, string>> $testToFiles
|
||||
*/
|
||||
public function replaceEdges(array $testToFiles): void
|
||||
{
|
||||
foreach ($testToFiles as $testFile => $sources) {
|
||||
$testRel = $this->relative($testFile);
|
||||
|
||||
if ($testRel === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->edges[$testRel] = [];
|
||||
|
||||
foreach ($sources as $source) {
|
||||
$this->link($testFile, $source);
|
||||
}
|
||||
|
||||
// Deduplicate ids for this test.
|
||||
$this->edges[$testRel] = array_values(array_unique($this->edges[$testRel]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops edges whose test file no longer exists on disk. Prevents the graph
|
||||
* from keeping stale entries for deleted / renamed tests that would later
|
||||
* be flagged as affected and confuse PHPUnit's discovery.
|
||||
*/
|
||||
public function pruneMissingTests(): void
|
||||
{
|
||||
$root = rtrim($this->projectRoot, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
|
||||
|
||||
foreach (array_keys($this->edges) as $testRel) {
|
||||
if (! is_file($root.$testRel)) {
|
||||
unset($this->edges[$testRel]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function load(string $projectRoot, string $path): ?self
|
||||
{
|
||||
if (! is_file($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($path);
|
||||
|
||||
if ($raw === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
|
||||
if (! is_array($data) || ($data['schema'] ?? null) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$graph = new self($projectRoot);
|
||||
$graph->fingerprint = is_array($data['fingerprint'] ?? null) ? $data['fingerprint'] : [];
|
||||
$graph->recordedAtSha = is_string($data['recorded_at_sha'] ?? null) ? $data['recorded_at_sha'] : null;
|
||||
$graph->files = is_array($data['files'] ?? null) ? array_values($data['files']) : [];
|
||||
$graph->fileIds = array_flip($graph->files);
|
||||
$graph->edges = is_array($data['edges'] ?? null) ? $data['edges'] : [];
|
||||
|
||||
return $graph;
|
||||
}
|
||||
|
||||
public function save(string $path): bool
|
||||
{
|
||||
$dir = dirname($path);
|
||||
|
||||
if (! is_dir($dir) && ! @mkdir($dir, 0755, true) && ! is_dir($dir)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'schema' => 1,
|
||||
'fingerprint' => $this->fingerprint,
|
||||
'recorded_at_sha' => $this->recordedAtSha,
|
||||
'files' => $this->files,
|
||||
'edges' => $this->edges,
|
||||
];
|
||||
|
||||
$tmp = $path.'.'.bin2hex(random_bytes(4)).'.tmp';
|
||||
|
||||
$json = json_encode($payload, JSON_UNESCAPED_SLASHES);
|
||||
|
||||
if ($json === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (@file_put_contents($tmp, $json) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! @rename($tmp, $path)) {
|
||||
@unlink($tmp);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalises a path to be relative to the project root; returns `null` for
|
||||
* paths we should ignore (outside the project, unknown, virtual, vendor).
|
||||
*
|
||||
* Accepts both absolute paths (from Xdebug/PCOV coverage) and
|
||||
* project-relative paths (from `git diff`) — we normalise without relying
|
||||
* on `realpath()` of relative paths because the current working directory
|
||||
* is not guaranteed to be the project root.
|
||||
*/
|
||||
private function relative(string $path): ?string
|
||||
{
|
||||
if ($path === '' || $path === 'unknown') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (str_contains($path, "eval()'d")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$root = rtrim($this->projectRoot, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
|
||||
|
||||
$isAbsolute = str_starts_with($path, DIRECTORY_SEPARATOR)
|
||||
|| (strlen($path) >= 2 && $path[1] === ':'); // Windows drive
|
||||
|
||||
if ($isAbsolute) {
|
||||
$real = @realpath($path);
|
||||
|
||||
if ($real === false) {
|
||||
$real = $path;
|
||||
}
|
||||
|
||||
if (! str_starts_with($real, $root)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Always normalise to forward slashes. Windows' native separator
|
||||
// would otherwise produce keys that never match paths reported
|
||||
// by `git` (which always uses forward slashes).
|
||||
$relative = str_replace(DIRECTORY_SEPARATOR, '/', substr($real, strlen($root)));
|
||||
} else {
|
||||
// Normalise directory separators and strip any "./" prefix.
|
||||
$relative = str_replace(DIRECTORY_SEPARATOR, '/', $path);
|
||||
|
||||
while (str_starts_with($relative, './')) {
|
||||
$relative = substr($relative, 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Vendor packages are pinned by composer.lock. Any upgrade bumps the
|
||||
// fingerprint and invalidates the graph wholesale, so there is no
|
||||
// reason to track individual vendor files — doing so inflates the
|
||||
// graph by orders of magnitude on Laravel-style projects.
|
||||
if (str_starts_with($relative, 'vendor/')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $relative;
|
||||
}
|
||||
}
|
||||
237
src/Plugins/Tia/Recorder.php
Normal file
237
src/Plugins/Tia/Recorder.php
Normal file
@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
/**
|
||||
* Captures per-test file coverage using the PCOV driver.
|
||||
*
|
||||
* Acts as a singleton because PCOV has a single global collection state and
|
||||
* the recorder is wired into PHPUnit through two distinct subscribers
|
||||
* (`Prepared` / `Finished`) that must share context.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class Recorder
|
||||
{
|
||||
private static ?self $instance = null;
|
||||
|
||||
/**
|
||||
* Test file currently being recorded, or `null` when idle.
|
||||
*/
|
||||
private ?string $currentTestFile = null;
|
||||
|
||||
/**
|
||||
* Aggregated map: absolute test file → set<absolute source file>.
|
||||
*
|
||||
* @var array<string, array<string, true>>
|
||||
*/
|
||||
private array $perTestFiles = [];
|
||||
|
||||
/**
|
||||
* Cached class → test file resolution.
|
||||
*
|
||||
* @var array<string, string|null>
|
||||
*/
|
||||
private array $classFileCache = [];
|
||||
|
||||
private bool $active = false;
|
||||
|
||||
private bool $driverChecked = false;
|
||||
|
||||
private bool $driverAvailable = false;
|
||||
|
||||
private string $driver = 'none';
|
||||
|
||||
public static function instance(): self
|
||||
{
|
||||
return self::$instance ??= new self;
|
||||
}
|
||||
|
||||
public function activate(): void
|
||||
{
|
||||
$this->active = true;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
public function driverAvailable(): bool
|
||||
{
|
||||
if (! $this->driverChecked) {
|
||||
if (function_exists('pcov\\start')) {
|
||||
$this->driver = 'pcov';
|
||||
$this->driverAvailable = true;
|
||||
} elseif (function_exists('xdebug_start_code_coverage')) {
|
||||
// Probe: Xdebug silently emits a warning and refuses to start
|
||||
// when not in coverage mode. Suppress + check for mode errors.
|
||||
$ok = @\xdebug_start_code_coverage();
|
||||
|
||||
if ($ok === null || $ok) {
|
||||
@\xdebug_stop_code_coverage(false);
|
||||
$this->driver = 'xdebug';
|
||||
$this->driverAvailable = true;
|
||||
}
|
||||
}
|
||||
|
||||
$this->driverChecked = true;
|
||||
}
|
||||
|
||||
return $this->driverAvailable;
|
||||
}
|
||||
|
||||
public function driver(): string
|
||||
{
|
||||
$this->driverAvailable();
|
||||
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
public function beginTest(string $className, string $methodName, string $fallbackFile): void
|
||||
{
|
||||
if (! $this->active || ! $this->driverAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file = $this->resolveTestFile($className, $fallbackFile);
|
||||
|
||||
if ($file === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->currentTestFile = $file;
|
||||
|
||||
if ($this->driver === 'pcov') {
|
||||
\pcov\clear();
|
||||
\pcov\start();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Xdebug
|
||||
\xdebug_start_code_coverage();
|
||||
}
|
||||
|
||||
public function endTest(): void
|
||||
{
|
||||
if (! $this->active || ! $this->driverAvailable() || $this->currentTestFile === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->driver === 'pcov') {
|
||||
\pcov\stop();
|
||||
/** @var array<string, mixed> $data */
|
||||
$data = \pcov\collect(\pcov\inclusive);
|
||||
} else {
|
||||
/** @var array<string, mixed> $data */
|
||||
$data = \xdebug_get_code_coverage();
|
||||
// `true` resets Xdebug's internal buffer so the next `start()`
|
||||
// does not accumulate earlier tests' coverage into the current
|
||||
// one — otherwise the graph becomes progressively polluted.
|
||||
\xdebug_stop_code_coverage(true);
|
||||
}
|
||||
|
||||
foreach (array_keys($data) as $sourceFile) {
|
||||
$this->perTestFiles[$this->currentTestFile][$sourceFile] = true;
|
||||
}
|
||||
|
||||
$this->currentTestFile = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>> absolute test file → list of absolute source files.
|
||||
*/
|
||||
public function perTestFiles(): array
|
||||
{
|
||||
$out = [];
|
||||
|
||||
foreach ($this->perTestFiles as $testFile => $sources) {
|
||||
$out[$testFile] = array_keys($sources);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
private function resolveTestFile(string $className, string $fallbackFile): ?string
|
||||
{
|
||||
if (array_key_exists($className, $this->classFileCache)) {
|
||||
$file = $this->classFileCache[$className];
|
||||
} else {
|
||||
$file = $this->readPestFilename($className);
|
||||
$this->classFileCache[$className] = $file;
|
||||
}
|
||||
|
||||
if ($file !== null) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
if ($fallbackFile !== '' && $fallbackFile !== 'unknown' && ! str_contains($fallbackFile, "eval()'d")) {
|
||||
return $fallbackFile;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the file that *defines* the test class.
|
||||
*
|
||||
* Order of preference:
|
||||
* 1. Pest's generated `$__filename` static — the original `*.php` file
|
||||
* containing the `test()` calls (the eval'd class itself has no file).
|
||||
* 2. `ReflectionClass::getFileName()` — the concrete class's file. This
|
||||
* is intentionally more specific than `ReflectionMethod::getFileName()`
|
||||
* (which would return the *trait* file for methods brought in via
|
||||
* `uses SharedTestBehavior`).
|
||||
*/
|
||||
private function readPestFilename(string $className): ?string
|
||||
{
|
||||
if (! class_exists($className, false)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$reflection = new ReflectionClass($className);
|
||||
} catch (ReflectionException) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($reflection->hasProperty('__filename')) {
|
||||
try {
|
||||
$property = $reflection->getProperty('__filename');
|
||||
|
||||
if ($property->isStatic()) {
|
||||
$value = $property->getValue();
|
||||
|
||||
if (is_string($value) && $value !== '') {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} catch (ReflectionException) {
|
||||
// fall through to getFileName()
|
||||
}
|
||||
}
|
||||
|
||||
$file = $reflection->getFileName();
|
||||
|
||||
return $file !== false && $file !== '' ? $file : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all captured state. Useful for long-running hosts (daemons,
|
||||
* PHP-FPM, watchers) that invoke Pest multiple times in a single process
|
||||
* — without this, coverage from run N would bleed into run N+1.
|
||||
*/
|
||||
public function reset(): void
|
||||
{
|
||||
$this->currentTestFile = null;
|
||||
$this->perTestFiles = [];
|
||||
$this->classFileCache = [];
|
||||
$this->active = false;
|
||||
}
|
||||
}
|
||||
158
src/Plugins/Tia/State.php
Normal file
158
src/Plugins/Tia/State.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
/**
|
||||
* Shared TIA replay state consulted by Pest's `Testable` trait at runtime.
|
||||
*
|
||||
* Why a singleton: the plugin runs in `handleArguments` (before tests are
|
||||
* discovered), but the actual replay decision has to happen when each test
|
||||
* boots (`setUp` / `__runTest`). Those call sites are inside a trait that
|
||||
* has no easy way to inject dependencies, so they reach into this state
|
||||
* holder.
|
||||
*
|
||||
* Decision: a test file replays its previous pass iff
|
||||
* 1. TIA replay mode is active,
|
||||
* 2. the file is **known** to the dependency graph,
|
||||
* 3. the file is **not** in the affected set (its deps are unchanged),
|
||||
* 4. it was **not** in the previous run's defect list (only cached passes
|
||||
* replay; previously-failing tests rerun so users see current state).
|
||||
*
|
||||
* Points 1-3 live in this class. Point 4 uses PHPUnit's own
|
||||
* `DefaultResultCache`, queried at decision time.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class State
|
||||
{
|
||||
private static ?self $instance = null;
|
||||
|
||||
private bool $replayMode = false;
|
||||
|
||||
/**
|
||||
* Keys are project-relative test file paths. Affected = must rerun.
|
||||
*
|
||||
* @var array<string, true>
|
||||
*/
|
||||
private array $affectedFiles = [];
|
||||
|
||||
/**
|
||||
* Keys are project-relative test file paths. Known = recorded in graph.
|
||||
*
|
||||
* @var array<string, true>
|
||||
*/
|
||||
private array $knownFiles = [];
|
||||
|
||||
/**
|
||||
* Test ids (class::method) that were in the previous run's defect list.
|
||||
*
|
||||
* @var array<string, true>
|
||||
*/
|
||||
private array $previousDefects = [];
|
||||
|
||||
/**
|
||||
* Canonicalised project root used for relative-path calculations.
|
||||
*/
|
||||
private string $projectRoot = '';
|
||||
|
||||
public static function instance(): self
|
||||
{
|
||||
return self::$instance ??= new self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns on replay mode with the given graph + affected set.
|
||||
*
|
||||
* @param array<string, true> $affectedFiles
|
||||
*/
|
||||
public function activate(string $projectRoot, Graph $graph, array $affectedFiles, array $previousDefects): void
|
||||
{
|
||||
$real = @realpath($projectRoot);
|
||||
|
||||
$this->projectRoot = $real !== false ? $real : $projectRoot;
|
||||
$this->replayMode = true;
|
||||
$this->affectedFiles = $affectedFiles;
|
||||
$this->previousDefects = $previousDefects;
|
||||
|
||||
// Pre-compute the known set from the graph so per-test lookups stay
|
||||
// O(1). Iterating edges once here beats calling `Graph::knowsTest`
|
||||
// from every test's `setUp`.
|
||||
$this->knownFiles = [];
|
||||
|
||||
foreach ($graph->allTestFiles() as $rel) {
|
||||
$this->knownFiles[$rel] = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function isReplayMode(): bool
|
||||
{
|
||||
return $this->replayMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` when the given absolute test file should replay its
|
||||
* previous passing result instead of re-executing. `$testId` may be
|
||||
* `null` when the caller cannot cheaply determine it (e.g. early in
|
||||
* `setUp` before PHPUnit has published the name) — in that case we
|
||||
* replay iff the file is safe at the file level, and `__runTest` will
|
||||
* repeat the check with a proper id.
|
||||
*/
|
||||
public function shouldReplayFromCache(string $absoluteTestFile, ?string $testId = null): bool
|
||||
{
|
||||
if (! $this->replayMode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$rel = $this->relative($absoluteTestFile);
|
||||
|
||||
if ($rel === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! isset($this->knownFiles[$rel])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($this->affectedFiles[$rel])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($testId !== null && isset($this->previousDefects[$testId])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->replayMode = false;
|
||||
$this->affectedFiles = [];
|
||||
$this->knownFiles = [];
|
||||
$this->previousDefects = [];
|
||||
$this->projectRoot = '';
|
||||
}
|
||||
|
||||
private function relative(string $path): ?string
|
||||
{
|
||||
if ($path === '' || $this->projectRoot === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$real = @realpath($path);
|
||||
|
||||
if ($real === false) {
|
||||
$real = $path;
|
||||
}
|
||||
|
||||
$root = rtrim($this->projectRoot, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
|
||||
|
||||
if (! str_starts_with($real, $root)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str_replace(DIRECTORY_SEPARATOR, '/', substr($real, strlen($root)));
|
||||
}
|
||||
}
|
||||
65
src/Plugins/Tia/TiaTestCaseFilter.php
Normal file
65
src/Plugins/Tia/TiaTestCaseFilter.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
use Pest\Contracts\TestCaseFilter;
|
||||
use Pest\Plugins\Tia\Graph;
|
||||
|
||||
/**
|
||||
* Accepts a test file in one of three cases:
|
||||
*
|
||||
* 1. The file falls outside the project root (we cannot reason about it, so
|
||||
* stay safe and run it).
|
||||
* 2. The graph has no record of the file — this is a new test that was
|
||||
* never part of a recording run, so we accept it by default. Skipping
|
||||
* unknown tests would be a correctness hazard (developers add tests and
|
||||
* TIA would silently not run them).
|
||||
* 3. The graph knows the file AND it is in the affected set.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class TiaTestCaseFilter implements TestCaseFilter
|
||||
{
|
||||
/**
|
||||
* @param array<string, true> $affectedTestFiles Keys are project-relative test file paths.
|
||||
*/
|
||||
public function __construct(
|
||||
private string $projectRoot,
|
||||
private Graph $graph,
|
||||
private array $affectedTestFiles,
|
||||
) {}
|
||||
|
||||
public function accept(string $testCaseFilename): bool
|
||||
{
|
||||
$rel = $this->relative($testCaseFilename);
|
||||
|
||||
if ($rel === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->graph->knowsTest($rel)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isset($this->affectedTestFiles[$rel]);
|
||||
}
|
||||
|
||||
private function relative(string $path): ?string
|
||||
{
|
||||
$real = @realpath($path);
|
||||
|
||||
if ($real === false) {
|
||||
$real = $path;
|
||||
}
|
||||
|
||||
$root = rtrim($this->projectRoot, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
|
||||
|
||||
if (! str_starts_with($real, $root)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str_replace(DIRECTORY_SEPARATOR, '/', substr($real, strlen($root)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user