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

GitVcsDriver.test.ts

99 lines3.2 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
3import { ChildProcessSpawner } from "effect/unstable/process";
4import { assert, it } from "@effect/vitest";
5​
6import { GitCommandError } from "@t3tools/contracts";
7import { ServerConfig } from "../config.ts";
8import * as GitVcsDriver from "./GitVcsDriver.ts";
9import * as VcsProcess from "./VcsProcess.ts";
10import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
11​
12const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
13 prefix: "t3-git-vcs-contract-",
14});
15const GitContractLayer = Layer.mergeAll(GitVcsDriver.vcsLayer, GitVcsDriver.layer).pipe(
16 Layer.provide(ServerConfigLayer),
17 Layer.provideMerge(VcsProcess.layer),
18 Layer.provideMerge(NodeServices.layer),
19);
20​
21const runGit = (cwd: string, args: ReadonlyArray<string>) =>
22 Effect.gen(function* () {
23 const driver = yield* GitVcsDriver.GitVcsDriver;
24 yield* driver.execute({
25 operation: "GitVcsDriver.contract.git",
26 cwd,
27 args,
28 timeoutMs: 10_000,
29 });
30 });
31​
32type GitContractError = GitCommandError | PlatformError.PlatformError;
33​
34runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
35 name: "Git",
36 kind: "git",
37 layer: GitContractLayer,
38 fixture: {
39 createRepo: (cwd) =>
40 Effect.gen(function* () {
41 yield* runGit(cwd, ["init"]);
42 yield* runGit(cwd, ["config", "user.email", "test@test.com"]);
43 yield* runGit(cwd, ["config", "user.name", "Test"]);
44 }),
45 writeFile: (cwd, relativePath, contents) =>
46 Effect.gen(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const absolutePath = path.join(cwd, relativePath);
50 yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
51 yield* fileSystem.writeFileString(absolutePath, contents);
52 }),
53 trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
54 commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
55 ignorePath: (cwd, pattern) =>
56 Effect.gen(function* () {
57 const fileSystem = yield* FileSystem.FileSystem;
58 const path = yield* Path.Path;
59 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
60 }),
61 },
62});
63​
64it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
65 let observedEnv: NodeJS.ProcessEnv | undefined;
66​
67 return Effect.gen(function* () {
68 const driver = yield* GitVcsDriver.makeVcsDriverShape();
69​
70 yield* driver.execute({
71 operation: "GitVcsDriver.test.env",
72 cwd: "/repo",
73 args: ["status"],
74 env: {
75 GIT_INDEX_FILE: "/tmp/t3-index",
76 },
77 });
78​
79 assert.deepStrictEqual(observedEnv, {
80 GIT_INDEX_FILE: "/tmp/t3-index",
81 });
82 }).pipe(
83 Effect.provide(
84 Layer.mock(VcsProcess.VcsProcess)({
85 run: (input) =>
86 Effect.sync(() => {
87 observedEnv = input.env;
88 return {
89 exitCode: ChildProcessSpawner.ExitCode(0),
90 stdout: "",
91 stderr: "",
92 stdoutTruncated: false,
93 stderrTruncated: false,
94 };
95 }),
96 }),
97 ),
98 );
99});