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

GitVcsDriver.test.ts

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