forked from coder/mux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorktreeManager.test.ts
More file actions
282 lines (239 loc) · 10.2 KB
/
WorktreeManager.test.ts
File metadata and controls
282 lines (239 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { describe, expect, it } from "bun:test";
import * as os from "os";
import * as path from "path";
import * as fsPromises from "fs/promises";
import { execSync } from "node:child_process";
import type { InitLogger } from "@/node/runtime/Runtime";
import { WorktreeManager } from "./WorktreeManager";
function initGitRepo(projectPath: string): void {
execSync("git init -b main", { cwd: projectPath, stdio: "ignore" });
execSync('git config user.email "test@example.com"', { cwd: projectPath, stdio: "ignore" });
execSync('git config user.name "test"', { cwd: projectPath, stdio: "ignore" });
// Ensure tests don't hang when developers have global commit signing enabled.
execSync("git config commit.gpgsign false", { cwd: projectPath, stdio: "ignore" });
execSync("bash -lc 'echo \"hello\" > README.md'", { cwd: projectPath, stdio: "ignore" });
execSync("git add README.md", { cwd: projectPath, stdio: "ignore" });
execSync('git commit -m "init"', { cwd: projectPath, stdio: "ignore" });
}
function initGitRepoWithSubmodule(projectPath: string, submoduleSourcePath: string): void {
initGitRepo(projectPath);
execSync(`git -c protocol.file.allow=always submodule add "${submoduleSourcePath}" deps/sub`, {
cwd: projectPath,
stdio: "ignore",
});
// Local-path submodules are used only in tests; allow file transport so
// `git submodule update --init --recursive` can run inside worktrees.
execSync("git config protocol.file.allow always", { cwd: projectPath, stdio: "ignore" });
execSync('git commit -m "add submodule"', { cwd: projectPath, stdio: "ignore" });
}
function createNullInitLogger(): InitLogger {
return {
logStep: (_message: string) => undefined,
logStdout: (_line: string) => undefined,
logStderr: (_line: string) => undefined,
logComplete: (_exitCode: number) => undefined,
};
}
describe("WorktreeManager constructor", () => {
it("should expand tilde in srcBaseDir", () => {
const manager = new WorktreeManager("~/workspace");
const workspacePath = manager.getWorkspacePath("/home/user/project", "branch");
// The workspace path should use the expanded home directory
const expected = path.join(os.homedir(), "workspace", "project", "branch");
expect(workspacePath).toBe(expected);
});
it("should handle absolute paths without expansion", () => {
const manager = new WorktreeManager("/absolute/path");
const workspacePath = manager.getWorkspacePath("/home/user/project", "branch");
const expected = path.join("/absolute/path", "project", "branch");
expect(workspacePath).toBe(expected);
});
it("should handle bare tilde", () => {
const manager = new WorktreeManager("~");
const workspacePath = manager.getWorkspacePath("/home/user/project", "branch");
const expected = path.join(os.homedir(), "project", "branch");
expect(workspacePath).toBe(expected);
});
});
describe("WorktreeManager.createWorkspace", () => {
it("initializes submodules in the created worktree", async () => {
const rootDir = await fsPromises.realpath(
await fsPromises.mkdtemp(path.join(os.tmpdir(), "worktree-manager-create-"))
);
try {
const submoduleSourcePath = path.join(rootDir, "submodule-source");
await fsPromises.mkdir(submoduleSourcePath, { recursive: true });
initGitRepo(submoduleSourcePath);
const projectPath = path.join(rootDir, "repo");
await fsPromises.mkdir(projectPath, { recursive: true });
initGitRepoWithSubmodule(projectPath, submoduleSourcePath);
const srcBaseDir = path.join(rootDir, "src");
await fsPromises.mkdir(srcBaseDir, { recursive: true });
const manager = new WorktreeManager(srcBaseDir);
const initLogger = createNullInitLogger();
const createResult = await manager.createWorkspace({
projectPath,
branchName: "feature_with_submodule",
trunkBranch: "main",
initLogger,
});
expect(createResult.success).toBe(true);
if (!createResult.success || !createResult.workspacePath) return;
const submoduleStatus = execSync("git submodule status", {
cwd: createResult.workspacePath,
stdio: ["ignore", "pipe", "ignore"],
})
.toString()
.trim();
expect(submoduleStatus.startsWith("-")).toBe(false);
} finally {
await fsPromises.rm(rootDir, { recursive: true, force: true });
}
}, 20_000);
});
describe("WorktreeManager.deleteWorkspace", () => {
it("deletes non-agent branches when removing worktrees (force)", async () => {
const rootDir = await fsPromises.realpath(
await fsPromises.mkdtemp(path.join(os.tmpdir(), "worktree-manager-delete-"))
);
try {
const projectPath = path.join(rootDir, "repo");
await fsPromises.mkdir(projectPath, { recursive: true });
initGitRepo(projectPath);
const srcBaseDir = path.join(rootDir, "src");
await fsPromises.mkdir(srcBaseDir, { recursive: true });
const manager = new WorktreeManager(srcBaseDir);
const initLogger = createNullInitLogger();
const branchName = "feature_aaaaaaaaaa";
const createResult = await manager.createWorkspace({
projectPath,
branchName,
trunkBranch: "main",
initLogger,
});
expect(createResult.success).toBe(true);
if (!createResult.success) return;
if (!createResult.workspacePath) {
throw new Error("Expected workspacePath from createWorkspace");
}
const workspacePath = createResult.workspacePath;
// Make the branch unmerged (so -d would fail); force delete should still delete it.
execSync("bash -lc 'echo \"change\" >> README.md'", {
cwd: workspacePath,
stdio: "ignore",
});
execSync("git add README.md", { cwd: workspacePath, stdio: "ignore" });
execSync('git commit -m "change"', { cwd: workspacePath, stdio: "ignore" });
const deleteResult = await manager.deleteWorkspace(projectPath, branchName, true);
expect(deleteResult.success).toBe(true);
const after = execSync(`git branch --list "${branchName}"`, {
cwd: projectPath,
stdio: ["ignore", "pipe", "ignore"],
})
.toString()
.trim();
expect(after).toBe("");
} finally {
await fsPromises.rm(rootDir, { recursive: true, force: true });
}
}, 20_000);
it("deletes merged branches when removing worktrees (safe delete)", async () => {
const rootDir = await fsPromises.realpath(
await fsPromises.mkdtemp(path.join(os.tmpdir(), "worktree-manager-delete-"))
);
try {
const projectPath = path.join(rootDir, "repo");
await fsPromises.mkdir(projectPath, { recursive: true });
initGitRepo(projectPath);
const srcBaseDir = path.join(rootDir, "src");
await fsPromises.mkdir(srcBaseDir, { recursive: true });
const manager = new WorktreeManager(srcBaseDir);
const initLogger = createNullInitLogger();
const branchName = "feature_merge_aaaaaaaaaa";
const createResult = await manager.createWorkspace({
projectPath,
branchName,
trunkBranch: "main",
initLogger,
});
expect(createResult.success).toBe(true);
if (!createResult.success) return;
if (!createResult.workspacePath) {
throw new Error("Expected workspacePath from createWorkspace");
}
const workspacePath = createResult.workspacePath;
// Commit on the workspace branch.
execSync("bash -lc 'echo \"merged-change\" >> README.md'", {
cwd: workspacePath,
stdio: "ignore",
});
execSync("git add README.md", { cwd: workspacePath, stdio: "ignore" });
execSync('git commit -m "merged-change"', {
cwd: workspacePath,
stdio: "ignore",
});
// Merge into main so `git branch -d` succeeds.
execSync(`git merge "${branchName}"`, { cwd: projectPath, stdio: "ignore" });
const deleteResult = await manager.deleteWorkspace(projectPath, branchName, false);
expect(deleteResult.success).toBe(true);
const after = execSync(`git branch --list "${branchName}"`, {
cwd: projectPath,
stdio: ["ignore", "pipe", "ignore"],
})
.toString()
.trim();
expect(after).toBe("");
} finally {
await fsPromises.rm(rootDir, { recursive: true, force: true });
}
}, 20_000);
it("does not delete protected branches", async () => {
const rootDir = await fsPromises.realpath(
await fsPromises.mkdtemp(path.join(os.tmpdir(), "worktree-manager-delete-"))
);
try {
const projectPath = path.join(rootDir, "repo");
await fsPromises.mkdir(projectPath, { recursive: true });
initGitRepo(projectPath);
// Move the main worktree off main so we can add a separate worktree on main.
execSync("git checkout -b other", { cwd: projectPath, stdio: "ignore" });
const srcBaseDir = path.join(rootDir, "src");
await fsPromises.mkdir(srcBaseDir, { recursive: true });
const manager = new WorktreeManager(srcBaseDir);
const initLogger = createNullInitLogger();
const branchName = "main";
const createResult = await manager.createWorkspace({
projectPath,
branchName,
trunkBranch: "main",
initLogger,
});
expect(createResult.success).toBe(true);
if (!createResult.success) return;
if (!createResult.workspacePath) {
throw new Error("Expected workspacePath from createWorkspace");
}
const workspacePath = createResult.workspacePath;
const deleteResult = await manager.deleteWorkspace(projectPath, branchName, true);
expect(deleteResult.success).toBe(true);
// The worktree directory should be removed.
let worktreeExists = true;
try {
await fsPromises.access(workspacePath);
} catch {
worktreeExists = false;
}
expect(worktreeExists).toBe(false);
// But protected branches (like main) should never be deleted.
const after = execSync(`git branch --list "${branchName}"`, {
cwd: projectPath,
stdio: ["ignore", "pipe", "ignore"],
})
.toString()
.trim();
expect(after).toBe("main");
} finally {
await fsPromises.rm(rootDir, { recursive: true, force: true });
}
}, 20_000);
});