From d314fc0540ede976a371ce67f085e7bae79f7800 Mon Sep 17 00:00:00 2001 From: Jhon Farias Date: Fri, 27 Nov 2026 14:00:39 +0100 Subject: [PATCH] fix(tia): keep numeric file, branch and page names as strings (#2066) Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> Co-authored-by: Punyapal Shah --- src/Plugins/Tia.php | 3 +- src/Plugins/Tia/ChangedFiles.php | 13 ++-- src/Plugins/Tia/Graph.php | 17 +++--- src/Plugins/Tia/Recorder.php | 3 +- src/Plugins/Tia/WatchPatterns.php | 9 +-- src/Support/Arr.php | 9 +++ tests/.snapshots/success.txt | 23 ++++++- tests/Unit/Plugins/Tia/ChangedFiles.php | 41 +++++++++++++ tests/Unit/Plugins/Tia/Graph.php | 26 ++++++++ .../Unit/Plugins/Tia/NumericInertiaPages.php | 61 +++++++++++++++++++ .../Plugins/Tia/NumericSourceFileNames.php | 32 ++++++++++ .../Unit/Plugins/Tia/NumericWatchPatterns.php | 23 +++++++ tests/Unit/Support/Arr.php | 6 ++ tests/Visual/Parallel.php | 2 +- 14 files changed, 246 insertions(+), 22 deletions(-) create mode 100644 tests/Unit/Plugins/Tia/NumericInertiaPages.php create mode 100644 tests/Unit/Plugins/Tia/NumericSourceFileNames.php create mode 100644 tests/Unit/Plugins/Tia/NumericWatchPatterns.php diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index b7a2e6b8..62396dd3 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -36,6 +36,7 @@ use Pest\Plugins\Tia\SourceScope; use Pest\Plugins\Tia\Storage; use Pest\Plugins\Tia\TableExtractor; use Pest\Plugins\Tia\WatchPatterns; +use Pest\Support\Arr; use Pest\Support\Ci; use Pest\Support\Container; use Pest\Support\Git; @@ -1765,7 +1766,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument return [ array_map(array_keys(...), $merged['files']), array_map(static fn (array $tables): array => array_map(strval(...), array_keys($tables)), $merged['tables']), - array_map(array_keys(...), $merged['inertia']), + array_map(Arr::stringKeys(...), $merged['inertia']), ]; } diff --git a/src/Plugins/Tia/ChangedFiles.php b/src/Plugins/Tia/ChangedFiles.php index 30cc4e2e..8d6f77bf 100644 --- a/src/Plugins/Tia/ChangedFiles.php +++ b/src/Plugins/Tia/ChangedFiles.php @@ -6,6 +6,7 @@ namespace Pest\Plugins\Tia; use Pest\Exceptions\MissingDependency; use Pest\Exceptions\TiaGitFailed; +use Pest\Support\Arr; use Pest\Support\Git; use Symfony\Component\Process\ExecutableFinder; @@ -78,7 +79,7 @@ final class ChangedFiles */ public function outsideProject(): array { - return array_map(strval(...), array_keys($this->outsideProject)); + return Arr::stringKeys($this->outsideProject); } /** @@ -86,7 +87,7 @@ final class ChangedFiles */ public function outsideProjectDirty(): array { - return array_map(strval(...), array_keys($this->outsideProjectDirty)); + return Arr::stringKeys($this->outsideProjectDirty); } /** @@ -102,13 +103,13 @@ final class ChangedFiles $candidates = array_fill_keys($files, true); - foreach (array_keys($lastRunTree) as $snapshotted) { + foreach (Arr::stringKeys($lastRunTree) as $snapshotted) { $candidates[$snapshotted] = true; } $remaining = []; - foreach (array_keys($candidates) as $file) { + foreach (Arr::stringKeys($candidates) as $file) { $snapshot = $lastRunTree[$file] ?? null; $current = $this->currentHash($file); @@ -189,7 +190,7 @@ final class ChangedFiles $unique[$file] = true; } - $candidates = array_keys($this->filterIgnored($unique)); + $candidates = Arr::stringKeys($this->filterIgnored($unique)); $this->filterIgnoredOutsideProject(); @@ -374,7 +375,7 @@ final class ChangedFiles } } - return array_keys($names); + return Arr::stringKeys($names); } public function hasRemote(): bool diff --git a/src/Plugins/Tia/Graph.php b/src/Plugins/Tia/Graph.php index 401b7803..77ffa717 100644 --- a/src/Plugins/Tia/Graph.php +++ b/src/Plugins/Tia/Graph.php @@ -7,6 +7,7 @@ namespace Pest\Plugins\Tia; use JsonException; use Pest\Factories\TestCaseFactory; use Pest\Factories\TestCaseMethodFactory; +use Pest\Support\Arr; use Pest\Support\Container; use Pest\Support\View; use Pest\TestSuite; @@ -594,7 +595,7 @@ final class Graph { $generated = []; - foreach ($this->fileIds as $path => $id) { + foreach ($this->files as $id => $path) { $hash = $this->livewireGeneratedHash($path); if ($hash === null) { @@ -1131,7 +1132,7 @@ final class Graph } } - $names = array_keys($normalised); + $names = Arr::stringKeys($normalised); sort($names); $this->testInertiaComponents[$testRel] = $names; @@ -1161,7 +1162,7 @@ final class Graph continue; } - $keys = array_keys($names); + $keys = Arr::stringKeys($names); sort($keys); $out[$path] = $keys; } @@ -1645,7 +1646,7 @@ final class Graph { $survivors = array_fill_keys($keep, true); - foreach (array_keys($this->baselines) as $branch) { + foreach (Arr::stringKeys($this->baselines) as $branch) { if (str_starts_with($branch, self::WORKSPACE_BRANCH_PREFIX)) { continue; } @@ -1711,8 +1712,8 @@ final class Graph $branches = []; - foreach (array_keys($data['baselines']) as $branch) { - if (is_string($branch) && $branch !== '') { + foreach (Arr::stringKeys($data['baselines']) as $branch) { + if ($branch !== '') { $branches[] = $branch; } } @@ -1847,8 +1848,8 @@ final class Graph if (is_array($baseline['tree'] ?? null)) { foreach ($baseline['tree'] as $path => $hash) { - if (is_string($path) && is_string($hash)) { - $tree[$path] = $hash; + if (is_string($hash)) { + $tree[(string) $path] = $hash; } } } diff --git a/src/Plugins/Tia/Recorder.php b/src/Plugins/Tia/Recorder.php index 7a7d8c69..9a9a3ff9 100644 --- a/src/Plugins/Tia/Recorder.php +++ b/src/Plugins/Tia/Recorder.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Pest\Plugins\Tia; +use Pest\Support\Arr; use Pest\Support\Container; use Pest\TestSuite; use ReflectionClass; @@ -329,7 +330,7 @@ final class Recorder $out = []; foreach ($this->perTestInertiaComponents as $testFile => $components) { - $names = array_keys($components); + $names = Arr::stringKeys($components); sort($names); $out[$testFile] = $names; } diff --git a/src/Plugins/Tia/WatchPatterns.php b/src/Plugins/Tia/WatchPatterns.php index c1d32579..7282a9d3 100644 --- a/src/Plugins/Tia/WatchPatterns.php +++ b/src/Plugins/Tia/WatchPatterns.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest\Plugins\Tia; use Pest\Plugins\Tia\Contracts\WatchDefault; +use Pest\Support\Arr; use Pest\TestSuite; /** @@ -173,7 +174,7 @@ final class WatchPatterns } } - return array_keys($matched); + return Arr::stringKeys($matched); } /** @@ -184,17 +185,17 @@ final class WatchPatterns { $matched = []; - foreach ($patterns as $key => $dirs) { + foreach (Arr::stringKeys($patterns) as $key) { if (! $this->keyMatches($key, $file)) { continue; } - foreach ($dirs as $dir) { + foreach ($patterns[$key] as $dir) { $matched[$dir] = true; } } - return array_keys($matched); + return Arr::stringKeys($matched); } private function usesDefaultsFor(string $file): bool diff --git a/src/Support/Arr.php b/src/Support/Arr.php index c30d7757..590f6e7c 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -9,6 +9,15 @@ namespace Pest\Support; */ final class Arr { + /** + * @param array $array + * @return list + */ + public static function stringKeys(array $array): array + { + return array_map(strval(...), array_keys($array)); + } + /** * @param array $array */ diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 5450a1ca..f4563201 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2002,6 +2002,10 @@ ✓ it keeps a tracked outside change and drops an untracked ignored one ✓ it keeps a tracked project change that the ignore rules match ✓ it reads committed paths from the repository root even when git is told to be relative + ✓ it keeps a changed file whose name is a number as a string + ✓ it keeps a changed file outside the project whose name is a number as a string + ✓ it keeps a snapshotted file whose name is a number as a string + ✓ it keeps a branch whose name is a number as a string PASS Tests\Unit\Plugins\Tia\ConfigurationFile ✓ it fingerprints the path and the content of the configuration file @@ -2111,6 +2115,8 @@ ✓ Laravel boot files → it watches the config and bootstrap files in the Laravel defaults ✓ clearResults() → it drops the results of the fallback branch too ✓ branch baseline over the fallback → it reads results written after an earlier read + ✓ pruneMissingBranches() → it keeps a branch whose name is a number + ✓ decode() → it keeps a file of the last run tree whose name is a number ✓ encode() and decode() → it round-trips paths that are not valid UTF-8 ✓ encode() and decode() → it lists a branch whose name is not valid UTF-8 ✓ legacy schema → it reads a graph of the previous schema without unescaping its strings @@ -2142,12 +2148,26 @@ ✓ it detects an added or removed non-platform dependency ✓ it is independent of package ordering + PASS Tests\Unit\Plugins\Tia\NumericInertiaPages + ✓ it selects the tests of a changed Inertia page whose name is a number + ✓ it keeps an Inertia page whose name is a number after the graph is decoded + ✓ it records an Inertia page whose name is a number as a string + ✓ it selects the tests of a shared module of an Inertia page whose name is a number + + PASS Tests\Unit\Plugins\Tia\NumericSourceFileNames + ✓ it selects the tests of a changed source file whose name is a number + ✓ it selects the tests of a numeric source file after the graph is decoded + PASS Tests\Unit\Plugins\Tia\NumericTableDependencies ✓ it exports numeric table dependencies from the recorder as strings ✓ it persists numeric tables as JSON strings and selects only their migration readers after reload ✓ it preserves numeric worker tables through payload cleaning union and graph reload ✓ it preserves observed and migration added numeric tables when augmenting database tests + PASS Tests\Unit\Plugins\Tia\NumericWatchPatterns + ✓ it matches a watch pattern whose key is a number + ✓ it selects the tests under a watch target whose name is a number + PASS Tests\Unit\Plugins\Tia\ReadCapture ✓ reads → it links a project file the test reads with dataset "file_get_contents" ✓ reads → it links a project file the test reads with dataset "file" @@ -2416,6 +2436,7 @@ ✓ last → it should return the last element for an array without changing the internal pointer ✓ last → it should return the last element for an associative array without changing the internal pointer ✓ last → it should return the last element for an mixed key array without changing the internal pointer + ✓ stringKeys → it returns every key as a string PASS Tests\Unit\Support\Backtrace ✓ it gets file name from called file @@ -2721,4 +2742,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, 39 skipped, 2004 passed (4509 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 2019 passed (4528 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Tia/ChangedFiles.php b/tests/Unit/Plugins/Tia/ChangedFiles.php index 0016a9f6..7d329627 100644 --- a/tests/Unit/Plugins/Tia/ChangedFiles.php +++ b/tests/Unit/Plugins/Tia/ChangedFiles.php @@ -6,6 +6,7 @@ use Pest\Plugins\Tia\ChangedFiles; use Pest\Plugins\Tia\Fingerprint; use Symfony\Component\Process\Process; use Tests\Fixtures\Tia\GitRepo; +use Tests\Fixtures\Tia\Project; /** * @return array @@ -51,6 +52,8 @@ 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 { @@ -285,3 +288,41 @@ it('reads committed paths from the repository root even when git is told to be r expect($changedFiles->since($monorepo['sha']))->toBe(['app/Service.php']) ->and($changedFiles->outsideProject())->toBe(['frontend/widget.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('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']); +}); diff --git a/tests/Unit/Plugins/Tia/Graph.php b/tests/Unit/Plugins/Tia/Graph.php index 4ea941f9..148bdbe1 100644 --- a/tests/Unit/Plugins/Tia/Graph.php +++ b/tests/Unit/Plugins/Tia/Graph.php @@ -410,6 +410,32 @@ describe('branch baseline over the fallback', function (): void { }); }); +describe('pruneMissingBranches()', function (): void { + it('keeps a branch whose name is a number', function (): void { + $graph = new Graph(sys_get_temp_dir()); + $graph->setRecordedAtSha('123', 'abc'); + $graph->setRecordedAtSha('gone', 'def'); + + $graph->pruneMissingBranches(['123']); + + expect($graph->recordedAtSha('123'))->toBe('abc') + ->and($graph->recordedAtSha('gone'))->toBeNull() + ->and(Graph::branchesIn($graph->encode()))->toBe(['123']); + }); +}); + +describe('decode()', function (): void { + it('keeps a file of the last run tree whose name is a number', function (): void { + $graph = new Graph(sys_get_temp_dir()); + $graph->setRecordedAtSha('master', 'abc'); + $graph->setLastRunTree('master', ['123' => 'hash']); + + $decoded = Graph::decode($graph->encode(), sys_get_temp_dir()); + + expect($decoded?->lastRunTree('master'))->toBe([123 => 'hash']); + }); +}); + describe('encode() and decode()', function (): void { it('round-trips paths that are not valid UTF-8', function (): void { $root = sys_get_temp_dir(); diff --git a/tests/Unit/Plugins/Tia/NumericInertiaPages.php b/tests/Unit/Plugins/Tia/NumericInertiaPages.php new file mode 100644 index 00000000..4cc9456c --- /dev/null +++ b/tests/Unit/Plugins/Tia/NumericInertiaPages.php @@ -0,0 +1,61 @@ +add(WatchPatterns::class, new WatchPatterns); +}); + +afterEach(function (): void { + Container::getInstance()->add(WatchPatterns::class, new WatchPatterns); +}); + +it('selects the tests of a changed Inertia page whose name is a number', function (): void { + $root = (string) realpath(sys_get_temp_dir()); + $graph = new Graph($root); + $graph->replaceTestInertiaComponents([ + $root.'/tests/Feature/NumericPageTest.php' => ['123'], + $root.'/tests/Feature/DashboardTest.php' => ['Dashboard'], + ]); + + expect($graph->affected(['resources/js/Pages/123.vue']))->toBe(['tests/Feature/NumericPageTest.php']); +}); + +it('keeps an Inertia page whose name is a number after the graph is decoded', function (): void { + $root = (string) realpath(sys_get_temp_dir()); + $graph = new Graph($root); + $graph->replaceTestInertiaComponents([ + $root.'/tests/Feature/NumericPageTest.php' => ['123'], + ]); + + $decoded = Graph::decode($graph->encode(), $root); + + expect($decoded?->affected(['resources/js/Pages/123.vue']))->toBe(['tests/Feature/NumericPageTest.php']); +}); + +it('records an Inertia page whose name is a number as a string', function (): void { + $recorder = new Recorder; + $recorder->activateLinkTracking(); + + $recorder->beginTest('Some\Missing\TestClass', 'it renders', '/project/tests/Feature/NumericPageTest.php'); + $recorder->linkInertiaComponent('123'); + $recorder->endTest(); + + expect($recorder->perTestInertiaComponents())->toBe(['/project/tests/Feature/NumericPageTest.php' => ['123']]); +}); + +it('selects the tests of a shared module of an Inertia page whose name is a number', function (): void { + $root = (string) realpath(sys_get_temp_dir()); + $graph = new Graph($root); + $graph->replaceTestInertiaComponents([ + $root.'/tests/Feature/NumericPageTest.php' => ['123'], + ]); + $graph->replaceJsFileToComponents(['resources/js/Components/Chart.vue' => ['123']]); + + expect($graph->affected(['resources/js/Components/Chart.vue']))->toBe(['tests/Feature/NumericPageTest.php']); +}); diff --git a/tests/Unit/Plugins/Tia/NumericSourceFileNames.php b/tests/Unit/Plugins/Tia/NumericSourceFileNames.php new file mode 100644 index 00000000..dbb71be7 --- /dev/null +++ b/tests/Unit/Plugins/Tia/NumericSourceFileNames.php @@ -0,0 +1,32 @@ +add(WatchPatterns::class, new WatchPatterns); +}); + +afterEach(function (): void { + Container::getInstance()->add(WatchPatterns::class, new WatchPatterns); +}); + +it('selects the tests of a changed source file whose name is a number', function (): void { + $graph = new Graph(sys_get_temp_dir()); + $graph->link('tests/Feature/NumericTest.php', '123'); + $graph->link('tests/Feature/OtherTest.php', 'app/Other.php'); + + expect($graph->affected(['123']))->toBe(['tests/Feature/NumericTest.php']); +}); + +it('selects the tests of a numeric source file after the graph is decoded', function (): void { + $graph = new Graph(sys_get_temp_dir()); + $graph->link('tests/Feature/NumericTest.php', '123'); + + $decoded = Graph::decode($graph->encode(), sys_get_temp_dir()); + + expect($decoded?->affected(['123']))->toBe(['tests/Feature/NumericTest.php']); +}); diff --git a/tests/Unit/Plugins/Tia/NumericWatchPatterns.php b/tests/Unit/Plugins/Tia/NumericWatchPatterns.php new file mode 100644 index 00000000..20699eba --- /dev/null +++ b/tests/Unit/Plugins/Tia/NumericWatchPatterns.php @@ -0,0 +1,23 @@ +add(['123' => 'tests/Feature']); + + expect($watchPatterns->matchedDirectories(sys_get_temp_dir(), ['123']))->toBe(['tests/Feature']); +}); + +it('selects the tests under a watch target whose name is a number', function (): void { + $watchPatterns = new WatchPatterns; + $watchPatterns->add(['config.txt' => '123']); + + $directories = $watchPatterns->matchedDirectories(sys_get_temp_dir(), ['config.txt']); + + expect($directories)->toBe(['123']) + ->and($watchPatterns->testsUnderDirectories($directories, ['123/ConfigTest.php', 'tests/OtherTest.php'])) + ->toBe(['123/ConfigTest.php']); +}); diff --git a/tests/Unit/Support/Arr.php b/tests/Unit/Support/Arr.php index c4667a1c..f38e3058 100644 --- a/tests/Unit/Support/Arr.php +++ b/tests/Unit/Support/Arr.php @@ -49,3 +49,9 @@ describe('last', function (): void { ->and(current($array))->toBe(2); }); }); + +describe('stringKeys', function (): void { + it('returns every key as a string', function (): void { + expect(Arr::stringKeys(['123' => true, 'app/Service.php' => true]))->toBe(['123', 'app/Service.php']); + }); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 813ba58f..36361423 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1873 passed (3979 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1888 passed (3998 assertions)'; expect($output) ->toContain("Tests: {$expected}")