forked from shelljs/shelljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.js
More file actions
92 lines (74 loc) · 2.46 KB
/
Copy pathcd.js
File metadata and controls
92 lines (74 loc) · 2.46 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
var shell = require('..');
var assert = require('assert'),
path = require('path'),
fs = require('fs'),
common = require('../src/common');
shell.config.silent = true;
// save current dir
var cur = shell.pwd();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp');
//
// Invalids
//
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
var result = shell.cd('/asdfasdf'); // dir does not exist
assert.ok(shell.error());
assert.equal(result.code, 1);
assert.equal(result.stderr, 'cd: no such file or directory: /asdfasdf');
assert.equal(fs.existsSync('resources/file1'), true); // sanity check
result = shell.cd('resources/file1'); // file, not dir
assert.ok(shell.error());
assert.equal(result.code, 1);
assert.equal(result.stderr, 'cd: not a directory: resources/file1');
result = shell.cd('-'); // Haven't changed yet, so there is no previous directory
assert.ok(shell.error());
assert.equal(result.code, 1);
assert.equal(result.stderr, 'cd: could not find previous directory');
//
// Valids
//
result = shell.cd(cur);
result = shell.cd('tmp');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.equal(path.basename(process.cwd()), 'tmp');
result = shell.cd(cur);
result = shell.cd('/');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.equal(process.cwd(), path.resolve('/'));
result = shell.cd(cur);
result = shell.cd('/');
result = shell.cd('-');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.equal(process.cwd(), path.resolve(cur.toString()));
// cd + other commands
result = shell.cd(cur);
result = shell.rm('-f', 'tmp/*');
assert.equal(fs.existsSync('tmp/file1'), false);
result = shell.cd('resources');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
result = shell.cp('file1', '../tmp');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
result = shell.cd('../tmp');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.equal(fs.existsSync('file1'), true);
// Test tilde expansion
result = shell.cd('~');
assert.equal(process.cwd(), common.getUserHome());
result = shell.cd('..');
assert.notEqual(process.cwd(), common.getUserHome());
result = shell.cd('~'); // Change back to home
assert.equal(process.cwd(), common.getUserHome());
// Goes to home directory if no arguments are passed
result = shell.cd(cur);
result = shell.cd();
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(process.cwd(), common.getUserHome());
shell.exit(123);