-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshell.test.ts
More file actions
378 lines (310 loc) · 10.7 KB
/
shell.test.ts
File metadata and controls
378 lines (310 loc) · 10.7 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Shell Utilities Tests
*
* Unit tests for I/O-dependent shell operations (file creation, writing,
* GitHub Actions). Pure function tests are in shell.property.test.ts.
*/
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import {
addToFpath,
addToGitHubPath,
addToPath,
detectShell,
findExistingConfigFile,
getConfigCandidates,
isBashAvailable,
} from "../../src/lib/shell.js";
describe("shell utilities", () => {
describe("getConfigCandidates", () => {
test("returns fallback candidates for unknown shell", () => {
const candidates = getConfigCandidates(
"unknown",
"/home/user",
"/home/user/.config"
);
expect(candidates).toContain("/home/user/.bashrc");
expect(candidates).toContain("/home/user/.bash_profile");
expect(candidates).toContain("/home/user/.profile");
});
});
describe("findExistingConfigFile", () => {
let testDir: string;
beforeEach(() => {
testDir = join(
"/tmp",
`shell-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("returns first existing file", () => {
const file1 = join(testDir, ".bashrc");
const file2 = join(testDir, ".bash_profile");
writeFileSync(file2, "# bash profile");
const result = findExistingConfigFile([file1, file2]);
expect(result).toBe(file2);
});
test("returns null when no files exist", () => {
const result = findExistingConfigFile([
join(testDir, ".nonexistent1"),
join(testDir, ".nonexistent2"),
]);
expect(result).toBeNull();
});
});
describe("detectShell", () => {
let testDir: string;
beforeEach(() => {
testDir = join(
"/tmp",
`shell-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("detects shell type and finds config file", () => {
const zshrc = join(testDir, ".zshrc");
writeFileSync(zshrc, "# zshrc");
const result = detectShell("/bin/zsh", testDir);
expect(result.type).toBe("zsh");
expect(result.configFile).toBe(zshrc);
});
test("returns null configFile when none exist", () => {
const result = detectShell("/bin/zsh", testDir);
expect(result.type).toBe("zsh");
expect(result.configFile).toBeNull();
});
});
describe("addToPath", () => {
let testDir: string;
beforeEach(() => {
testDir = join(
"/tmp",
`shell-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("creates config file if it doesn't exist", async () => {
const configFile = join(testDir, ".bashrc");
const result = await addToPath(
configFile,
"/home/user/.sentry/bin",
"bash"
);
expect(result.modified).toBe(true);
expect(result.configFile).toBe(configFile);
const content = await Bun.file(configFile).text();
expect(content).toContain('export PATH="/home/user/.sentry/bin:$PATH"');
});
test("appends to existing config file", async () => {
const configFile = join(testDir, ".bashrc");
writeFileSync(configFile, "# existing content\n");
const result = await addToPath(
configFile,
"/home/user/.sentry/bin",
"bash"
);
expect(result.modified).toBe(true);
const content = await Bun.file(configFile).text();
expect(content).toContain("# existing content");
expect(content).toContain("# sentry");
expect(content).toContain('export PATH="/home/user/.sentry/bin:$PATH"');
});
test("skips if already configured", async () => {
const configFile = join(testDir, ".bashrc");
writeFileSync(
configFile,
'# sentry\nexport PATH="/home/user/.sentry/bin:$PATH"\n'
);
const result = await addToPath(
configFile,
"/home/user/.sentry/bin",
"bash"
);
expect(result.modified).toBe(false);
expect(result.message).toContain("already configured");
});
test("appends newline separator when file doesn't end with newline", async () => {
const configFile = join(testDir, ".bashrc");
writeFileSync(configFile, "# existing content without newline");
const result = await addToPath(
configFile,
"/home/user/.sentry/bin",
"bash"
);
expect(result.modified).toBe(true);
const content = await Bun.file(configFile).text();
expect(content).toContain(
"# existing content without newline\n\n# sentry\n"
);
});
test("returns manualCommand when config file cannot be created", async () => {
const configFile = "/dev/null/impossible/path/.bashrc";
const result = await addToPath(
configFile,
"/home/user/.sentry/bin",
"bash"
);
expect(result.modified).toBe(false);
expect(result.manualCommand).toBe(
'export PATH="/home/user/.sentry/bin:$PATH"'
);
});
});
describe("addToFpath", () => {
let testDir: string;
beforeEach(() => {
testDir = join(
"/tmp",
`shell-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("creates config file if it doesn't exist", async () => {
const configFile = join(testDir, ".zshrc");
const result = await addToFpath(
configFile,
"/home/user/.local/share/zsh/site-functions"
);
expect(result.modified).toBe(true);
expect(result.configFile).toBe(configFile);
const content = await Bun.file(configFile).text();
expect(content).toContain(
'fpath=("/home/user/.local/share/zsh/site-functions" $fpath)'
);
});
test("appends to existing config file", async () => {
const configFile = join(testDir, ".zshrc");
writeFileSync(configFile, "# existing content\n");
const result = await addToFpath(
configFile,
"/home/user/.local/share/zsh/site-functions"
);
expect(result.modified).toBe(true);
const content = await Bun.file(configFile).text();
expect(content).toContain("# existing content");
expect(content).toContain("# sentry");
expect(content).toContain(
'fpath=("/home/user/.local/share/zsh/site-functions" $fpath)'
);
});
test("skips if already configured", async () => {
const configFile = join(testDir, ".zshrc");
writeFileSync(
configFile,
'# sentry\nfpath=("/home/user/.local/share/zsh/site-functions" $fpath)\n'
);
const result = await addToFpath(
configFile,
"/home/user/.local/share/zsh/site-functions"
);
expect(result.modified).toBe(false);
expect(result.message).toContain("already configured");
});
test("appends newline separator when file doesn't end with newline", async () => {
const configFile = join(testDir, ".zshrc");
writeFileSync(configFile, "# existing content without newline");
const result = await addToFpath(
configFile,
"/home/user/.local/share/zsh/site-functions"
);
expect(result.modified).toBe(true);
const content = await Bun.file(configFile).text();
expect(content).toContain(
"# existing content without newline\n\n# sentry\n"
);
});
test("returns manualCommand when config file cannot be created", async () => {
const configFile = "/dev/null/impossible/path/.zshrc";
const result = await addToFpath(
configFile,
"/home/user/.local/share/zsh/site-functions"
);
expect(result.modified).toBe(false);
expect(result.manualCommand).toBe(
'fpath=("/home/user/.local/share/zsh/site-functions" $fpath)'
);
});
});
describe("addToGitHubPath", () => {
let testDir: string;
beforeEach(() => {
testDir = join(
"/tmp",
`shell-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("returns false when not in GitHub Actions", async () => {
const result = await addToGitHubPath("/usr/local/bin", {});
expect(result).toBe(false);
});
test("returns false when GITHUB_PATH is not set", async () => {
const result = await addToGitHubPath("/usr/local/bin", {
GITHUB_ACTIONS: "true",
});
expect(result).toBe(false);
});
test("writes directory to GITHUB_PATH file", async () => {
const pathFile = join(testDir, "github_path");
writeFileSync(pathFile, "");
const result = await addToGitHubPath("/usr/local/bin", {
GITHUB_ACTIONS: "true",
GITHUB_PATH: pathFile,
});
expect(result).toBe(true);
const content = await Bun.file(pathFile).text();
expect(content).toContain("/usr/local/bin");
});
test("does not duplicate existing directory", async () => {
const pathFile = join(testDir, "github_path");
writeFileSync(pathFile, "/usr/local/bin\n");
const result = await addToGitHubPath("/usr/local/bin", {
GITHUB_ACTIONS: "true",
GITHUB_PATH: pathFile,
});
expect(result).toBe(true);
const content = await Bun.file(pathFile).text();
expect(content).toBe("/usr/local/bin\n");
});
test("returns false when GITHUB_PATH file is not writable", async () => {
const result = await addToGitHubPath("/usr/local/bin", {
GITHUB_ACTIONS: "true",
GITHUB_PATH: "/dev/null/impossible",
});
expect(result).toBe(false);
});
});
});
describe("isBashAvailable", () => {
test("returns true when bash is in PATH", () => {
// Point PATH at the directory containing bash
const bashPath = Bun.which("bash");
if (!bashPath) {
// Skip if bash truly isn't on this system
return;
}
expect(isBashAvailable(dirname(bashPath))).toBe(true);
});
test("returns false when PATH has no bash", () => {
expect(isBashAvailable("/nonexistent")).toBe(false);
});
test("returns false when PATH is empty", () => {
expect(isBashAvailable("")).toBe(false);
});
});