Files
pest/tests/Unit/Plugins/Tia/Storage.php
T
Jhon Farias a2d3e6badd fix(tia): support a Pest project nested in a git repository (#1914)
* fix(tia): support a Pest project nested in a git repository

Tia refused to run whenever the Pest project was not the git repository
root, because git prints and addresses paths relative to the repository
root while the dependency graph is keyed on project-relative paths.

Translate at every git boundary instead of guarding against the mismatch.
`Git::pathPrefix()` resolves the directory of the project inside the
repository, and `ChangedFiles` strips that prefix off the paths git prints,
drops the paths of sibling packages, and prepends it to `git show`. Both
diffs read `-z` records, because `core.quotePath` C-quotes a path outside
ASCII, and `--no-relative` holds the frame when a user sets `diff.relative`.

Three places read `<projectRoot>/.git` directly, which is absent in a
nested project and in a worktree, and now ask git: `BaseRemote`, `Storage`
and `Fingerprint::isTrackedByGit()`. `Storage` folds the prefix into the
state key so two projects in one repository do not share a graph, and the
fingerprint carries the prefix so a baseline published by a sibling project
is rejected. A project at the repository root keeps the key and the
fingerprint it had.

`git check-ignore` runs without `--no-index`, so a tracked file that an
ignore rule matches counts as part of the project on both sides of the
boundary.

Changes outside the project are dropped and reported by count in the run
summary, so a nested project does not look idle when the repository around
it moved.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(tia): keep whitespace at the edge of the paths git returns

* fix(tia): run the full suite when a PHP file outside a nested project changes

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
2026-11-22 20:41:58 +05:30

71 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
use Pest\Plugins\Tia\Bootstrapper;
use Pest\Plugins\Tia\Configuration;
use Pest\Plugins\Tia\Contracts\State;
use Pest\Plugins\Tia\FileState;
use Pest\Plugins\Tia\Storage;
use Pest\Support\Container;
use Pest\TestSuite;
use Symfony\Component\Process\Process;
use Tests\Fixtures\Tia\GitRepo;
afterEach(function (): void {
Storage::useDirectory(null);
});
it('uses a project-relative configured directory', function (): void {
$container = new Container;
$testSuite = new TestSuite(sys_get_temp_dir(), 'tests');
$container->add(TestSuite::class, $testSuite);
(new Configuration)->directory('.pest/tia');
new Bootstrapper($container)->boot();
$state = $container->get(State::class);
if (! $state instanceof FileState) {
throw new RuntimeException('Expected the TIA state to use file storage.');
}
expect(Storage::tempDir('/project'))->toBe('/project'.DIRECTORY_SEPARATOR.'.pest/tia')
->and($state->pathFor('graph.json'))->toBe($testSuite->rootPath.DIRECTORY_SEPARATOR.'.pest/tia/graph.json');
});
it('uses an absolute configured directory', function (): void {
(new Configuration)->directory('/tmp/pest-tia');
expect(Storage::tempDir('/project'))->toBe('/tmp/pest-tia');
});
it('keeps the state directory of a project at the repository root', function (): void {
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-storage-'.bin2hex(random_bytes(8));
mkdir($root.'/backend', 0755, true);
$repo = new GitRepo($root);
$repo->init('master');
$repo->addOrigin('git@github.com:pestphp/tia-fixture.git');
$home = $root.DIRECTORY_SEPARATOR.'.home';
mkdir($home, 0755, true);
$original = getenv('HOME');
putenv('HOME='.$home);
try {
$rootDir = Storage::tempDir($root);
$nestedDir = Storage::tempDir($root.'/backend');
} finally {
putenv($original === false ? 'HOME' : 'HOME='.$original);
new Process(['rm', '-rf', $root])->run();
}
$identity = substr(hash('sha256', 'github.com/pestphp/tia-fixture'), 0, 16);
expect(basename($rootDir))->toEndWith('-'.$identity)
->and(basename($nestedDir))->not->toEndWith('-'.$identity);
})->skipOnWindows();