Files
pest/tests/Unit/Plugins/Tia/Fingerprint.php
T
Jhon Farias 0fe7c2440c fix(tia): fingerprint the bootstrap and include path the command line sets (#2068)
* fix(tia): fingerprint the bootstrap and include path the command line sets

* fix(tia): keep the bootstrap and include path in their own fingerprint fields

* fix(tia): keep a literal backslash in a Unix path in the fingerprint

* fix(tia): keep the include path entries apart in the fingerprint

* Refresh output snapshots for TIA configuration tests

---------

Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
2026-11-24 00:48:55 +05:30

137 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
use Pest\Plugins\Tia\ConfigurationFile;
use Pest\Plugins\Tia\Fingerprint;
use Symfony\Component\Process\Process;
function fingerprintGit(string $cwd, string ...$args): void
{
$process = new Process(['git', ...$args], $cwd);
$process->setTimeout(10.0);
$process->mustRun();
}
function fingerprintRepository(): string
{
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest_fingerprint_'.uniqid();
mkdir($root, 0777, true);
fingerprintGit($root, 'init', '-q');
fingerprintGit($root, 'config', 'user.email', 'pest@example.com');
fingerprintGit($root, 'config', 'user.name', 'Pest');
return $root;
}
function fingerprintRemoveDirectory(string $path): void
{
if (! is_dir($path)) {
return;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($iterator as $file) {
@chmod($file->getPathname(), 0777);
$file->isDir() ? @rmdir($file->getPathname()) : @unlink($file->getPathname());
}
@rmdir($path);
}
describe('compute()', function (): void {
it('fingerprints tracked structural files in a regular clone', function (): void {
$clone = fingerprintRepository();
try {
file_put_contents($clone.'/composer.lock', '{"packages": []}');
file_put_contents($clone.'/phpunit.xml', '<phpunit/>');
fingerprintGit($clone, 'add', '-A');
fingerprintGit($clone, 'commit', '-q', '-m', 'init');
$fingerprint = Fingerprint::compute($clone);
expect($fingerprint['structural']['composer_lock'])->not->toBeNull()
->and($fingerprint['structural']['phpunit_configuration'])->not->toBeNull();
} finally {
fingerprintRemoveDirectory($clone);
}
});
it('fingerprints tracked structural files inside a linked git worktree', function (): void {
$clone = fingerprintRepository();
try {
file_put_contents($clone.'/composer.lock', '{"packages": []}');
file_put_contents($clone.'/phpunit.xml', '<phpunit/>');
file_put_contents($clone.'/.gitignore', ".worktrees/\n");
fingerprintGit($clone, 'add', '-A');
fingerprintGit($clone, 'commit', '-q', '-m', 'init');
fingerprintGit($clone, 'worktree', 'add', '-q', '.worktrees/feature');
$worktree = $clone.'/.worktrees/feature';
$cloneFingerprint = Fingerprint::compute($clone);
$worktreeFingerprint = Fingerprint::compute($worktree);
expect($worktreeFingerprint['structural']['composer_lock'])->not->toBeNull()
->and($worktreeFingerprint['structural']['phpunit_configuration'])->not->toBeNull()
->and($worktreeFingerprint['structural']['composer_lock'])->toBe($cloneFingerprint['structural']['composer_lock'])
->and($worktreeFingerprint['structural']['phpunit_configuration'])->toBe($cloneFingerprint['structural']['phpunit_configuration']);
} finally {
fingerprintRemoveDirectory($clone);
}
});
it('excludes gitignored untracked files from the fingerprint', function (): void {
$clone = fingerprintRepository();
try {
file_put_contents($clone.'/phpunit.xml', '<phpunit/>');
file_put_contents($clone.'/.gitignore', "composer.lock\n");
fingerprintGit($clone, 'add', '-A');
fingerprintGit($clone, 'commit', '-q', '-m', 'init');
file_put_contents($clone.'/composer.lock', '{"packages": []}');
$fingerprint = Fingerprint::compute($clone);
expect($fingerprint['structural']['composer_lock'])->toBeNull()
->and($fingerprint['structural']['phpunit_configuration'])->not->toBeNull();
} finally {
fingerprintRemoveDirectory($clone);
}
});
it('keeps the command line bootstrap and include path out of a run that sets neither', function (): void {
$root = fingerprintRepository();
try {
$structural = Fingerprint::compute($root, ConfigurationFile::fromArguments(['--tia']))['structural'];
expect($structural)->not->toHaveKey('phpunit_bootstrap')
->and($structural)->not->toHaveKey('phpunit_include_path');
} finally {
fingerprintRemoveDirectory($root);
}
});
it('tells a bootstrap whose name looks like an include path apart from a bootstrap and an include path', function (): void {
$root = fingerprintRepository();
try {
$bootstrapOnly = Fingerprint::compute($root, ConfigurationFile::fromArguments(['--bootstrap', 'bootstrap.php;include-path=app']));
$bootstrapAndIncludePath = Fingerprint::compute($root, ConfigurationFile::fromArguments(['--bootstrap', 'bootstrap.php', '--include-path', 'app']));
expect(Fingerprint::structuralMatches($bootstrapOnly, $bootstrapAndIncludePath))->toBeFalse()
->and(Fingerprint::structuralDrift($bootstrapOnly, $bootstrapAndIncludePath))->toBe(['phpunit_bootstrap', 'phpunit_include_path']);
} finally {
fingerprintRemoveDirectory($root);
}
});
});