From a9e5fbf9ea861bcf010b4fe4621f757e75da17eb Mon Sep 17 00:00:00 2001 From: Einar Hansen Date: Sun, 11 Oct 2026 16:20:23 +0200 Subject: [PATCH] test(tia): cover monorepo baseline and path regressions --- tests/.snapshots/success.txt | 49 ++++++- tests/Unit/Plugins/Tia/MonorepoBaselines.php | 121 ++++++++++++++++++ .../Unit/Plugins/Tia/MonorepoChangedFiles.php | 65 ++++++++++ tests/Visual/Parallel.php | 2 +- 4 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Plugins/Tia/MonorepoBaselines.php create mode 100644 tests/Unit/Plugins/Tia/MonorepoChangedFiles.php diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index f5ad410d2..42a1fd1ee 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1861,6 +1861,22 @@ ✓ addOutput → it uses singular form for single test file ✓ addOutput → it returns original exit code when shard is not set + PASS Tests\Unit\Plugins\Tia\ChangedFiles + ✓ it resolves the project prefix inside the repository + ✓ it reports working tree changes relative to the project, dropping siblings + ✓ it reports committed changes relative to the project, dropping siblings + ✓ it drops a file whose content came back to its baseline across the prefix boundary + ✓ it honours a gitignore inside the project + ✓ it leaves a project at the repository root untouched + ✓ it reports the changes it left outside the project + ✓ it reports nothing outside the project for a project at the repository root + ✓ it ties a fingerprint to the project location inside the repository + ✓ it sees a committed non-ascii path that git would otherwise quote + ✓ it sees a committed non-ascii path at the repository root too + ✓ 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 + PASS Tests\Unit\Plugins\Tia\ContentHash ✓ of() → it returns false when file does not exist ✓ of() → it hashes an existing file @@ -1949,6 +1965,36 @@ ✓ it detects an added or removed non-platform dependency ✓ it is independent of package ordering + PASS Tests\Unit\Plugins\Tia\MonorepoBaselines + ✓ it detects the baseline repository from a project subdirectory with ('github.com') + ✓ it detects the baseline repository from a project subdirectory with ('gitlab.com') + ✓ it uses the nearest repository for baseline detection with ('github.com') + ✓ it uses the nearest repository for baseline detection with ('gitlab.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "relative gitdir and commondir" / ('github.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "relative gitdir and commondir" / ('gitlab.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "relative gitdir and absolute commondir" / ('github.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "relative gitdir and absolute commondir" / ('gitlab.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "absolute gitdir and relative commondir" / ('github.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "absolute gitdir and relative commondir" / ('gitlab.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "absolute gitdir and commondir" / ('github.com') + ✓ it detects worktree baselines through relative and absolute git pointers with dataset "absolute gitdir and commondir" / ('gitlab.com') + ✓ it uses the submodule baseline repository instead of its parent remote with dataset "relative gitdir" / ('github.com') + ✓ it uses the submodule baseline repository instead of its parent remote with dataset "relative gitdir" / ('gitlab.com') + ✓ it uses the submodule baseline repository instead of its parent remote with dataset "absolute gitdir" / ('github.com') + ✓ it uses the submodule baseline repository instead of its parent remote with dataset "absolute gitdir" / ('gitlab.com') + ✓ it does not use the parent baseline remote when a nested git file is malformed with ('github.com') + ✓ it does not use the parent baseline remote when a nested git file is malformed with ('gitlab.com') + ✓ it keeps caches separate for sibling projects with the same name + ✓ it shares a nested project cache and fingerprint across worktrees + + PASS Tests\Unit\Plugins\Tia\MonorepoChangedFiles + ✓ it detects committed Unicode filenames with git path quoting enabled with dataset "repository root" + ✓ it detects committed Unicode filenames with git path quoting enabled with dataset "nested project" + ✓ it reports the old path when a file moves outside the project with dataset "staged move" + ✓ it reports the old path when a file moves outside the project with dataset "committed move" + ✓ it ignores cosmetic changes when reading a nested project baseline + ✓ it includes untracked files inside the project + PASS Tests\Unit\Plugins\Tia\Recorder ✓ activateLinkTracking() → it tracks tables without a coverage driver ✓ activateLinkTracking() → it tracks inertia components without a coverage driver @@ -1963,6 +2009,7 @@ PASS Tests\Unit\Plugins\Tia\Storage ✓ it uses a project-relative configured directory ✓ it uses an absolute configured directory + ✓ it keeps the state directory of a project at the repository root PASS Tests\Unit\Plugins\Tia\TableExtractor ✓ fromSql() → it extracts tables from plain DML @@ -2259,4 +2306,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, 35 skipped, 1600 passed (3489 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1641 passed (3541 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Tia/MonorepoBaselines.php b/tests/Unit/Plugins/Tia/MonorepoBaselines.php new file mode 100644 index 000000000..6db1b631e --- /dev/null +++ b/tests/Unit/Plugins/Tia/MonorepoBaselines.php @@ -0,0 +1,121 @@ +root = sys_get_temp_dir().'/pest-tia-monorepo-baselines-'.bin2hex(random_bytes(8)); + mkdir($this->root.'/apps/api', 0755, true); + file_put_contents($this->root.'/apps/api/composer.json', '{}'); + + $this->repo = new GitRepo($this->root); + $this->repo->init(); + $this->repo->addOrigin('git@github.com:acme/mono.git'); +}); + +afterEach(function (): void { + Storage::useDirectory(null); + new Process(['rm', '-rf', $this->root])->mustRun(); +}); + +it('detects the baseline repository from a project subdirectory', function (string $host): void { + $this->repo->setOriginUrl('git@'.$host.':acme/mono.git'); + + $remote = $host === 'github.com' + ? new GitHubRemote(new WatchPatterns) + : new GitLabRemote(new WatchPatterns); + + expect($remote->detect($this->root.'/apps/api'))->toBe('acme/mono'); +})->with(['github.com', 'gitlab.com'])->skipOnWindows(); + +it('uses the nearest repository for baseline detection', function (string $host): void { + $this->repo->setOriginUrl('git@'.$host.':acme/mono.git'); + $nested = new GitRepo($this->root.'/apps/api'); + $nested->init(); + $nested->addOrigin('git@'.$host.':acme/nested.git'); + + $remote = $host === 'github.com' + ? new GitHubRemote(new WatchPatterns) + : new GitLabRemote(new WatchPatterns); + + expect($remote->detect($nested->path))->toBe('acme/nested'); +})->with(['github.com', 'gitlab.com'])->skipOnWindows(); + +it('detects worktree baselines through relative and absolute git pointers', function (bool $absoluteGitDir, bool $absoluteCommonDir, string $host): void { + $this->repo->setOriginUrl('git@'.$host.':acme/mono.git'); + $worktree = $this->repo->worktree($this->root.'/worktree', 'feature'); + $gitDir = $this->root.'/.git/worktrees/worktree'; + + file_put_contents($worktree.'/.git', 'gitdir: '.($absoluteGitDir ? $gitDir : '../.git/worktrees/worktree')."\n"); + file_put_contents($gitDir.'/commondir', ($absoluteCommonDir ? $this->root.'/.git' : '../..')."\n"); + file_put_contents($gitDir.'/config', "[remote \"origin\"]\n\turl = git@".$host.":acme/wrong.git\n"); + + $remote = $host === 'github.com' + ? new GitHubRemote(new WatchPatterns) + : new GitLabRemote(new WatchPatterns); + + expect($remote->detect($worktree.'/apps/api'))->toBe('acme/mono'); +})->with([ + 'relative gitdir and commondir' => [false, false], + 'relative gitdir and absolute commondir' => [false, true], + 'absolute gitdir and relative commondir' => [true, false], + 'absolute gitdir and commondir' => [true, true], +])->with(['github.com', 'gitlab.com'])->skipOnWindows(); + +it('uses the submodule baseline repository instead of its parent remote', function (bool $absoluteGitDir, string $host): void { + $this->repo->setOriginUrl('git@'.$host.':acme/mono.git'); + mkdir($this->root.'/source/apps/api', 0755, true); + file_put_contents($this->root.'/source/apps/api/composer.json', '{}'); + $source = new GitRepo($this->root.'/source'); + $source->init(); + + $this->repo->run(['-c', 'protocol.file.allow=always', 'submodule', 'add', $source->path, 'packages/module']); + $submodule = new GitRepo($this->root.'/packages/module'); + $submodule->setOriginUrl('git@'.$host.':acme/module.git'); + $gitDir = $submodule->output(['rev-parse', '--absolute-git-dir']); + + file_put_contents($submodule->path.'/.git', 'gitdir: '.($absoluteGitDir ? $gitDir : '../../.git/modules/packages/module')."\n"); + + $remote = $host === 'github.com' + ? new GitHubRemote(new WatchPatterns) + : new GitLabRemote(new WatchPatterns); + + expect($remote->detect($submodule->path.'/apps/api'))->toBe('acme/module'); +})->with([ + 'relative gitdir' => false, + 'absolute gitdir' => true, +])->with(['github.com', 'gitlab.com'])->skipOnWindows(); + +it('does not use the parent baseline remote when a nested git file is malformed', function (string $host): void { + $this->repo->setOriginUrl('git@'.$host.':acme/mono.git'); + file_put_contents($this->root.'/apps/api/.git', "not a gitdir\n"); + + $remote = $host === 'github.com' + ? new GitHubRemote(new WatchPatterns) + : new GitLabRemote(new WatchPatterns); + + expect($remote->detect($this->root.'/apps/api'))->toBeNull(); +})->with(['github.com', 'gitlab.com'])->skipOnWindows(); + +it('keeps caches separate for sibling projects with the same name', function (): void { + mkdir($this->root.'/services/api', 0755, true); + + expect(Storage::tempDir($this->root.'/apps/api')) + ->not->toBe(Storage::tempDir($this->root.'/services/api')); +})->skipOnWindows(); + +it('shares a nested project cache and fingerprint across worktrees', function (): void { + $worktree = $this->repo->worktree($this->root.'/worktree', 'feature'); + + expect(Storage::tempDir($this->root.'/apps/api')) + ->toBe(Storage::tempDir($worktree.'/apps/api')) + ->and(Fingerprint::compute($this->root.'/apps/api')) + ->toBe(Fingerprint::compute($worktree.'/apps/api')); +})->skipOnWindows(); diff --git a/tests/Unit/Plugins/Tia/MonorepoChangedFiles.php b/tests/Unit/Plugins/Tia/MonorepoChangedFiles.php new file mode 100644 index 000000000..90cf0ebaf --- /dev/null +++ b/tests/Unit/Plugins/Tia/MonorepoChangedFiles.php @@ -0,0 +1,65 @@ +root = sys_get_temp_dir().'/pest-tia-monorepo-changes-'.bin2hex(random_bytes(8)); + mkdir($this->root.'/apps/api/app', 0755, true); + mkdir($this->root.'/apps/web', 0755, true); + file_put_contents($this->root.'/apps/api/app/Service.php', "repo = new GitRepo($this->root); + $this->repo->init(); + $this->sha = $this->repo->sha(); +}); + +afterEach(function (): void { + new Process(['rm', '-rf', $this->root])->mustRun(); +}); + +it('detects committed Unicode filenames with git path quoting enabled', function (string $directory): void { + $this->repo->config('core.quotePath', 'true'); + $project = $this->root.$directory; + file_put_contents($project.'/Ærlig.php', "repo->commit('Add a Unicode filename'); + $sha = $this->repo->sha(); + + file_put_contents($project.'/Ærlig.php', "repo->commit('Change the Unicode file'); + + expect(new ChangedFiles($project)->since($sha))->toBe(['Ærlig.php']); +})->with([ + 'repository root' => '', + 'nested project' => '/apps/api', +])->skipOnWindows(); + +it('reports the old path when a file moves outside the project', function (bool $committed): void { + $this->repo->run(['mv', 'apps/api/app/Service.php', 'apps/web/Service.php']); + + if ($committed) { + $this->repo->commit('Move the service into a sibling project'); + } + + expect(new ChangedFiles($this->root.'/apps/api')->since($this->sha))->toBe(['app/Service.php']); +})->with([ + 'staged move' => false, + 'committed move' => true, +])->skipOnWindows(); + +it('ignores cosmetic changes when reading a nested project baseline', function (): void { + file_put_contents($this->root.'/apps/api/app/Service.php', "repo->commit('Change whitespace'); + + expect(new ChangedFiles($this->root.'/apps/api')->since($this->sha))->toBe([]); +})->skipOnWindows(); + +it('includes untracked files inside the project', function (): void { + file_put_contents($this->root.'/apps/api/app/Fresh.php', "root.'/apps/web/Other.php', "root.'/apps/api')->since($this->sha))->toBe(['app/Fresh.php']); +})->skipOnWindows(); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 3837686fc..732fcfe1f 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -20,7 +20,7 @@ $output = $run('--exclude-group=integration'); $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1581 passed (3431 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1622 passed (3483 assertions)'; expect($output) ->toContain("Tests: {$expected}")