Select dirty tests with quoted Git paths (#2042)

* Select dirty tests with quoted Git paths

* Skip missing files when selecting dirty tests

---------

Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Ikki Nishihara
2026-11-20 06:03:19 +09:00
committed by GitHub
parent 989c08008f
commit a07f68bc54
3 changed files with 99 additions and 17 deletions
+14 -16
View File
@@ -46,33 +46,31 @@ final class GitDirtyTestCaseFilter implements TestCaseFilter
private function loadChangedFiles(): void
{
$status = new Git(timeout: 60.0)->raw(['status', '--short', '--', '*.php']);
$git = new Git($this->projectRoot, timeout: 60.0);
$status = $git->raw(['status', '--porcelain=v1', '-z', '--no-renames', '--', '*.php']);
$prefix = $git->raw(['rev-parse', '--show-prefix']);
if ($status === null) {
if ($status === null || $prefix === null) {
throw new MissingDependency('Filter by dirty files', 'git');
}
$output = preg_split('/\R+/', $status, flags: PREG_SPLIT_NO_EMPTY);
assert(is_array($output));
$pathOffset = 3 + strlen(rtrim($prefix, "\r\n"));
$dirtyFiles = [];
foreach ($output as $dirtyFile) {
$dirtyFiles[substr($dirtyFile, 3)] = trim(substr($dirtyFile, 0, 3));
foreach (explode("\0", $status) as $entry) {
if ($entry === '' || trim(substr($entry, 0, 2)) === 'D') {
continue;
}
$dirtyFiles[] = substr($entry, $pathOffset);
}
$dirtyFiles = array_filter($dirtyFiles, fn (string $status): bool => $status !== 'D');
$dirtyFiles = array_map(
fn (string $file, string $status): string => in_array($status, ['R', 'RM'], true)
? explode(' -> ', $file)[1]
: $file, array_keys($dirtyFiles), $dirtyFiles,
);
$dirtyFiles = array_filter(
$dirtyFiles,
fn (string $file): bool => str_starts_with('.'.DIRECTORY_SEPARATOR.$file, TestSuite::getInstance()->testPath)
fn (string $file): bool => (
str_starts_with('.'.DIRECTORY_SEPARATOR.$file, TestSuite::getInstance()->testPath)
|| str_starts_with($file, TestSuite::getInstance()->testPath)
) && is_file($this->projectRoot.DIRECTORY_SEPARATOR.$file)
);
$dirtyFiles = array_values($dirtyFiles);
+9 -1
View File
@@ -2401,6 +2401,14 @@
PASS Tests\Visual\Collision
✓ collision with ([''])
PASS Tests\Visual\Dirty
✓ dirty selects paths with spaces and Unicode with dataset "repository root"
✓ dirty selects paths with spaces and Unicode with dataset "project subdirectory"
✓ dirty selects paths with spaces and Unicode with dataset "subdirectory with leading space"
✓ dirty ignores a test removed after staging a modification with dataset "sequential"
✓ dirty ignores a test removed after staging a modification with dataset "parallel"
✓ dirty selects an unresolved test only while its file exists
PASS Tests\Visual\FailingBoot
✓ a failure in the boot files renders only that failure
@@ -2537,4 +2545,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1839 passed (4203 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1845 passed (4231 assertions)
+76
View File
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
use Symfony\Component\Process\Process;
use Tests\Fixtures\Tia\GitRepo;
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('dirty selects paths with spaces and Unicode', function (string $directory): void {
$project = Project::make();
$root = $directory === '' ? $project->path() : $project->nested($directory);
$testDirectory = $directory === '' ? 'tests/Unit' : $directory.'/tests/Unit';
$project->git()->run(['config', 'core.quotePath', 'true']);
foreach (['RegularTest', 'Before Rename Test', 'UnchangedTest', 'DeletedTest'] as $file) {
$project->write($testDirectory.'/'.$file.'.php', "<?php\n\nit('runs $file')->assertTrue(true);\n");
}
$project->git()->commit('add dirty test fixtures');
$project->git()->run(['mv', $testDirectory.'/Before Rename Test.php', $testDirectory.'/After Rename Test.php']);
$project->write($testDirectory.'/RegularTest.php', "<?php\n\nit('runs modified test')->assertTrue(true);\n");
$project->write($testDirectory.'/Café Test.php', "<?php\n\nit('runs new test')->assertTrue(true);\n");
unlink($project->path($testDirectory.'/DeletedTest.php'));
$result = $project->pestIn($root, '--dirty');
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->output)->toContain('3 passed (3 assertions)', 'runs modified test', 'runs Before Rename Test', 'runs new test')
->not->toContain('runs UnchangedTest', 'runs DeletedTest');
})->with([
'repository root' => [''],
'project subdirectory' => ['backend'],
'subdirectory with leading space' => [' backend'],
])->skipOnWindows();
test('dirty ignores a test removed after staging a modification', function (array $arguments): void {
$project = Project::make();
$testFile = 'tests/Unit/GreeterTest.php';
$project->write($testFile, "<?php\n\nit('runs staged test')->assertTrue(true);\n");
$project->git()->run(['add', '--', $testFile]);
unlink($project->path($testFile));
$result = $project->pest('--dirty', '--fail-on-empty-test-suite', '--cache-directory', $project->path('.home/cache'), ...$arguments);
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->output)->toContain('No "dirty" tests found.');
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
test('dirty selects an unresolved test only while its file exists', function (): void {
$project = Project::make();
$testFile = 'tests/Unit/GreeterTest.php';
$project->git()->switchTo('modified', true);
$project->write($testFile, "<?php\n\nit('runs the unresolved test')->assertTrue(true);\n");
$project->git()->commit('modify the test');
$project->git()->switchTo('master');
unlink($project->path($testFile));
$project->git()->commit('remove the test');
$merge = new Process(['git', 'merge', '--no-edit', 'modified'], $project->path(), GitRepo::ENV);
expect($merge->run())->toBe(1);
$present = $project->pest('--dirty', '--fail-on-empty-test-suite');
expect($present->exitCode)->toBe(0, $present->describe())
->and($present->output)->toContain('1 passed (1 assertions)', 'runs the unresolved test');
unlink($project->path($testFile));
$missing = $project->pest('--dirty', '--fail-on-empty-test-suite');
expect($missing->exitCode)->toBe(0, $missing->describe())
->and($missing->output)->toContain('No "dirty" tests found.');
})->skipOnWindows();