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

GitVcsDriver.test.ts

178 lines6.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Path from "effect/Path";
6import * as PlatformError from "effect/PlatformError";
7import { ChildProcessSpawner } from "effect/unstable/process";
8import { assert, it } from "@effect/vitest";
9​
10import { CheckpointRef, GitCommandError } from "@t3tools/contracts";
11import * as ServerConfig from "../config.ts";
12import * as GitVcsDriver from "./GitVcsDriver.ts";
13import * as VcsProcess from "./VcsProcess.ts";
14import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
15​
16const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
17 prefix: "t3-git-vcs-contract-",
18});
19const GitContractLayer = Layer.mergeAll(GitVcsDriver.vcsLayer, GitVcsDriver.layer).pipe(
20 Layer.provide(ServerConfigLayer),
21 Layer.provideMerge(VcsProcess.layer),
22 Layer.provideMerge(NodeServices.layer),
23);
24​
25const runGit = (cwd: string, args: ReadonlyArray<string>) =>
26 Effect.gen(function* () {
27 const driver = yield* GitVcsDriver.GitVcsDriver;
28 yield* driver.execute({
29 operation: "GitVcsDriver.contract.git",
30 cwd,
31 args,
32 timeoutMs: 10_000,
33 });
34 });
35​
36type GitContractError = GitCommandError | PlatformError.PlatformError;
37​
38runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
39 name: "Git",
40 kind: "git",
41 layer: GitContractLayer,
42 fixture: {
43 createRepo: (cwd) =>
44 Effect.gen(function* () {
45 yield* runGit(cwd, ["init"]);
46 yield* runGit(cwd, ["config", "user.email", "test@test.com"]);
47 yield* runGit(cwd, ["config", "user.name", "Test"]);
48 }),
49 writeFile: (cwd, relativePath, contents) =>
50 Effect.gen(function* () {
51 const fileSystem = yield* FileSystem.FileSystem;
52 const path = yield* Path.Path;
53 const absolutePath = path.join(cwd, relativePath);
54 yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
55 yield* fileSystem.writeFileString(absolutePath, contents);
56 }),
57 trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
58 commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
59 ignorePath: (cwd, pattern) =>
60 Effect.gen(function* () {
61 const fileSystem = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
64 }),
65 },
66});
67​
68it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
69 Effect.gen(function* () {
70 const fileSystem = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 const driver = yield* GitVcsDriver.makeVcsDriverShape();
73 for (const nested of [false, true]) {
74 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-empty-checkpoint-" });
75 yield* runGit(root, ["init"]);
76 yield* runGit(root, ["config", "user.email", "test@test.com"]);
77 yield* runGit(root, ["config", "user.name", "Test"]);
78 if (nested) {
79 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "original\n");
80 yield* runGit(root, ["add", "."]);
81 }
82 yield* runGit(root, ["commit", "--allow-empty", "-m", "initial"]);
83 const cwd = nested ? path.join(root, "nested") : root;
84 yield* fileSystem.makeDirectory(cwd, { recursive: true });
85 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/empty");
86 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
87 if (nested) {
88 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "changed\n");
89 yield* runGit(root, ["add", "outside.txt"]);
90 }
91 for (const staged of [false, true]) {
92 const addedPath = path.join(cwd, "added.txt");
93 yield* fileSystem.writeFileString(addedPath, "new\n");
94 if (staged) yield* runGit(cwd, ["add", "added.txt"]);
95 assert.isTrue(
96 yield* driver.checkpoints.restoreCheckpoint({
97 cwd,
98 checkpointRef,
99 fallbackToHead: false,
100 }),
101 );
102 assert.isFalse(yield* fileSystem.exists(addedPath));
103 }
104 yield* fileSystem.writeFileString(
105 path.join(root, ".git", "info", "exclude"),
106 "ignored.txt\n",
107 );
108 yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "keep\n");
109 yield* fileSystem.makeDirectory(path.join(cwd, "untracked"));
110 yield* fileSystem.writeFileString(path.join(cwd, "untracked", "file.txt"), "remove\n");
111 assert.isTrue(
112 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
113 );
114 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, "ignored.txt")), "keep\n");
115 assert.isFalse(yield* fileSystem.exists(path.join(cwd, "untracked")));
116 if (nested) {
117 assert.strictEqual(
118 yield* fileSystem.readFileString(path.join(root, "outside.txt")),
119 "changed\n",
120 );
121 const staged = yield* driver.execute({
122 operation: "test",
123 cwd: root,
124 args: ["diff", "--cached", "--name-only"],
125 });
126 assert.strictEqual(staged.stdout.trim(), "outside.txt");
127 }
128 }
129 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
130);
131​
132it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
133 let observedEnv: NodeJS.ProcessEnv | undefined;
134 let observedAppendTruncationMarker: boolean | undefined;
135 let observedOutputMode: VcsProcess.VcsProcessInput["outputMode"];
136​
137 return Effect.gen(function* () {
138 const driver = yield* GitVcsDriver.makeVcsDriverShape();
139​
140 yield* driver.execute({
141 operation: "GitVcsDriver.test.env",
142 cwd: "/repo",
143 args: ["status"],
144 env: {
145 GIT_INDEX_FILE: "/tmp/t3-index",
146 },
147 appendTruncationMarker: true,
148 outputMode: "error",
149 });
150​
151 assert.deepStrictEqual(observedEnv, {
152 GIT_INDEX_FILE: "/tmp/t3-index",
153 });
154 assert.strictEqual(observedAppendTruncationMarker, true);
155 assert.strictEqual(observedOutputMode, "error");
156 }).pipe(
157 Effect.provide(
158 Layer.mergeAll(
159 NodeServices.layer,
160 Layer.mock(VcsProcess.VcsProcess)({
161 run: (input) =>
162 Effect.sync(() => {
163 observedEnv = input.env;
164 observedAppendTruncationMarker = input.appendTruncationMarker;
165 observedOutputMode = input.outputMode;
166 return {
167 exitCode: ChildProcessSpawner.ExitCode(0),
168 stdout: "",
169 stderr: "",
170 stdoutTruncated: false,
171 stderrTruncated: false,
172 };
173 }),
174 }),
175 ),
176 ),
177 );
178});