Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4167033

Browse files
committed
Add tests for rm -r/-f on links to dirs
1 parent 1438bf1 commit 4167033

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

test/rm.js

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ test('file does not exist', t => {
3535
t.is(result.stderr, 'rm: no such file or directory: asdfasdf');
3636
});
3737

38-
test('cannot delete a directoy without recursive flag', t => {
38+
test('cannot delete a directory without recursive flag', t => {
3939
const result = shell.rm(`${t.context.tmp}/rm`);
4040
t.truthy(shell.error());
4141
t.is(result.code, 1);
4242
t.is(result.stderr, 'rm: path is a directory');
43+
t.truthy(fs.existsSync(`${t.context.tmp}/rm`));
44+
const contents = fs.readdirSync(t.context.tmp);
45+
t.true(contents.length > 0);
4346
});
4447

4548
test('only an option', t => {
@@ -57,16 +60,23 @@ test('invalid option', t => {
5760
t.is(result.stderr, 'rm: option not recognized: @');
5861
});
5962

60-
test('remove symbolic link to a dir without -r fails', t => {
61-
utils.skipOnWin(t, () => {
62-
const result = shell.rm(`${t.context.tmp}/rm/link_to_a_dir/`);
63-
t.truthy(shell.error());
64-
t.is(result.code, 1);
65-
t.is(result.stderr, 'rm: path is a directory');
66-
t.truthy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
67-
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
68-
});
69-
});
63+
test(
64+
'cannot remove a symbolic link to a directory with trailing slash without -r flag',
65+
t => {
66+
utils.skipOnWin(t, () => {
67+
// the trailing slash signifies that we want to delete the source
68+
// directory and its contents, which can only be done with the -r flag
69+
const result = shell.rm(`${t.context.tmp}/rm/link_to_a_dir/`);
70+
t.truthy(shell.error());
71+
t.is(result.code, 1);
72+
t.is(result.stderr, 'rm: path is a directory');
73+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
74+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
75+
const contents = fs.readdirSync(`${t.context.tmp}/rm/a_dir`);
76+
t.true(contents.length > 0);
77+
});
78+
}
79+
);
7080

7181
//
7282
// Valids
@@ -290,19 +300,54 @@ test('remove symbolic link to a dir', t => {
290300
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
291301
});
292302

293-
test('rm -rf on a symbolic link to a dir deletes its contents', t => {
303+
test('rm -r, symlink to a dir, trailing slash', t => {
304+
utils.skipOnWin(t, () => {
305+
const result = shell.rm('-r', `${t.context.tmp}/rm/link_to_a_dir/`);
306+
t.truthy(shell.error());
307+
t.is(result.code, 1);
308+
// Both the link and original dir should remain, but contents are deleted
309+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
310+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
311+
t.falsy(fs.existsSync(`${t.context.tmp}/rm/a_dir/a_file`));
312+
});
313+
});
314+
315+
test('rm -rf, symlink to a dir, trailing slash', t => {
294316
utils.skipOnWin(t, () => {
295317
const result = shell.rm('-rf', `${t.context.tmp}/rm/link_to_a_dir/`);
296318
t.falsy(shell.error());
297319
t.is(result.code, 0);
298-
299320
// Both the link and original dir should remain, but contents are deleted
300321
t.truthy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
301322
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
302323
t.falsy(fs.existsSync(`${t.context.tmp}/rm/a_dir/a_file`));
303324
});
304325
});
305326

327+
test('rm -r, symlink to a dir, no trailing slash', t => {
328+
utils.skipOnWin(t, () => {
329+
const result = shell.rm('-rf', `${t.context.tmp}/rm/link_to_a_dir`);
330+
t.falsy(shell.error());
331+
t.is(result.code, 0);
332+
// The link should be deleted, but the dir and contents remain
333+
t.falsy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
334+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
335+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir/a_file`));
336+
});
337+
});
338+
339+
test('rm -rf, symlink to a dir, no trailing slash', t => {
340+
utils.skipOnWin(t, () => {
341+
const result = shell.rm('-rf', `${t.context.tmp}/rm/link_to_a_dir`);
342+
t.falsy(shell.error());
343+
t.is(result.code, 0);
344+
// The link should be deleted, but the dir and contents remain
345+
t.falsy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
346+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir`));
347+
t.truthy(fs.existsSync(`${t.context.tmp}/rm/a_dir/a_file`));
348+
});
349+
});
350+
306351
test('remove broken symbolic link', t => {
307352
utils.skipOnWin(t, () => {
308353
t.truthy(shell.test('-L', `${t.context.tmp}/rm/fake.lnk`));

0 commit comments

Comments
 (0)