-
Notifications
You must be signed in to change notification settings - Fork 755
Expand file tree
/
Copy pathrm.js
More file actions
310 lines (273 loc) · 10.7 KB
/
Copy pathrm.js
File metadata and controls
310 lines (273 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
const fs = require('fs');
const path = require('path');
const test = require('ava');
const shell = require('..');
const utils = require('./utils/utils');
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.cp('-r', 'test/resources', t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Invalids
//
test('no args', t => {
const result = shell.rm();
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'rm: no paths given');
});
test('file does not exist', t => {
const result = shell.rm('asdfasdf');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'rm: no such file or directory: asdfasdf');
});
test('cannot delete a directory without recursive flag', t => {
const result = shell.rm(`${t.context.tmp}/rm`);
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'rm: path is a directory');
});
test('only an option', t => {
const result = shell.rm('-f');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'rm: no paths given');
});
test('invalid option', t => {
const result = shell.rm('-@', 'test/resources/file1');
t.truthy(shell.error());
t.is(result.code, 1);
t.truthy(fs.existsSync('test/resources/file1'));
t.is(result.stderr, 'rm: option not recognized: @');
});
//
// Valids
//
test('file does not exist, but -f specified', t => {
const result = shell.rm('-f', 'asdfasdf');
t.falsy(shell.error());
t.is(result.code, 0);
});
test('directory does not exist, but -fr specified', t => {
const result = shell.rm('-fr', 'fake_dir/');
t.falsy(shell.error());
t.is(result.code, 0);
});
test('directory does not exist, but *only -f* specified', t => {
const result = shell.rm('-f', 'fake_dir/');
t.falsy(shell.error());
t.is(result.code, 0);
});
test('file (in fake dir) does not exist, but -f specified', t => {
const result = shell.rm('-f', 'fake_dir/asdfasdf');
t.falsy(shell.error());
t.is(result.code, 0);
});
test('dir (in fake dir) does not exist, but -fr specified', t => {
const result = shell.rm('-fr', 'fake_dir/sub/');
t.falsy(shell.error());
t.is(result.code, 0);
});
test('simple rm', t => {
t.truthy(fs.existsSync(`${t.context.tmp}/file1`));
const result = shell.rm(`${t.context.tmp}/file1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/file1`));
});
test('recursive dir removal: -r option', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
const result = shell.rm('-rf', `${t.context.tmp}/a`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/a`));
});
test('-R option does the same thing', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
const result = shell.rm('-Rf', `${t.context.tmp}/a`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/a`));
});
test('recursive dir removal - absolute path', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
const result = shell.rm('-Rf', path.resolve(`./${t.context.tmp}/a`));
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/a`));
});
test('wildcard', t => {
const result = shell.rm(`${t.context.tmp}/file*`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/file1`));
t.falsy(fs.existsSync(`${t.context.tmp}/file2`));
t.falsy(fs.existsSync(`${t.context.tmp}/file1.js`));
t.falsy(fs.existsSync(`${t.context.tmp}/file2.js`));
});
test('recursive dir removal', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
shell.mkdir('-p', `${t.context.tmp}/b`);
shell.mkdir('-p', `${t.context.tmp}/c`);
shell.mkdir('-p', `${t.context.tmp}/.hidden`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
t.truthy(fs.existsSync(`${t.context.tmp}/b`));
t.truthy(fs.existsSync(`${t.context.tmp}/c`));
t.truthy(fs.existsSync(`${t.context.tmp}/.hidden`));
const result = shell.rm('-rf', `${t.context.tmp}/*`);
t.falsy(shell.error());
t.is(result.code, 0);
const contents = fs.readdirSync(t.context.tmp);
t.is(contents.length, 1);
t.is(contents[0], '.hidden'); // shouldn't remove hidden if no .* given
});
test('recursive dir removal #2', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
shell.mkdir('-p', `${t.context.tmp}/b`);
shell.mkdir('-p', `${t.context.tmp}/c`);
shell.mkdir('-p', `${t.context.tmp}/.hidden`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
t.truthy(fs.existsSync(`${t.context.tmp}/b`));
t.truthy(fs.existsSync(`${t.context.tmp}/c`));
t.truthy(fs.existsSync(`${t.context.tmp}/.hidden`));
const result = shell.rm('-rf', `${t.context.tmp}/*`, `${t.context.tmp}/.*`);
t.falsy(shell.error());
t.is(result.code, 0);
const contents = fs.readdirSync(t.context.tmp);
t.is(contents.length, 0);
});
test('recursive dir removal - array-syntax', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
shell.mkdir('-p', `${t.context.tmp}/b`);
shell.mkdir('-p', `${t.context.tmp}/c`);
shell.mkdir('-p', `${t.context.tmp}/.hidden`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
t.truthy(fs.existsSync(`${t.context.tmp}/b`));
t.truthy(fs.existsSync(`${t.context.tmp}/c`));
t.truthy(fs.existsSync(`${t.context.tmp}/.hidden`));
const result = shell.rm('-rf', [`${t.context.tmp}/*`, `${t.context.tmp}/.*`]);
t.falsy(shell.error());
t.is(result.code, 0);
const contents = fs.readdirSync(t.context.tmp);
t.is(contents.length, 0);
});
test('removal of a read-only file (unforced)', t => {
shell.mkdir('-p', `${t.context.tmp}/readonly`);
shell.ShellString('asdf').to(`${t.context.tmp}/readonly/file1`);
fs.chmodSync(`${t.context.tmp}/readonly/file1`, '0444'); // -r--r--r--
shell.rm(`${t.context.tmp}/readonly/file1`);
t.truthy(shell.error());
t.truthy(fs.existsSync(`${t.context.tmp}/readonly/file1`)); // bash's rm always asks before removing read-only files
// here we just assume "no"
});
test('removal of a read-only file (forced)', t => {
shell.mkdir('-p', `${t.context.tmp}/readonly`);
shell.ShellString('asdf').to(`${t.context.tmp}/readonly/file2`);
fs.chmodSync(`${t.context.tmp}/readonly/file2`, '0444'); // -r--r--r--
shell.rm('-f', `${t.context.tmp}/readonly/file2`);
t.falsy(shell.error());
t.falsy(fs.existsSync(`${t.context.tmp}/readonly/file2`));
});
test('removal of a tree containing read-only files (unforced)', t => {
shell.mkdir('-p', `${t.context.tmp}/tree2`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree2/file1`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree2/file2`);
fs.chmodSync(`${t.context.tmp}/tree2/file1`, '0444'); // -r--r--r--
shell.rm('-r', `${t.context.tmp}/tree2`);
t.truthy(shell.error());
t.truthy(fs.existsSync(`${t.context.tmp}/tree2/file1`));
t.falsy(fs.existsSync(`${t.context.tmp}/tree2/file2`));
});
test('removal of a tree containing read-only files (forced)', t => {
shell.mkdir('-p', `${t.context.tmp}/tree`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree/file1`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree/file2`);
fs.chmodSync(`${t.context.tmp}/tree/file1`, '0444'); // -r--r--r--
shell.rm('-rf', `${t.context.tmp}/tree`);
t.falsy(shell.error());
t.falsy(fs.existsSync(`${t.context.tmp}/tree`));
});
test(
'removal of a sub-tree containing read-only and hidden files - glob',
t => {
shell.mkdir('-p', `${t.context.tmp}/tree3`);
shell.mkdir('-p', `${t.context.tmp}/tree3/subtree`);
shell.mkdir('-p', `${t.context.tmp}/tree3/.hidden`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree3/subtree/file`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree3/.hidden/file`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree3/file`);
fs.chmodSync(`${t.context.tmp}/tree3/file`, '0444'); // -r--r--r--
fs.chmodSync(`${t.context.tmp}/tree3/subtree/file`, '0444'); // -r--r--r--
fs.chmodSync(`${t.context.tmp}/tree3/.hidden/file`, '0444'); // -r--r--r--
shell.rm('-rf', `${t.context.tmp}/tree3/*`, `${t.context.tmp}/tree3/.*`); // erase dir contents
t.is(shell.ls(`${t.context.tmp}/tree3`).length, 0);
}
);
test(
'removal of a sub-tree containing read-only and hidden files - without glob',
t => {
shell.mkdir('-p', `${t.context.tmp}/tree4`);
shell.mkdir('-p', `${t.context.tmp}/tree4/subtree`);
shell.mkdir('-p', `${t.context.tmp}/tree4/.hidden`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree4/subtree/file`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree4/.hidden/file`);
shell.ShellString('asdf').to(`${t.context.tmp}/tree4/file`);
fs.chmodSync(`${t.context.tmp}/tree4/file`, '0444'); // -r--r--r--
fs.chmodSync(`${t.context.tmp}/tree4/subtree/file`, '0444'); // -r--r--r--
fs.chmodSync(`${t.context.tmp}/tree4/.hidden/file`, '0444'); // -r--r--r--
shell.rm('-rf', `${t.context.tmp}/tree4`); // erase dir contents
t.falsy(fs.existsSync(`${t.context.tmp}/tree4`));
}
);
test('remove symbolic link to a dir', t => {
const result = shell.rm(`${t.context.tmp}/rm/link_to_a_dir`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
});
test('rm -rf on a symbolic link to a dir deletes its contents', t => {
utils.skipOnWin(t, () => {
const result = shell.rm('-rf', `${t.context.tmp}/rm/link_to_a_dir/`);
t.falsy(shell.error());
t.is(result.code, 0);
// Both the link and original dir should remain, but contents are deleted
t.truthy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
t.falsy(fs.existsSync(`${t.context.tmp}/rm/a_dir/a_file`));
});
});
test('remove broken symbolic link', t => {
utils.skipOnWin(t, () => {
t.truthy(shell.test('-L', `${t.context.tmp}/rm/fake.lnk`));
const result = shell.rm(`${t.context.tmp}/rm/fake.lnk`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(shell.test('-L', `${t.context.tmp}/rm/fake.lnk`));
t.falsy(fs.existsSync(`${t.context.tmp}/rm/fake.lnk`));
});
});
test('recursive dir removal, for non-normalized path', t => {
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
const result = shell.rm('-rf', `${t.context.tmp}/a/.././a`);
t.falsy(shell.error());
t.is(result.code, 0);
t.falsy(fs.existsSync(`${t.context.tmp}/a`));
});
test('remove fifo', t => {
utils.skipOnWin(t, () => {
const fifo = utils.mkfifo(t.context.tmp);
const result = shell.rm(fifo);
t.falsy(shell.error());
t.is(result.code, 0);
});
});