Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

VcsDriverRegistry.test.ts

185 lines7.7 KB

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