-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcd-virtual-command.test.mjs
More file actions
584 lines (478 loc) · 17.5 KB
/
cd-virtual-command.test.mjs
File metadata and controls
584 lines (478 loc) · 17.5 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import { test, expect, describe, beforeEach, afterEach } from 'bun:test';
import { beforeTestCleanup, afterTestCleanup, originalCwd } from './test-cleanup.mjs';
import { $, shell, enableVirtualCommands } from '../src/$.mjs';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, existsSync } from 'fs';
import { tmpdir, homedir } from 'os';
import { join, resolve } from 'path';
// Helper function to verify we're in the expected directory
function verifyCwd(expected, message) {
const actual = process.cwd();
if (actual !== expected) {
throw new Error(`${message}: Expected cwd to be ${expected}, but got ${actual}`);
}
}
describe('cd Virtual Command - Core Behavior', () => {
beforeEach(async () => {
await beforeTestCleanup();
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
enableVirtualCommands();
// Verify we start in the original directory
verifyCwd(originalCwd, 'Before test start');
});
afterEach(async () => {
await afterTestCleanup();
// Verify we restored to original directory
verifyCwd(originalCwd, 'After test cleanup');
});
test('should change to absolute path', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-test-'));
const testStartCwd = process.cwd();
try {
// Verify we start in the original directory
verifyCwd(originalCwd, 'Test start');
const result = await $`cd ${tempDir}`;
expect(result.code).toBe(0);
expect(result.stdout).toBe(''); // cd should not output anything
expect(result.stderr).toBe('');
// Verify cd actually changed the directory
verifyCwd(tempDir, 'After cd to tempDir');
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(tempDir);
// Go back to original
await $`cd ${testStartCwd}`;
verifyCwd(testStartCwd, 'After cd back');
} finally {
// Ensure we're back in original before cleanup
if (process.cwd() !== testStartCwd) {
process.chdir(testStartCwd);
}
rmSync(tempDir, { recursive: true, force: true });
}
});
test('should change to relative path', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-base-'));
const subDir = join(baseDir, 'subdir');
mkdirSync(subDir);
const originalCwd = process.cwd();
try {
await $`cd ${baseDir}`;
const result = await $`cd subdir`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(subDir);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should handle cd with no arguments (go to home)', async () => {
const originalCwd = process.cwd();
try {
const result = await $`cd`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwd = await $`pwd`;
const home = homedir();
expect(pwd.stdout.trim()).toBe(home);
await $`cd ${originalCwd}`;
} catch (error) {
await $`cd ${originalCwd}`;
throw error;
}
});
test('should handle cd - (return to previous directory)', async () => {
const dir1 = mkdtempSync(join(tmpdir(), 'cd-dir1-'));
const dir2 = mkdtempSync(join(tmpdir(), 'cd-dir2-'));
const originalCwd = process.cwd();
try {
await $`cd ${dir1}`;
const pwd1 = await $`pwd`;
expect(pwd1.stdout.trim()).toBe(dir1);
await $`cd ${dir2}`;
const pwd2 = await $`pwd`;
expect(pwd2.stdout.trim()).toBe(dir2);
// Note: cd - might not be implemented in virtual command yet
// This test documents expected behavior
const result = await $`cd - 2>&1 || echo "not implemented"`;
await $`cd ${originalCwd}`;
} finally {
rmSync(dir1, { recursive: true, force: true });
rmSync(dir2, { recursive: true, force: true });
}
});
test('should handle cd .. (parent directory)', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-parent-'));
const subDir = join(baseDir, 'child');
mkdirSync(subDir);
const originalCwd = process.cwd();
try {
await $`cd ${subDir}`;
const result = await $`cd ..`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(baseDir);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should handle cd . (current directory)', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-dot-'));
const originalCwd = process.cwd();
try {
await $`cd ${tempDir}`;
const pwdBefore = await $`pwd`;
const result = await $`cd .`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwdAfter = await $`pwd`;
expect(pwdAfter.stdout).toBe(pwdBefore.stdout);
await $`cd ${originalCwd}`;
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
test('should fail with non-existent directory', async () => {
const nonExistent = '/this/path/should/not/exist/at/all';
const originalCwd = process.cwd();
const result = await $`cd ${nonExistent} 2>&1 || echo "failed"`;
expect(result.stdout).toContain('failed');
// Verify we're still in the same directory
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(originalCwd);
});
test('should handle paths with spaces', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-spaces-'));
const dirWithSpaces = join(baseDir, 'my test directory');
mkdirSync(dirWithSpaces);
const originalCwd = process.cwd();
try {
const result = await $`cd ${dirWithSpaces}`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(dirWithSpaces);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should handle special characters in paths', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-special-'));
// Create directory with special characters (but valid for filesystem)
const specialDir = join(baseDir, "test-dir_123");
mkdirSync(specialDir);
const originalCwd = process.cwd();
try {
const result = await $`cd ${specialDir}`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(specialDir);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
});
describe('cd Virtual Command - Command Chains', () => {
beforeEach(async () => {
await beforeTestCleanup();
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
enableVirtualCommands();
verifyCwd(originalCwd, 'Before test start');
});
afterEach(async () => {
await afterTestCleanup();
verifyCwd(originalCwd, 'After test cleanup');
});
test('should persist directory change within command chain', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-chain-'));
const originalCwd = process.cwd();
try {
// Create a test file in temp directory
writeFileSync(join(tempDir, 'test.txt'), 'test content');
// cd and run command in same chain
const result = await $`cd ${tempDir} && cat test.txt`;
expect(result.code).toBe(0);
expect(result.stdout.trim()).toBe('test content');
await $`cd ${originalCwd}`;
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
test('should handle multiple cd commands in chain', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-multi-'));
const dir1 = join(baseDir, 'dir1');
const dir2 = join(baseDir, 'dir2');
mkdirSync(dir1);
mkdirSync(dir2);
const originalCwd = process.cwd();
try {
writeFileSync(join(dir1, 'file1.txt'), 'content1');
writeFileSync(join(dir2, 'file2.txt'), 'content2');
// Chain multiple cd commands
const result = await $`cd ${dir1} && cat file1.txt && cd ${dir2} && cat file2.txt`;
expect(result.code).toBe(0);
expect(result.stdout).toContain('content1');
expect(result.stdout).toContain('content2');
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should work with git commands in chain', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-git-'));
const originalCwd = process.cwd();
try {
const result = await $`cd ${tempDir} && git init`;
expect(result.code).toBe(0);
// Git init outputs to stderr
const output = (result.stdout + result.stderr).toLowerCase();
expect(output).toContain('initialized');
// Verify git repo was created in the right place
expect(existsSync(join(tempDir, '.git'))).toBe(true);
await $`cd ${originalCwd}`;
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
test('should maintain separate directory context per command when not chained', async () => {
const dir1 = mkdtempSync(join(tmpdir(), 'cd-ctx1-'));
const dir2 = mkdtempSync(join(tmpdir(), 'cd-ctx2-'));
const originalCwd = process.cwd();
try {
// First command changes to dir1
await $`cd ${dir1}`;
const pwd1 = await $`pwd`;
expect(pwd1.stdout.trim()).toBe(dir1);
// Second separate command should still be in dir1
const pwd2 = await $`pwd`;
expect(pwd2.stdout.trim()).toBe(dir1);
await $`cd ${originalCwd}`;
} finally {
rmSync(dir1, { recursive: true, force: true });
rmSync(dir2, { recursive: true, force: true });
}
});
});
describe('cd Virtual Command - Subshell Behavior', () => {
beforeEach(async () => {
await beforeTestCleanup();
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
enableVirtualCommands();
verifyCwd(originalCwd, 'Before test start');
});
afterEach(async () => {
await afterTestCleanup();
verifyCwd(originalCwd, 'After test cleanup');
});
test('should not affect parent shell when in subshell', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-subshell-'));
const originalCwd = process.cwd();
try {
// Run cd in subshell (parentheses)
await $`(cd ${tempDir})`;
// Parent shell should still be in original directory
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(originalCwd);
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
test('should work in subshell with other commands', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-subshell-cmd-'));
const originalCwd = process.cwd();
try {
writeFileSync(join(tempDir, 'test.txt'), 'subshell test');
// Run commands in subshell
const result = await $`(cd ${tempDir} && cat test.txt)`;
expect(result.code).toBe(0);
expect(result.stdout.trim()).toBe('subshell test');
// Verify we're still in original directory
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(originalCwd);
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
});
describe('cd Virtual Command - Edge Cases', () => {
beforeEach(async () => {
await beforeTestCleanup();
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
enableVirtualCommands();
verifyCwd(originalCwd, 'Before test start');
});
afterEach(async () => {
await afterTestCleanup();
verifyCwd(originalCwd, 'After test cleanup');
});
test('should handle symlinks', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-symlink-'));
const realDir = join(baseDir, 'real');
const linkDir = join(baseDir, 'link');
mkdirSync(realDir);
const originalCwd = process.cwd();
try {
// Create symlink
await $`ln -s ${realDir} ${linkDir}`;
// cd through symlink
const result = await $`cd ${linkDir}`;
expect(result.code).toBe(0);
// pwd should show the symlink path (default behavior)
const pwd = await $`pwd`;
// Note: behavior may vary between pwd and pwd -P
expect(pwd.stdout.trim()).toBeTruthy();
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should handle very long paths', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-long-'));
const originalCwd = process.cwd();
try {
// Create deeply nested directory
let currentPath = baseDir;
for (let i = 0; i < 10; i++) {
currentPath = join(currentPath, `level${i}`);
mkdirSync(currentPath);
}
const result = await $`cd ${currentPath}`;
expect(result.code).toBe(0);
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(currentPath);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should handle permission errors gracefully', async () => {
// Skip on Windows where permissions work differently
if (process.platform === 'win32') {
return;
}
const baseDir = mkdtempSync(join(tmpdir(), 'cd-perm-'));
const restrictedDir = join(baseDir, 'restricted');
mkdirSync(restrictedDir);
const originalCwd = process.cwd();
try {
// Remove execute permission
await $`chmod 000 ${restrictedDir}`;
const result = await $`cd ${restrictedDir} 2>&1 || echo "permission denied"`;
expect(result.stdout.toLowerCase()).toContain('denied');
// Should still be in original directory
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(originalCwd);
} finally {
// Restore permissions for cleanup - need to restore parent dir permission first
try {
await $`chmod 755 ${baseDir} 2>/dev/null || true`;
await $`chmod 755 ${restrictedDir} 2>/dev/null || true`;
rmSync(baseDir, { recursive: true, force: true });
} catch (e) {
// If cleanup fails, try with sudo as last resort
await $`sudo rm -rf ${baseDir} 2>/dev/null || true`.catch(() => {});
}
}
});
test('should handle cd with trailing slash', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'cd-slash-'));
const originalCwd = process.cwd();
try {
// Test with trailing slash
const result = await $`cd ${tempDir}/`;
expect(result.code).toBe(0);
expect(result.stdout).toBe('');
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(tempDir);
await $`cd ${originalCwd}`;
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
test('should handle cd with multiple slashes', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-slashes-'));
const subDir = join(baseDir, 'sub');
mkdirSync(subDir);
const originalCwd = process.cwd();
try {
// Test with multiple slashes (should normalize)
const result = await $`cd ${baseDir}//sub///`;
expect(result.code).toBe(0);
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(subDir);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
});
describe('cd Virtual Command - Platform Compatibility', () => {
beforeEach(async () => {
await beforeTestCleanup();
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
enableVirtualCommands();
verifyCwd(originalCwd, 'Before test start');
});
afterEach(async () => {
await afterTestCleanup();
verifyCwd(originalCwd, 'After test cleanup');
});
test('should handle platform-specific path separators', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-platform-'));
const subDir = join(baseDir, 'cross', 'platform', 'test');
mkdirSync(subDir, { recursive: true });
const originalCwd = process.cwd();
try {
// Use platform-specific path
const result = await $`cd ${subDir}`;
expect(result.code).toBe(0);
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(subDir);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
test('should normalize paths correctly', async () => {
const baseDir = mkdtempSync(join(tmpdir(), 'cd-normalize-'));
const sub1 = join(baseDir, 'sub1');
const sub2 = join(sub1, 'sub2');
mkdirSync(sub2, { recursive: true });
const originalCwd = process.cwd();
try {
// Test path with ./ and ../
await $`cd ${baseDir}`;
await $`cd ./sub1/../sub1/sub2`;
const pwd = await $`pwd`;
expect(pwd.stdout.trim()).toBe(sub2);
await $`cd ${originalCwd}`;
} finally {
rmSync(baseDir, { recursive: true, force: true });
}
});
});