forked from shelljs/shelljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopd.js
More file actions
155 lines (135 loc) · 3.58 KB
/
popd.js
File metadata and controls
155 lines (135 loc) · 3.58 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
import path from 'path';
import test from 'ava';
import shell from '..';
import mocks from './utils/mocks';
const rootDir = path.resolve();
function reset() {
shell.dirs('-c');
shell.cd(rootDir);
}
test.beforeEach(() => {
shell.config.resetForTesting();
reset();
});
test.after.always(() => {
reset();
});
//
// Valids
//
test('basic usage', t => {
shell.pushd('test/resources/pushd');
const trail = shell.popd();
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [rootDir]);
});
test('two directories on the stack', t => {
shell.pushd('test/resources/pushd');
shell.pushd('a');
const trail = shell.popd();
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [
path.resolve(rootDir, 'test/resources/pushd'),
rootDir,
]);
});
test('three directories on the stack', t => {
shell.pushd('test/resources/pushd');
shell.pushd('b');
shell.pushd('c');
const trail = shell.popd();
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [
path.resolve(rootDir, 'test/resources/pushd/b'),
path.resolve(rootDir, 'test/resources/pushd'),
rootDir,
]);
});
test('Valid by index', t => {
shell.pushd('test/resources/pushd');
const trail = shell.popd('+0');
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [rootDir]);
});
test('Using +1 option', t => {
shell.pushd('test/resources/pushd');
const trail = shell.popd('+1');
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [path.resolve(rootDir, 'test/resources/pushd')]);
});
test('Using -0 option', t => {
shell.pushd('test/resources/pushd');
const trail = shell.popd('-0');
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [path.resolve(rootDir, 'test/resources/pushd')]);
});
test('Using -1 option', t => {
shell.pushd('test/resources/pushd');
const trail = shell.popd('-1');
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [rootDir]);
});
test('Using -n option', t => {
shell.pushd('test/resources/pushd');
const trail = shell.popd('-n');
t.falsy(shell.error());
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [path.resolve(rootDir, 'test/resources/pushd')]);
});
test('Popping an empty stack', t => {
shell.popd();
t.truthy(shell.error('popd: directory stack empty\n'));
});
test('Test that rootDir is not stored', t => {
shell.cd('test/resources/pushd');
shell.pushd('b');
const trail = shell.popd();
t.falsy(shell.error());
t.is(trail[0], path.resolve(rootDir, 'test/resources/pushd'));
t.is(process.cwd(), trail[0]);
shell.popd(); // no more in the stack
t.truthy(shell.error());
});
test('quiet mode off', t => {
try {
shell.pushd('test/resources/pushd');
shell.config.silent = false;
mocks.init();
const trail = shell.popd();
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(stdout, '');
t.is(stderr, `${rootDir}\n`);
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [rootDir]);
} finally {
shell.config.silent = true;
mocks.restore();
}
});
test('quiet mode on', t => {
try {
shell.pushd('test/resources/pushd');
shell.config.silent = false;
mocks.init();
const trail = shell.popd('-q');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(stdout, '');
t.is(stderr, '');
t.is(process.cwd(), trail[0]);
t.deepEqual(trail, [rootDir]);
} finally {
shell.config.silent = true;
mocks.restore();
}
});