Open History to see the last commit
1import { assert, it, describe } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7import { ChildProcessSpawner } from "effect/process";
8
9import * as VcsProcess from "./VcsProcess.ts";
10import * as VcsProjectConfig from "./VcsProjectConfig.ts";
11import * as VcsDriverRegistry from "./VcsDriverRegistry.ts";
12
13const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
14 exitCode: ChildProcessSpawner.ExitCode(0),
15 stdout,
16 stderr: "",
17 stdoutTruncated: false,
18 stderrTruncated: false,
19});
20
21const normalizeGitArgs = (args: ReadonlyArray<string>): ReadonlyArray<string> =>
22 args[0] === "-C" && args.length >= 2 ? args.slice(2) : args;
23
24describe("VcsDriverRegistry", () => {
25 it.effect("routes directly by VCS driver kind for non-repository workflows", () => {
26 const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
27 Layer.provide(NodeServices.layer),
28 Layer.provide(
29 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
30 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
31 }),
32 ),
33 Layer.provide(
34 Layer.mock(VcsProcess.VcsProcess)({
35 run: () => Effect.succeed(processOutput("")),
36 }),
37 ),
38 );
39
40 return Effect.gen(function* () {
41 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
42 const driver = yield* registry.get("git");
43
44 assert.strictEqual(driver.capabilities.kind, "git");
45 }).pipe(Effect.provide(layer));
46 });
47
48 const makeDiskBackedLayer = (rootDir: string, calls: string[]) =>
49 Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
50 Layer.provide(NodeServices.layer),
51 Layer.provide(
52 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
53 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
54 }),
55 ),
56 Layer.provide(
57 Layer.mock(VcsProcess.VcsProcess)({
58 run: (input) =>
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const command = normalizeGitArgs(input.args).join(" ");
63 calls.push(command);
64 let repoDir: string | null = input.cwd;
65 while (repoDir !== null && !(yield* fs.exists(path.join(repoDir, ".git")))) {
66 repoDir = repoDir === rootDir ? null : path.dirname(repoDir);
67 }
68 if (repoDir === null) {
69 return {
70 ...processOutput(""),
71 exitCode: ChildProcessSpawner.ExitCode(128),
72 stderr: "fatal: not a git repository",
73 };
74 }
75 if (command === "rev-parse --is-inside-work-tree") return processOutput("true\n");
76 if (command === "rev-parse --show-toplevel") return processOutput(`${repoDir}\n`);
77 if (command === "rev-parse --git-common-dir") {
78 return processOutput(`${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`);
79 }
80 return processOutput("");
81 }).pipe(Effect.provide(NodeServices.layer), Effect.orDie),
82 }),
83 ),
84 );
85
86 it.effect("caches repository detection for repeated resolves in the same cwd and kind", () =>
87 Effect.gen(function* () {
88 const fs = yield* FileSystem.FileSystem;
89 const path = yield* Path.Path;
90 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
91 yield* fs.makeDirectory(path.join(repoDir, ".git"));
92 const calls: string[] = [];
93
94 yield* Effect.gen(function* () {
95 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
96 const first = yield* registry.resolve({ cwd: repoDir, requestedKind: "git" });
97 const second = yield* registry.resolve({ cwd: repoDir, requestedKind: "git" });
98
99 assert.equal(first.repository.rootPath, repoDir);
100 assert.equal(second.repository.rootPath, repoDir);
101 assert.deepStrictEqual(calls, [
102 "rev-parse --is-inside-work-tree",
103 "rev-parse --show-toplevel",
104 "rev-parse --git-common-dir",
105 ]);
106 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
107 }).pipe(Effect.provide(NodeServices.layer)),
108 );
109
110 it.effect("detects again when a cached repository is removed from disk", () =>
111 Effect.gen(function* () {
112 const fs = yield* FileSystem.FileSystem;
113 const path = yield* Path.Path;
114 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
115 yield* fs.makeDirectory(path.join(repoDir, ".git"));
116 const calls: string[] = [];
117
118 yield* Effect.gen(function* () {
119 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
120 assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
121
122 yield* fs.remove(path.join(repoDir, ".git"), { recursive: true });
123
124 assert.equal(yield* registry.detect({ cwd: repoDir }), null);
125 assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
126 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
127 }).pipe(Effect.provide(NodeServices.layer)),
128 );
129
130 it.effect("detects again when git init creates a repository inside a cached one", () =>
131 Effect.gen(function* () {
132 const fs = yield* FileSystem.FileSystem;
133 const path = yield* Path.Path;
134 const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
135 const projectDir = path.join(parentDir, "project");
136 yield* fs.makeDirectory(path.join(parentDir, ".git"));
137 yield* fs.makeDirectory(projectDir);
138 const calls: string[] = [];
139
140 yield* Effect.gen(function* () {
141 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
142 assert.equal((yield* registry.detect({ cwd: projectDir }))?.repository.rootPath, parentDir);
143
144 yield* fs.makeDirectory(path.join(projectDir, ".git"));
145
146 assert.equal(
147 (yield* registry.detect({ cwd: projectDir }))?.repository.rootPath,
148 projectDir,
149 );
150 }).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls)));
151 }).pipe(Effect.provide(NodeServices.layer)),
152 );
153
154 it.effect("detects again when git init creates a repository between the cwd and its root", () =>
155 Effect.gen(function* () {
156 const fs = yield* FileSystem.FileSystem;
157 const path = yield* Path.Path;
158 const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
159 const middleDir = path.join(parentDir, "a");
160 const cwd = path.join(middleDir, "b");
161 yield* fs.makeDirectory(path.join(parentDir, ".git"));
162 yield* fs.makeDirectory(cwd, { recursive: true });
163 const calls: string[] = [];
164
165 yield* Effect.gen(function* () {
166 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
167 assert.equal((yield* registry.detect({ cwd }))?.repository.rootPath, parentDir);
168
169 yield* fs.makeDirectory(path.join(middleDir, ".git"));
170
171 assert.equal((yield* registry.detect({ cwd }))?.repository.rootPath, middleDir);
172 }).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls)));
173 }).pipe(Effect.provide(NodeServices.layer)),
174 );
175
176 it.effect("detects a repository created after a negative lookup", () =>
177 Effect.gen(function* () {
178 const fs = yield* FileSystem.FileSystem;
179 const path = yield* Path.Path;
180 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
181 const calls: string[] = [];
182
183 yield* Effect.gen(function* () {
184 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
185 assert.equal(yield* registry.detect({ cwd: repoDir }), null);
186
187 yield* fs.makeDirectory(path.join(repoDir, ".git"));
188
189 assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
190 assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
191 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
192 }).pipe(Effect.provide(NodeServices.layer)),
193 );
194});