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

GitVcsDriver.test.ts

114 lines3.9 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 { 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("GitVcsDriver forwards execute env to the VCS process", () => {
69 let observedEnv: NodeJS.ProcessEnv | undefined;
70 let observedAppendTruncationMarker: boolean | undefined;
71 let observedOutputMode: VcsProcess.VcsProcessInput["outputMode"];
72​
73 return Effect.gen(function* () {
74 const driver = yield* GitVcsDriver.makeVcsDriverShape();
75​
76 yield* driver.execute({
77 operation: "GitVcsDriver.test.env",
78 cwd: "/repo",
79 args: ["status"],
80 env: {
81 GIT_INDEX_FILE: "/tmp/t3-index",
82 },
83 appendTruncationMarker: true,
84 outputMode: "error",
85 });
86​
87 assert.deepStrictEqual(observedEnv, {
88 GIT_INDEX_FILE: "/tmp/t3-index",
89 });
90 assert.strictEqual(observedAppendTruncationMarker, true);
91 assert.strictEqual(observedOutputMode, "error");
92 }).pipe(
93 Effect.provide(
94 Layer.mergeAll(
95 NodeServices.layer,
96 Layer.mock(VcsProcess.VcsProcess)({
97 run: (input) =>
98 Effect.sync(() => {
99 observedEnv = input.env;
100 observedAppendTruncationMarker = input.appendTruncationMarker;
101 observedOutputMode = input.outputMode;
102 return {
103 exitCode: ChildProcessSpawner.ExitCode(0),
104 stdout: "",
105 stderr: "",
106 stdoutTruncated: false,
107 stderrTruncated: false,
108 };
109 }),
110 }),
111 ),
112 ),
113 );
114});