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 <mrpunyapal@gmail.com>
This commit is contained in:
Jhon Farias
2026-11-27 14:00:39 +01:00
committed by GitHub
parent 093ca57693
commit d314fc0540
14 changed files with 246 additions and 22 deletions
+2 -1
View File
@@ -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']),
];
}
+7 -6
View File
@@ -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
+9 -8
View File
@@ -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;
}
}
}
+2 -1
View File
@@ -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;
}
+5 -4
View File
@@ -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
+9
View File
@@ -9,6 +9,15 @@ namespace Pest\Support;
*/
final class Arr
{
/**
* @param array<array-key, mixed> $array
* @return list<string>
*/
public static function stringKeys(array $array): array
{
return array_map(strval(...), array_keys($array));
}
/**
* @param array<array-key, mixed> $array
*/
+22 -1
View File
@@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 2019 passed (4528 assertions)
+41
View File
@@ -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<int, string>
@@ -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']);
});
+26
View File
@@ -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();
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
use Pest\Plugins\Tia\Graph;
use Pest\Plugins\Tia\Recorder;
use Pest\Plugins\Tia\WatchPatterns;
use Pest\Support\Container;
beforeEach(function (): void {
Container::getInstance()->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']);
});
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
use Pest\Plugins\Tia\Graph;
use Pest\Plugins\Tia\WatchPatterns;
use Pest\Support\Container;
beforeEach(function (): void {
Container::getInstance()->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']);
});
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
use Pest\Plugins\Tia\WatchPatterns;
it('matches a watch pattern whose key is a number', function (): void {
$watchPatterns = new WatchPatterns;
$watchPatterns->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']);
});
+6
View File
@@ -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']);
});
});
+1 -1
View File
@@ -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}")