mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
b5851d67ff
* fix(tia): run the full suite when code the project loads from outside its root changes * test(tia): cover the configuration fingerprint of a nested project * fix(tia): run the full suite when a declared external root holds files git ignores * fix(tia): track ignored inputs under external roots by content hash * fix(tia): harden external source tracking from the review loop * test: update the suite totals for the external source tests --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
445 lines
18 KiB
PHP
445 lines
18 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Pest\Plugins\Tia\ChangedFiles;
|
|
use Pest\Plugins\Tia\ExternalSources;
|
|
use Pest\Plugins\Tia\Fingerprint;
|
|
use Symfony\Component\Process\Process;
|
|
use Tests\Fixtures\Tia\GitRepo;
|
|
use Tests\Fixtures\Tia\Project;
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
function tiaMonorepoRoots(?string $created = null): array
|
|
{
|
|
static $roots = [];
|
|
|
|
if ($created !== null) {
|
|
$roots[] = $created;
|
|
|
|
return $roots;
|
|
}
|
|
|
|
$all = $roots;
|
|
$roots = [];
|
|
|
|
return $all;
|
|
}
|
|
|
|
/**
|
|
* @return array{root: string, project: string, repo: GitRepo, sha: string}
|
|
*/
|
|
function tiaMonorepoRepository(): array
|
|
{
|
|
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8));
|
|
|
|
mkdir($root.'/backend/app', 0755, true);
|
|
mkdir($root.'/frontend', 0755, true);
|
|
|
|
file_put_contents($root.'/backend/app/Service.php', "<?php\n\$service = 1;\n");
|
|
file_put_contents($root.'/frontend/widget.php', "<?php\n\$widget = 1;\n");
|
|
|
|
tiaMonorepoRoots($root);
|
|
|
|
$repo = new GitRepo($root);
|
|
$repo->init('master');
|
|
|
|
return ['root' => $root, 'project' => $root.'/backend', 'repo' => $repo, 'sha' => $repo->sha()];
|
|
}
|
|
|
|
afterEach(function (): void {
|
|
foreach (tiaMonorepoRoots() as $root) {
|
|
new Process(['rm', '-rf', $root])->run();
|
|
}
|
|
|
|
Project::destroyAll();
|
|
});
|
|
|
|
it('resolves the project prefix inside the repository', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->gitPrefix())->toBe('backend/')
|
|
->and(new ChangedFiles($monorepo['root'])->gitPrefix())->toBeEmpty();
|
|
})->skipOnWindows();
|
|
|
|
it('keeps a space that begins the name of the project directory', function (): void {
|
|
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8));
|
|
|
|
mkdir($root.'/ backend/app', 0755, true);
|
|
file_put_contents($root.'/ backend/app/Service.php', "<?php\n\$service = 1;\n");
|
|
|
|
tiaMonorepoRoots($root);
|
|
|
|
$repo = new GitRepo($root);
|
|
$repo->init('master');
|
|
|
|
file_put_contents($root.'/ backend/app/Service.php', "<?php\n\$service = 2;\n");
|
|
|
|
$project = new ChangedFiles($root.'/ backend');
|
|
|
|
expect($project->gitPrefix())->toBe(' backend/')
|
|
->and($project->since($repo->sha()))->toBe(['app/Service.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('keeps the character that ends the name of the repository root', function (string $lastCharacter): void {
|
|
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8)).$lastCharacter;
|
|
|
|
mkdir($root.'/backend/app', 0755, true);
|
|
mkdir($root.'/frontend', 0755, true);
|
|
file_put_contents($root.'/backend/app/Service.php', "<?php\n\$service = 1;\n");
|
|
file_put_contents($root.'/frontend/widget.php', "<?php\n\$widget = 1;\n");
|
|
|
|
tiaMonorepoRoots($root);
|
|
|
|
$repo = new GitRepo($root);
|
|
$repo->init('master');
|
|
|
|
file_put_contents($root.'/backend/app/Service.php', "<?php\n\$service = 2;\n");
|
|
file_put_contents($root.'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
|
|
$project = new ChangedFiles($root.'/backend');
|
|
|
|
expect($project->since($repo->sha()))->toBe(['app/Service.php'])
|
|
->and($project->outsideProject())->toBe(['frontend/widget.php']);
|
|
})->with(['a space' => ' ', 'a carriage return' => "\r"])->skipOnWindows();
|
|
|
|
it('reports working tree changes relative to the project, dropping siblings', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))
|
|
->toBe(['app/Service.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('reports committed changes relative to the project, dropping siblings', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
|
|
$monorepo['repo']->commit('Second commit');
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))
|
|
->toBe(['app/Service.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('drops a file whose content came back to its baseline across the prefix boundary', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
|
$monorepo['repo']->commit('Change the service');
|
|
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 1;\n");
|
|
$monorepo['repo']->commit('Put the service back');
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))->toBe([]);
|
|
})->skipOnWindows();
|
|
|
|
it('honours a gitignore inside the project', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['project'].'/.gitignore', "/ignored.php\n");
|
|
file_put_contents($monorepo['project'].'/ignored.php', "<?php\n");
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))
|
|
->toContain('app/Service.php')
|
|
->not->toContain('ignored.php');
|
|
})->skipOnWindows();
|
|
|
|
it('leaves a project at the repository root untouched', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
|
|
expect(new ChangedFiles($monorepo['root'])->since($monorepo['sha']))
|
|
->toBe(['frontend/widget.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('reports the changes it left outside the project', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
$changedFiles->since($monorepo['sha']);
|
|
|
|
expect($changedFiles->outsideProject())->toBe(['frontend/widget.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('reports nothing outside the project for a project at the repository root', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['root']);
|
|
$changedFiles->since($monorepo['sha']);
|
|
|
|
expect($changedFiles->outsideProject())->toBeEmpty();
|
|
})->skipOnWindows();
|
|
|
|
it('ties a fingerprint to the project location inside the repository', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
mkdir($monorepo['root'].'/admin/app', 0755, true);
|
|
file_put_contents($monorepo['root'].'/admin/app/Service.php', "<?php\n\$service = 1;\n");
|
|
$monorepo['repo']->commit('add a second project');
|
|
|
|
$nested = Fingerprint::compute($monorepo['project']);
|
|
$sibling = Fingerprint::compute($monorepo['root'].'/admin');
|
|
$root = Fingerprint::compute($monorepo['root']);
|
|
|
|
expect($nested['structural']['project_prefix'])->toBe('backend/')
|
|
->and($sibling['structural']['project_prefix'])->toBe('admin/')
|
|
->and($root['structural'])->not->toHaveKey('project_prefix')
|
|
->and(Fingerprint::structuralMatches($nested, $sibling))->toBeFalse()
|
|
->and(Fingerprint::structuralMatches($nested, $root))->toBeFalse()
|
|
->and(Fingerprint::structuralDrift($sibling, $nested))->toContain('project_prefix');
|
|
})->skipOnWindows();
|
|
|
|
it('sees a committed non-ascii path that git would otherwise quote', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
mkdir($monorepo['project'].'/resources/views', 0755, true);
|
|
file_put_contents($monorepo['project'].'/resources/views/\u{26a1}orders.blade.php', "<div>a</div>\n");
|
|
$monorepo['repo']->commit('add a lightning view');
|
|
|
|
$sha = $monorepo['repo']->sha();
|
|
|
|
file_put_contents($monorepo['project'].'/resources/views/\u{26a1}orders.blade.php', "<div>b</div>\n");
|
|
$monorepo['repo']->commit('reword the lightning view');
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
|
|
expect($changedFiles->since($sha))->toBe(['resources/views/\u{26a1}orders.blade.php'])
|
|
->and($changedFiles->outsideProject())->toBeEmpty();
|
|
})->skipOnWindows();
|
|
|
|
it('sees a committed non-ascii path at the repository root too', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/\u{26a1}widget.php', "<?php\n\$widget = 1;\n");
|
|
$monorepo['repo']->commit('add a lightning widget');
|
|
|
|
$sha = $monorepo['repo']->sha();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/\u{26a1}widget.php', "<?php\n\$widget = 2;\n");
|
|
$monorepo['repo']->commit('rework the lightning widget');
|
|
|
|
expect(new ChangedFiles($monorepo['root'])->since($sha))
|
|
->toBe(['frontend/\u{26a1}widget.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('keeps a tracked outside change and drops an untracked ignored one', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/.gitignore', "dist/\n");
|
|
mkdir($monorepo['root'].'/frontend/dist', 0755, true);
|
|
file_put_contents($monorepo['root'].'/frontend/dist/bundle.js', "var a = 1;\n");
|
|
$monorepo['repo']->run(['add', '-f', 'frontend/dist/bundle.js']);
|
|
$monorepo['repo']->commit('track a built bundle that the ignore rules match');
|
|
|
|
$sha = $monorepo['repo']->sha();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/dist/bundle.js', "var a = 2;\n");
|
|
file_put_contents($monorepo['root'].'/frontend/dist/untracked.js', "var b = 1;\n");
|
|
$monorepo['repo']->commit('rebuild the bundle');
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
$changedFiles->since($sha);
|
|
|
|
expect($changedFiles->outsideProject())->toBe(['frontend/dist/bundle.js']);
|
|
})->skipOnWindows();
|
|
|
|
it('keeps a tracked project change that the ignore rules match', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/.gitignore', "build/\n");
|
|
mkdir($monorepo['project'].'/build', 0755, true);
|
|
file_put_contents($monorepo['project'].'/build/Generated.php', "<?php\n\$built = 1;\n");
|
|
$monorepo['repo']->run(['add', '-f', 'backend/build/Generated.php']);
|
|
$monorepo['repo']->commit('track a generated file that the ignore rules match');
|
|
|
|
$sha = $monorepo['repo']->sha();
|
|
|
|
file_put_contents($monorepo['project'].'/build/Generated.php', "<?php\n\$built = 2;\n");
|
|
file_put_contents($monorepo['project'].'/build/Untracked.php', "<?php\n");
|
|
$monorepo['repo']->commit('regenerate the file');
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->since($sha))
|
|
->toBe(['build/Generated.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('reads committed paths from the repository root even when git is told to be relative', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
$monorepo['repo']->run(['config', 'diff.relative', 'true']);
|
|
|
|
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
$monorepo['repo']->commit('rework both sides');
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
|
|
expect($changedFiles->since($monorepo['sha']))->toBe(['app/Service.php'])
|
|
->and($changedFiles->outsideProject())->toBe(['frontend/widget.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('tells a working tree change outside the project from a committed one', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
|
$monorepo['repo']->commit('rework the widget');
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/other.php', "<?php\n\$other = 1;\n");
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
$changedFiles->since($monorepo['sha']);
|
|
|
|
expect($changedFiles->outsideProject())->toBe(['frontend/widget.php', 'frontend/other.php'])
|
|
->and($changedFiles->outsideProjectDirty())->toBe(['frontend/other.php']);
|
|
})->skipOnWindows();
|
|
|
|
it('keeps a changed file whose name is a number as a string', function (): void {
|
|
$project = Project::make();
|
|
$sha = $project->git()->sha();
|
|
|
|
file_put_contents($project->path('123'), "changed\n");
|
|
$project->git()->commit('add a file with a numeric name');
|
|
|
|
expect(new ChangedFiles($project->path())->since($sha))->toBe(['123']);
|
|
})->skipOnWindows();
|
|
|
|
it('keeps a changed file outside the project whose name is a number as a string', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/123', "outside\n");
|
|
file_put_contents($monorepo['project'].'/123', "inside\n");
|
|
$monorepo['repo']->commit('add files with a numeric name');
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
|
|
expect($changedFiles->since($monorepo['sha']))->toBe(['123'])
|
|
->and($changedFiles->outsideProject())->toBe(['123']);
|
|
})->skipOnWindows();
|
|
|
|
it('matches no external root for a changed file whose name is a number', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
$previous = (string) getcwd();
|
|
|
|
mkdir($monorepo['root'].'/packages/shared/src', 0755, true);
|
|
file_put_contents($monorepo['root'].'/packages/shared/src/Shared.php', "<?php\n\$shared = 1;\n");
|
|
file_put_contents($monorepo['project'].'/composer.json', (string) json_encode([
|
|
'autoload' => ['psr-4' => ['Shared\\' => '../packages/shared/src']],
|
|
], JSON_PRETTY_PRINT));
|
|
$monorepo['repo']->commit('let the project load a sibling package');
|
|
$sha = $monorepo['repo']->sha();
|
|
|
|
file_put_contents($monorepo['root'].'/123', "changed\n");
|
|
$monorepo['repo']->commit('add a file with a numeric name');
|
|
|
|
chdir($monorepo['project']);
|
|
ExternalSources::flush();
|
|
|
|
try {
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
$changedFiles->since($sha);
|
|
|
|
$matched = ExternalSources::matching($monorepo['project'], $changedFiles->outsideProject());
|
|
} finally {
|
|
chdir($previous);
|
|
ExternalSources::flush();
|
|
}
|
|
|
|
expect($matched)->toBeEmpty();
|
|
})->skipOnWindows();
|
|
|
|
it('hashes an external input without reading it into memory', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
$handle = fopen($monorepo['root'].'/frontend/database.sqlite', 'wb');
|
|
ftruncate($handle, 64 * 1024 * 1024);
|
|
fclose($handle);
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
|
|
memory_reset_peak_usage();
|
|
$before = memory_get_peak_usage();
|
|
|
|
$tree = $changedFiles->baselineTree($monorepo['sha'], ['frontend/database.sqlite']);
|
|
|
|
expect(memory_get_peak_usage() - $before)->toBeLessThan(8 * 1024 * 1024)
|
|
->and($tree)->toHaveKey('../frontend/database.sqlite');
|
|
})->skipOnWindows();
|
|
|
|
it('sees a whitespace change inside an external input', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/data.js', "export default \"a b\";\n");
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
$tree = $changedFiles->baselineTree($monorepo['sha'], ['frontend/data.js']);
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/data.js', "export default \"a b\";\n");
|
|
|
|
expect($changedFiles->changedSinceLastRun([], ['frontend/data.js'], $tree))->toBe([[], ['frontend/data.js']]);
|
|
})->skipOnWindows();
|
|
|
|
it('reports a changed file whose name looks like a pathspec', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['project'].'/:(fixtures).php', "<?php\n");
|
|
|
|
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))->toContain(':(fixtures).php');
|
|
})->skipOnWindows();
|
|
|
|
it('reports an external input that the listing no longer names', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/generated.php', "<?php\n");
|
|
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
$before = $changedFiles->externalInputHashes(['frontend/generated.php']);
|
|
|
|
expect($changedFiles->changedExternalInputs($before, $changedFiles->externalInputHashes([])))->toBe(['frontend/generated.php'])
|
|
->and($changedFiles->changedExternalInputs($before, $changedFiles->externalInputHashes(['frontend/generated.php'])))->toBeEmpty();
|
|
})->skipOnWindows();
|
|
|
|
it('reports no change for an external input it cannot read', function (): void {
|
|
$monorepo = tiaMonorepoRepository();
|
|
|
|
file_put_contents($monorepo['root'].'/frontend/secret.php', "<?php\n");
|
|
chmod($monorepo['root'].'/frontend/secret.php', 0000);
|
|
|
|
try {
|
|
$changedFiles = new ChangedFiles($monorepo['project']);
|
|
|
|
expect($changedFiles->changedExternalInputs(
|
|
$changedFiles->externalInputHashes(['frontend/secret.php']),
|
|
$changedFiles->externalInputHashes(['frontend/secret.php']),
|
|
))->toBeEmpty();
|
|
} finally {
|
|
chmod($monorepo['root'].'/frontend/secret.php', 0644);
|
|
}
|
|
})->skipOnWindows()->skip(function_exists('posix_geteuid') && posix_geteuid() === 0, 'root reads every file.');
|
|
|
|
it('keeps a snapshotted file whose name is a number as a string', function (): void {
|
|
$project = Project::make();
|
|
|
|
file_put_contents($project->path('123'), "changed\n");
|
|
|
|
expect(new ChangedFiles($project->path())->filterUnchangedSinceLastRun([], ['123' => 'stale']))->toBe(['123']);
|
|
});
|
|
|
|
it('keeps a branch whose name is a number as a string', function (): void {
|
|
$project = Project::make();
|
|
$project->git()->switchTo('123', new: true);
|
|
|
|
expect(new ChangedFiles($project->path())->branchNames())->toBe(['123', 'master']);
|
|
});
|