-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathrestore-config.test.ts
More file actions
190 lines (157 loc) · 5.97 KB
/
Copy pathrestore-config.test.ts
File metadata and controls
190 lines (157 loc) · 5.97 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
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { execFileSync } from "child_process";
import {
existsSync,
mkdtempSync,
mkdirSync,
readFileSync,
rmSync,
writeFileSync,
} from "fs";
import { dirname, isAbsolute, join } from "path";
import { restoreConfigFromBase } from "../src/github/operations/restore-config";
const CLAUDE_PR_EXCLUDE_PATTERN = "/.claude-pr/";
describe("restoreConfigFromBase", () => {
let originalCwd: string;
let tempDir = "";
let repoDir: string;
let remoteDir: string;
beforeEach(() => {
originalCwd = process.cwd();
tempDir = mkdtempSync(join("/tmp", "restore-config-"));
repoDir = join(tempDir, "repo");
remoteDir = join(tempDir, "origin.git");
execFileSync("git", ["init", "--bare", remoteDir], { stdio: "pipe" });
execFileSync("git", ["init", repoDir], { stdio: "pipe" });
git(["checkout", "-b", "main"]);
git(["config", "user.email", "test@example.com"]);
git(["config", "user.name", "Test User"]);
writeRepoFile("CLAUDE.md", "base claude instructions\n");
writeRepoFile(
".claude/settings.json",
`${JSON.stringify({ source: "base" })}\n`,
);
writeRepoFile("src/index.ts", "export const base = true;\n");
git(["add", "CLAUDE.md", ".claude/settings.json", "src/index.ts"]);
git(["commit", "-m", "base config"]);
git(["remote", "add", "origin", remoteDir]);
git(["push", "-u", "origin", "main"]);
git(["checkout", "-b", "pr"]);
writeRepoFile("CLAUDE.md", "pr claude instructions\n");
writeRepoFile(
".claude/settings.json",
`${JSON.stringify({ source: "pr" })}\n`,
);
git(["add", "CLAUDE.md", ".claude/settings.json"]);
git(["commit", "-m", "pr config"]);
process.chdir(repoDir);
});
afterEach(() => {
process.chdir(originalCwd);
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
}
});
test("preserves PR sensitive files while excluding .claude-pr from broad staging", () => {
const gitignoreExistedBefore = existsRepoFile(".gitignore");
const gitignoreContentsBefore = gitignoreExistedBefore
? readRepoFile(".gitignore")
: "";
restoreConfigFromBase("main");
expect(readRepoFile(".claude-pr/CLAUDE.md")).toBe(
"pr claude instructions\n",
);
expect(readRepoFile(".claude-pr/.claude/settings.json")).toBe(
`${JSON.stringify({ source: "pr" })}\n`,
);
expect(readRepoFile("CLAUDE.md")).toBe("base claude instructions\n");
expect(readRepoFile(".claude/settings.json")).toBe(
`${JSON.stringify({ source: "base" })}\n`,
);
expect(git(["check-ignore", ".claude-pr/CLAUDE.md"]).trim()).toBe(
".claude-pr/CLAUDE.md",
);
expect(countClaudePrExcludeEntries()).toBe(1);
restoreConfigFromBase("main");
expect(countClaudePrExcludeEntries()).toBe(1);
expect(existsRepoFile(".gitignore")).toBe(gitignoreExistedBefore);
if (gitignoreExistedBefore) {
expect(readRepoFile(".gitignore")).toBe(gitignoreContentsBefore);
}
writeRepoFile("src/fix.ts", "export const fix = true;\n");
git(["add", "-A"]);
const stagedFiles = git(["diff", "--cached", "--name-only"])
.trim()
.split(/\r?\n/)
.filter(Boolean);
expect(stagedFiles).toContain("src/fix.ts");
expect(stagedFiles.some((file) => file.startsWith(".claude-pr/"))).toBe(
false,
);
git(["commit", "-m", "apply fix"]);
const committedFiles = git(["show", "--name-only", "--format=", "HEAD"])
.trim()
.split(/\r?\n/)
.filter(Boolean);
expect(committedFiles).toContain("src/fix.ts");
expect(committedFiles.some((file) => file.startsWith(".claude-pr/"))).toBe(
false,
);
expect(existsRepoFile(".gitignore")).toBe(gitignoreExistedBefore);
if (gitignoreExistedBefore) {
expect(readRepoFile(".gitignore")).toBe(gitignoreContentsBefore);
}
});
test("does not modify an existing .gitignore", () => {
writeRepoFile(".gitignore", "node_modules\n");
git(["add", ".gitignore"]);
git(["commit", "-m", "add gitignore"]);
const gitignoreBefore = readRepoFile(".gitignore");
restoreConfigFromBase("main");
expect(readRepoFile(".gitignore")).toBe(gitignoreBefore);
expect(countClaudePrExcludeEntries()).toBe(1);
});
test("handles dangling symlinks in .claude/ without crashing", () => {
const skillsDir = join(repoDir, ".claude/skills");
mkdirSync(skillsDir, { recursive: true });
// Create a symlink pointing to a non-existent file (simulating a dependency
// directory that hasn't been installed yet)
execFileSync("ln", ["-s", "../../vendor/package/.claude/skills/check", join(skillsDir, "check")], {
cwd: repoDir,
stdio: "pipe",
});
git(["add", ".claude/skills/check"]);
git(["commit", "-m", "add dangling symlink"]);
// This should not throw ENOENT
expect(() => restoreConfigFromBase("main")).not.toThrow();
// Verify the symlink was preserved in .claude-pr/
expect(existsRepoFile(".claude-pr/.claude/skills/check")).toBe(true);
});
function git(args: string[]): string {
return execFileSync("git", args, {
cwd: repoDir,
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
});
}
function writeRepoFile(path: string, contents: string): void {
const fullPath = join(repoDir, path);
mkdirSync(dirname(fullPath), { recursive: true });
writeFileSync(fullPath, contents);
}
function readRepoFile(path: string): string {
return readFileSync(join(repoDir, path), "utf8");
}
function existsRepoFile(path: string): boolean {
return existsSync(join(repoDir, path));
}
function countClaudePrExcludeEntries(): number {
return readFileSync(getExcludePath(), "utf8")
.split(/\r?\n/)
.filter((line) => line === CLAUDE_PR_EXCLUDE_PATTERN).length;
}
function getExcludePath(): string {
const gitPath = git(["rev-parse", "--git-path", "info/exclude"]).trim();
return isAbsolute(gitPath) ? gitPath : join(repoDir, gitPath);
}
});