*/ function tiaMonorepoRoots(?string $created = null): array { static $roots = []; if ($created !== null) { $roots[] = $created; return $roots; } $all = $roots; $roots = []; return $all; } /** * @return array{root: string, project: string, repo: GitRepo, sha: string} */ function tiaMonorepoRepository(): array { $root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8)); mkdir($root.'/backend/app', 0755, true); mkdir($root.'/frontend', 0755, true); file_put_contents($root.'/backend/app/Service.php', "init('master'); return ['root' => $root, 'project' => $root.'/backend', 'repo' => $repo, 'sha' => $repo->sha()]; } afterEach(function (): void { foreach (tiaMonorepoRoots() as $root) { new Process(['rm', '-rf', $root])->run(); } }); it('resolves the project prefix inside the repository', function (): void { $monorepo = tiaMonorepoRepository(); expect(new ChangedFiles($monorepo['project'])->gitPrefix())->toBe('backend/') ->and(new ChangedFiles($monorepo['root'])->gitPrefix())->toBeEmpty(); })->skipOnWindows(); it('keeps a space that begins the name of the project directory', function (): void { $root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8)); mkdir($root.'/ backend/app', 0755, true); file_put_contents($root.'/ backend/app/Service.php', "init('master'); file_put_contents($root.'/ backend/app/Service.php', "gitPrefix())->toBe(' backend/') ->and($project->since($repo->sha()))->toBe(['app/Service.php']); })->skipOnWindows(); it('keeps the character that ends the name of the repository root', function (string $lastCharacter): void { $root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8)).$lastCharacter; mkdir($root.'/backend/app', 0755, true); mkdir($root.'/frontend', 0755, true); file_put_contents($root.'/backend/app/Service.php', "init('master'); file_put_contents($root.'/backend/app/Service.php', "since($repo->sha()))->toBe(['app/Service.php']) ->and($project->outsideProject())->toBe(['frontend/widget.php']); })->with(['a space' => ' ', 'a carriage return' => "\r"])->skipOnWindows(); it('reports working tree changes relative to the project, dropping siblings', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['project'].'/app/Service.php', "since($monorepo['sha'])) ->toBe(['app/Service.php']); })->skipOnWindows(); it('reports committed changes relative to the project, dropping siblings', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['project'].'/app/Service.php', "commit('Second commit'); expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha'])) ->toBe(['app/Service.php']); })->skipOnWindows(); it('drops a file whose content came back to its baseline across the prefix boundary', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['project'].'/app/Service.php', "commit('Change the service'); file_put_contents($monorepo['project'].'/app/Service.php', "commit('Put the service back'); expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))->toBe([]); })->skipOnWindows(); it('honours a gitignore inside the project', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['project'].'/.gitignore', "/ignored.php\n"); file_put_contents($monorepo['project'].'/ignored.php', "since($monorepo['sha'])) ->toContain('app/Service.php') ->not->toContain('ignored.php'); })->skipOnWindows(); it('leaves a project at the repository root untouched', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['root'].'/frontend/widget.php', "since($monorepo['sha'])) ->toBe(['frontend/widget.php']); })->skipOnWindows(); it('reports the changes it left outside the project', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['project'].'/app/Service.php', "since($monorepo['sha']); expect($changedFiles->outsideProject())->toBe(['frontend/widget.php']); })->skipOnWindows(); it('reports nothing outside the project for a project at the repository root', function (): void { $monorepo = tiaMonorepoRepository(); file_put_contents($monorepo['root'].'/frontend/widget.php', "since($monorepo['sha']); expect($changedFiles->outsideProject())->toBeEmpty(); })->skipOnWindows(); it('ties a fingerprint to the project location inside the repository', function (): void { $monorepo = tiaMonorepoRepository(); mkdir($monorepo['root'].'/admin/app', 0755, true); file_put_contents($monorepo['root'].'/admin/app/Service.php', "commit('add a second project'); $nested = Fingerprint::compute($monorepo['project']); $sibling = Fingerprint::compute($monorepo['root'].'/admin'); $root = Fingerprint::compute($monorepo['root']); expect($nested['structural']['project_prefix'])->toBe('backend/') ->and($sibling['structural']['project_prefix'])->toBe('admin/') ->and($root['structural'])->not->toHaveKey('project_prefix') ->and(Fingerprint::structuralMatches($nested, $sibling))->toBeFalse() ->and(Fingerprint::structuralMatches($nested, $root))->toBeFalse() ->and(Fingerprint::structuralDrift($sibling, $nested))->toContain('project_prefix'); })->skipOnWindows(); it('sees a committed non-ascii path that git would otherwise quote', function (): void { $monorepo = tiaMonorepoRepository(); mkdir($monorepo['project'].'/resources/views', 0755, true); file_put_contents($monorepo['project'].'/resources/views/\u{26a1}orders.blade.php', "