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 Layer from "effect/Layer";
5import { ChildProcessSpawner } from "effect/unstable/process";
6
7import * as VcsProcess from "./VcsProcess.ts";
8import * as VcsProjectConfig from "./VcsProjectConfig.ts";
9import * as VcsDriverRegistry from "./VcsDriverRegistry.ts";
10
11const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
12 exitCode: ChildProcessSpawner.ExitCode(0),
13 stdout,
14 stderr: "",
15 stdoutTruncated: false,
16 stderrTruncated: false,
17});
18
19const normalizeGitArgs = (args: ReadonlyArray<string>): ReadonlyArray<string> =>
20 args[0] === "-C" && args.length >= 2 ? args.slice(2) : args;
21
22describe("VcsDriverRegistry", () => {
23 it.effect("routes directly by VCS driver kind for non-repository workflows", () => {
24 const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
25 Layer.provide(NodeServices.layer),
26 Layer.provide(
27 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
28 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
29 }),
30 ),
31 Layer.provide(
32 Layer.mock(VcsProcess.VcsProcess)({
33 run: () => Effect.succeed(processOutput("")),
34 }),
35 ),
36 );
37
38 return Effect.gen(function* () {
39 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
40 const driver = yield* registry.get("git");
41
42 assert.strictEqual(driver.capabilities.kind, "git");
43 }).pipe(Effect.provide(layer));
44 });
45
46 it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => {
47 const calls: VcsProcess.VcsProcessInput[] = [];
48 const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
49 Layer.provide(NodeServices.layer),
50 Layer.provide(
51 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
52 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
53 }),
54 ),
55 Layer.provide(
56 Layer.mock(VcsProcess.VcsProcess)({
57 run: (input) =>
58 Effect.sync(() => {
59 calls.push(input);
60 const normalizedArgs =
61 input.args[0] === "-C" && input.args.length >= 2 ? input.args.slice(2) : input.args;
62 const command = normalizedArgs.join(" ");
63 if (command === "rev-parse --is-inside-work-tree") {
64 return processOutput("true\n");
65 }
66 if (command === "rev-parse --show-toplevel") {
67 return processOutput("/repo\n");
68 }
69 if (command === "rev-parse --git-common-dir") {
70 return processOutput("/repo/.git\n");
71 }
72 return processOutput("");
73 }),
74 }),
75 ),
76 );
77
78 return Effect.gen(function* () {
79 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
80 const first = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
81 const second = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
82
83 assert.equal(first.repository.rootPath, "/repo");
84 assert.equal(second.repository.rootPath, "/repo");
85 assert.deepStrictEqual(
86 calls.map((call) => normalizeGitArgs(call.args).join(" ")),
87 [
88 "rev-parse --is-inside-work-tree",
89 "rev-parse --show-toplevel",
90 "rev-parse --git-common-dir",
91 ],
92 );
93 }).pipe(Effect.provide(layer));
94 });
95
96 it.effect("detects a repository created after a negative lookup", () => {
97 let insideWorkTreeChecks = 0;
98 const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
99 Layer.provide(NodeServices.layer),
100 Layer.provide(
101 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
102 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
103 }),
104 ),
105 Layer.provide(
106 Layer.mock(VcsProcess.VcsProcess)({
107 run: (input) =>
108 Effect.sync(() => {
109 const command = normalizeGitArgs(input.args).join(" ");
110 if (command === "rev-parse --is-inside-work-tree") {
111 insideWorkTreeChecks += 1;
112 return insideWorkTreeChecks === 1
113 ? {
114 ...processOutput(""),
115 exitCode: ChildProcessSpawner.ExitCode(128),
116 stderr: "fatal: not a git repository",
117 }
118 : processOutput("true\n");
119 }
120 if (command === "rev-parse --show-toplevel") {
121 return processOutput("/repo\n");
122 }
123 if (command === "rev-parse --git-common-dir") {
124 return processOutput("/repo/.git\n");
125 }
126 return processOutput("");
127 }),
128 }),
129 ),
130 );
131
132 return Effect.gen(function* () {
133 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
134
135 assert.equal(yield* registry.detect({ cwd: "/repo" }), null);
136 assert.equal((yield* registry.detect({ cwd: "/repo" }))?.repository.rootPath, "/repo");
137 assert.equal(insideWorkTreeChecks, 2);
138 }).pipe(Effect.provide(layer));
139 });
140});