Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/Plugins/Tia/Fingerprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Pest\Plugins\Tia;

use Pest\Plugins\Tia\Contracts\Lockfile;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;
use Symfony\Component\Process\Process;

/**
* @internal
Expand Down Expand Up @@ -279,13 +280,16 @@ private static function isTrackedByGit(string $projectRoot, string $relativePath
return $cache[$key] = true;
}

$finder = (new Finder)
->in($projectRoot)
->depth('== 0')
->name($relativePath)
->ignoreVCSIgnored(true);
$process = new Process(['git', 'check-ignore', '-q', '--', $relativePath], $projectRoot);
$process->setTimeout(5.0);

return $cache[$key] = $finder->hasResults();
try {
$process->run();
} catch (ProcessException) {
return $cache[$key] = true;
}

return $cache[$key] = $process->getExitCode() !== 0;
}

private static function contentHashOrNull(string $path): ?string
Expand Down
109 changes: 109 additions & 0 deletions tests/Unit/Plugins/Tia/Fingerprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

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_xml'])->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_xml'])->not->toBeNull()
->and($worktreeFingerprint['structural']['composer_lock'])->toBe($cloneFingerprint['structural']['composer_lock'])
->and($worktreeFingerprint['structural']['phpunit_xml'])->toBe($cloneFingerprint['structural']['phpunit_xml']);
} finally {
fingerprintRemoveDirectory($clone);
}
});

it('excludes gitignored untracked files from the fingerprint', function (): void {
$clone = fingerprintRepository();

try {
file_put_contents($clone.'/composer.lock', '{"packages": []}');
file_put_contents($clone.'/.gitignore', "phpunit.xml\n");
fingerprintGit($clone, 'add', '-A');
fingerprintGit($clone, 'commit', '-q', '-m', 'init');

file_put_contents($clone.'/phpunit.xml', '<phpunit/>');

$fingerprint = Fingerprint::compute($clone);

expect($fingerprint['structural']['phpunit_xml'])->toBeNull()
->and($fingerprint['structural']['composer_lock'])->not->toBeNull();
} finally {
fingerprintRemoveDirectory($clone);
}
});
});