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

worktreeContainer.test.ts

150 lines6.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
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 * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import { ChildProcess, ChildProcessSpawner } from "effect/process";
10​
11import * as ProcessRunner from "../processRunner.ts";
12import { makeIsWorktreeContainer } from "./worktreeContainer.ts";
13​
14const git = Effect.fn("worktreeContainerTest.git")(function* (
15 cwd: string,
16 ...args: ReadonlyArray<string>
17) {
18 const processRunner = yield* ProcessRunner.ProcessRunner;
19 return yield* processRunner.run({
20 command: "git",
21 args: ["-C", cwd, "-c", "user.email=t@t", "-c", "user.name=t", ...args],
22 });
23});
24​
25it.layer(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
26 "isWorktreeContainer",
27 (it) => {
28 it.effect("is true only for a folder of worktrees around metadata that is no checkout", () =>
29 Effect.gen(function* () {
30 const fileSystem = yield* FileSystem.FileSystem;
31 const path = yield* Path.Path;
32 const isWorktreeContainer = yield* makeIsWorktreeContainer;
33 const fixture = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-container-test-" });
34​
35 const source = path.join(fixture, "source");
36 yield* fileSystem.makeDirectory(source);
37 yield* git(source, "init", "-b", "develop");
38 yield* fileSystem.writeFileString(path.join(source, "README.md"), "readme\n");
39 yield* git(source, "add", "README.md");
40 yield* git(source, "commit", "-m", "init");
41​
42 const makeContainer = Effect.fn(function* (name: string, coreBare: boolean) {
43 const root = path.join(fixture, name);
44 yield* fileSystem.makeDirectory(root);
45 yield* git(fixture, "clone", "--bare", source, path.join(root, ".bare"));
46 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
47 yield* git(root, "worktree", "add", "develop", "develop");
48 yield* git(root, "config", "core.bare", String(coreBare));
49 return root;
50 });
51 const makeSeparateGitDirCheckout = Effect.fn(function* (name: string) {
52 const root = path.join(fixture, name);
53 yield* fileSystem.makeDirectory(root);
54 yield* git(root, "init", "-b", "develop", "--separate-git-dir", ".metadata", ".");
55 return root;
56 });
57​
58 const container = yield* makeContainer("container", false);
59 assert.isTrue(yield* isWorktreeContainer(container), "container");
60 assert.isFalse(yield* isWorktreeContainer(path.join(container, "develop")), "its worktree");
61​
62 const linked = path.join(fixture, "link");
63 yield* fileSystem.symlink(container, linked);
64 assert.isTrue(yield* isWorktreeContainer(linked), "container through a symlink");
65​
66 // The issue's steps leave core.bare set; Initialize Git would clear it later.
67 const bare = yield* makeContainer("bare", true);
68 assert.isTrue(yield* isWorktreeContainer(bare), "core.bare container");
69​
70 // Git allows an orphan worktree before the first commit: no index, yet a valid checkout.
71 const fresh = yield* makeSeparateGitDirCheckout("fresh");
72 yield* git(fresh, "worktree", "add", "--orphan", "-b", "feature", "../fresh-feature");
73 assert.isFalse(yield* isWorktreeContainer(fresh), "fresh checkout with an orphan worktree");
74​
75 const checkout = yield* makeSeparateGitDirCheckout("checkout");
76 yield* fileSystem.writeFileString(path.join(checkout, "README.md"), "readme\n");
77 yield* git(checkout, "add", "README.md");
78 yield* git(checkout, "commit", "-m", "init");
79 yield* git(checkout, "worktree", "add", "worktrees/feature", "-b", "feature");
80 assert.isFalse(yield* isWorktreeContainer(checkout), "checkout with a nested worktree");
81​
82 const outside = path.join(fixture, "outside");
83 yield* git(
84 fixture,
85 "clone",
86 "--no-checkout",
87 "--separate-git-dir",
88 path.join(outside, ".metadata"),
89 source,
90 outside,
91 );
92 yield* git(outside, "worktree", "add", "../outside-feature", "-b", "feature");
93 assert.isFalse(yield* isWorktreeContainer(outside), "worktrees outside the folder");
94 }),
95 );
96 it.effect("finds a container with Git that rejects worktree list -z", () =>
97 Effect.gen(function* () {
98 const fileSystem = yield* FileSystem.FileSystem;
99 const path = yield* Path.Path;
100 const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
101 // Git before 2.36 has no `worktree list -z` and exits with a usage error.
102 const oldGitSpawner = ChildProcessSpawner.make((command) =>
103 Effect.gen(function* () {
104 if (!ChildProcess.isStandardCommand(command)) {
105 return yield* Effect.die("expected a standard Git command");
106 }
107 if (command.args.includes("worktree") && command.args.includes("-z")) {
108 return ChildProcessSpawner.makeHandle({
109 pid: ChildProcessSpawner.ProcessId(1),
110 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(129)),
111 isRunning: Effect.succeed(false),
112 kill: () => Effect.void,
113 unref: Effect.succeed(Effect.void),
114 stdin: Sink.drain,
115 stdout: Stream.empty,
116 stderr: Stream.encodeText(Stream.make("error: unknown switch `z'")),
117 all: Stream.empty,
118 getInputFd: () => Sink.drain,
119 getOutputFd: () => Stream.empty,
120 });
121 }
122 return yield* delegate.spawn(command);
123 }),
124 );
125 const isWorktreeContainer = yield* makeIsWorktreeContainer.pipe(
126 Effect.provideServiceEffect(
127 ProcessRunner.ProcessRunner,
128 ProcessRunner.make().pipe(
129 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, oldGitSpawner),
130 ),
131 ),
132 );
133 const fixture = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-container-test-" });
134 const source = path.join(fixture, "source");
135 yield* fileSystem.makeDirectory(source);
136 yield* git(source, "init", "-b", "develop");
137 yield* fileSystem.writeFileString(path.join(source, "README.md"), "readme\n");
138 yield* git(source, "add", "README.md");
139 yield* git(source, "commit", "-m", "init");
140 const root = path.join(fixture, "container");
141 yield* fileSystem.makeDirectory(root);
142 yield* git(fixture, "clone", "--bare", source, path.join(root, ".bare"));
143 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
144 yield* git(root, "worktree", "add", "develop", "develop");
145​
146 assert.isTrue(yield* isWorktreeContainer(root));
147 }),
148 );
149 },
150);