From a07f68bc5467d913117bb9604477dc80a86e5ce4 Mon Sep 17 00:00:00 2001 From: Ikki Nishihara Date: Fri, 20 Nov 2026 06:03:19 +0900 Subject: [PATCH] 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 --- .../GitDirtyTestCaseFilter.php | 30 ++++---- tests/.snapshots/success.txt | 10 ++- tests/Visual/Dirty.php | 76 +++++++++++++++++++ 3 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 tests/Visual/Dirty.php diff --git a/src/TestCaseFilters/GitDirtyTestCaseFilter.php b/src/TestCaseFilters/GitDirtyTestCaseFilter.php index 87bbd4c9..2fa998d3 100644 --- a/src/TestCaseFilters/GitDirtyTestCaseFilter.php +++ b/src/TestCaseFilters/GitDirtyTestCaseFilter.php @@ -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); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9968b4ba..a34b6bb5 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -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) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1845 passed (4231 assertions) \ No newline at end of file diff --git a/tests/Visual/Dirty.php b/tests/Visual/Dirty.php new file mode 100644 index 00000000..987bb5af --- /dev/null +++ b/tests/Visual/Dirty.php @@ -0,0 +1,76 @@ +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', "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', "assertTrue(true);\n"); + $project->write($testDirectory.'/Café Test.php', "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, "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, "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();