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

VcsDriverRegistry.test.ts

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