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

VcsDriverRegistry.test.ts

125 lines5.3 KB

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 const makeDiskBackedLayer = (rootDir: string, calls: string[]) =>
26 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: (input) =>
36 Effect.gen(function* () {
37 const fs = yield* FileSystem.FileSystem;
38 const path = yield* Path.Path;
39 const command = normalizeGitArgs(input.args).join(" ");
40 calls.push(command);
41 let repoDir: string | null = input.cwd;
42 while (repoDir !== null && !(yield* fs.exists(path.join(repoDir, ".git")))) {
43 repoDir = repoDir === rootDir ? null : path.dirname(repoDir);
44 }
45 if (repoDir === null) {
46 return {
47 ...processOutput(""),
48 exitCode: ChildProcessSpawner.ExitCode(128),
49 stderr: "fatal: not a git repository",
50 };
51 }
52 if (command === "rev-parse --is-inside-work-tree") return processOutput("true\n");
53 if (command === "rev-parse --show-toplevel") return processOutput(`${repoDir}\n`);
54 if (command === "rev-parse --git-common-dir") {
55 return processOutput(`${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`);
56 }
57 return processOutput("");
58 }).pipe(Effect.provide(NodeServices.layer), Effect.orDie),
59 }),
60 ),
61 );
62​
63 it.effect("detects again when a cached repository is removed from disk", () =>
64 Effect.gen(function* () {
65 const fs = yield* FileSystem.FileSystem;
66 const path = yield* Path.Path;
67 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
68 yield* fs.makeDirectory(path.join(repoDir, ".git"));
69 const calls: string[] = [];
70​
71 yield* Effect.gen(function* () {
72 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
73 assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
74​
75 yield* fs.remove(path.join(repoDir, ".git"), { recursive: true });
76​
77 assert.equal(yield* registry.detect({ cwd: repoDir }), null);
78 assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
79 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
80 }).pipe(Effect.provide(NodeServices.layer)),
81 );
82​
83 it.effect("detects again when git init creates a repository inside a cached one", () =>
84 Effect.gen(function* () {
85 const fs = yield* FileSystem.FileSystem;
86 const path = yield* Path.Path;
87 const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
88 const projectDir = path.join(parentDir, "project");
89 yield* fs.makeDirectory(path.join(parentDir, ".git"));
90 yield* fs.makeDirectory(projectDir);
91 const calls: string[] = [];
92​
93 yield* Effect.gen(function* () {
94 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
95 assert.equal((yield* registry.detect({ cwd: projectDir }))?.repository.rootPath, parentDir);
96​
97 yield* fs.makeDirectory(path.join(projectDir, ".git"));
98​
99 assert.equal(
100 (yield* registry.detect({ cwd: projectDir }))?.repository.rootPath,
101 projectDir,
102 );
103 }).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls)));
104 }).pipe(Effect.provide(NodeServices.layer)),
105 );
106​
107 it.effect("detects a repository created after a negative lookup", () =>
108 Effect.gen(function* () {
109 const fs = yield* FileSystem.FileSystem;
110 const path = yield* Path.Path;
111 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
112 const calls: string[] = [];
113​
114 yield* Effect.gen(function* () {
115 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
116 assert.equal(yield* registry.detect({ cwd: repoDir }), null);
117​
118 yield* fs.makeDirectory(path.join(repoDir, ".git"));
119​
120 assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
121 assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
122 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
123 }).pipe(Effect.provide(NodeServices.layer)),
124 );
125});