Open History to see the last commit
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";
9
10import * as VcsProcess from "./VcsProcess.ts";
11import * as VcsProjectConfig from "./VcsProjectConfig.ts";
12import * as VcsDriverRegistry from "./VcsDriverRegistry.ts";
13
14const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
15 exitCode: ChildProcessSpawner.ExitCode(0),
16 stdout,
17 stderr: "",
18 stdoutTruncated: false,
19 stderrTruncated: false,
20});
21
22const normalizeGitArgs = (args: ReadonlyArray<string>): ReadonlyArray<string> =>
23 args[0] === "-C" && args.length >= 2 ? args.slice(2) : args;
24
25const DETECT_COMMAND = "rev-parse --is-inside-work-tree --show-toplevel --git-common-dir";
26
27describe("VcsDriverRegistry", () => {
28 const makeDiskBackedLayer = (rootDir: string, calls: string[]) =>
29 Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
30 Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
31 Layer.provide(
32 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
33 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
34 }),
35 ),
36 Layer.provide(
37 Layer.mock(VcsProcess.VcsProcess)({
38 run: (input) =>
39 Effect.gen(function* () {
40 const fs = yield* FileSystem.FileSystem;
41 const path = yield* Path.Path;
42 const command = normalizeGitArgs(input.args).join(" ");
43 calls.push(command);
44 let repoDir: string | null = input.cwd;
45 while (repoDir !== null && !(yield* fs.exists(path.join(repoDir, ".git")))) {
46 repoDir = repoDir === rootDir ? null : path.dirname(repoDir);
47 }
48 if (repoDir === null) {
49 return {
50 ...processOutput(""),
51 exitCode: ChildProcessSpawner.ExitCode(128),
52 stderr: "fatal: not a git repository",
53 };
54 }
55 if (command === DETECT_COMMAND) {
56 return processOutput(
57 `true\n${repoDir}\n${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`,
58 );
59 }
60 return processOutput("");
61 }).pipe(
62 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
63 Effect.orDie,
64 ),
65 }),
66 ),
67 );
68
69 it.effect("detects again when a cached repository is removed from disk", () =>
70 Effect.gen(function* () {
71 const fs = yield* FileSystem.FileSystem;
72 const path = yield* Path.Path;
73 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
74 yield* fs.makeDirectory(path.join(repoDir, ".git"));
75 const calls: string[] = [];
76
77 yield* Effect.gen(function* () {
78 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
79 assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
80
81 yield* fs.remove(path.join(repoDir, ".git"), { recursive: true });
82
83 assert.equal(yield* registry.detect({ cwd: repoDir }), null);
84 assert.equal(calls.filter((call) => call === DETECT_COMMAND).length, 2);
85 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
86 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
87 );
88
89 it.effect("detects again when git init creates a repository inside a cached one", () =>
90 Effect.gen(function* () {
91 const fs = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
94 const projectDir = path.join(parentDir, "project");
95 yield* fs.makeDirectory(path.join(parentDir, ".git"));
96 yield* fs.makeDirectory(projectDir);
97 const calls: string[] = [];
98
99 yield* Effect.gen(function* () {
100 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
101 assert.equal((yield* registry.detect({ cwd: projectDir }))?.repository.rootPath, parentDir);
102
103 yield* fs.makeDirectory(path.join(projectDir, ".git"));
104
105 assert.equal(
106 (yield* registry.detect({ cwd: projectDir }))?.repository.rootPath,
107 projectDir,
108 );
109 }).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls)));
110 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
111 );
112
113 it.effect("detects a repository created after a negative lookup", () =>
114 Effect.gen(function* () {
115 const fs = yield* FileSystem.FileSystem;
116 const path = yield* Path.Path;
117 const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
118 const calls: string[] = [];
119
120 yield* Effect.gen(function* () {
121 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
122 assert.equal(yield* registry.detect({ cwd: repoDir }), null);
123
124 yield* fs.makeDirectory(path.join(repoDir, ".git"));
125
126 assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
127 assert.equal(calls.filter((call) => call === DETECT_COMMAND).length, 2);
128 }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
129 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
130 );
131});