Skip to content

Commit 44c581e

Browse files
mpaktititargos
authored andcommitted
test: add more recursive fs.rmdir() tests
Update tests to create more nested folders and files, like rimraf package tests do. Each test will create 28 nested directories and 210 files. It will also create different types of files, like symlinks. PR-URL: #29815 Reviewed-By: Ben Coe <[email protected]>
1 parent 1dde617 commit 44c581e

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

test/parallel/test-fs-rmdir-recursive.js

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,56 @@ let count = 0;
1010

1111
tmpdir.refresh();
1212

13-
function makeNonEmptyDirectory() {
14-
const dirname = `rmdir-recursive-${count}`;
15-
fs.mkdirSync(path.join(dirname, 'foo', 'bar', 'baz'), { recursive: true });
13+
function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) {
14+
fs.mkdirSync(dirname, { recursive: true });
1615
fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8');
17-
count++;
18-
return dirname;
19-
}
2016

21-
// Test the asynchronous version.
22-
{
23-
const dir = makeNonEmptyDirectory();
17+
const options = { flag: 'wx' };
18+
19+
for (let f = files; f > 0; f--) {
20+
fs.writeFileSync(path.join(dirname, `f-${depth}-${f}`), '', options);
21+
}
22+
23+
if (createSymLinks) {
24+
// Valid symlink
25+
fs.symlinkSync(
26+
`f-${depth}-1`,
27+
path.join(dirname, `link-${depth}-good`),
28+
'file'
29+
);
30+
31+
// Invalid symlink
32+
fs.symlinkSync(
33+
'does-not-exist',
34+
path.join(dirname, `link-${depth}-bad`),
35+
'file'
36+
);
37+
}
38+
39+
// File with a name that looks like a glob
40+
fs.writeFileSync(path.join(dirname, '[a-z0-9].txt'), '', options);
41+
42+
depth--;
43+
if (depth <= 0) {
44+
return;
45+
}
46+
47+
for (let f = folders; f > 0; f--) {
48+
fs.mkdirSync(
49+
path.join(dirname, `folder-${depth}-${f}`),
50+
{ recursive: true }
51+
);
52+
makeNonEmptyDirectory(
53+
depth,
54+
files,
55+
folders,
56+
path.join(dirname, `d-${depth}-${f}`),
57+
createSymLinks
58+
);
59+
}
60+
}
2461

62+
function removeAsync(dir) {
2563
// Removal should fail without the recursive option.
2664
fs.rmdir(dir, common.mustCall((err) => {
2765
assert.strictEqual(err.syscall, 'rmdir');
@@ -48,9 +86,31 @@ function makeNonEmptyDirectory() {
4886
}));
4987
}
5088

89+
// Test the asynchronous version
90+
{
91+
// Create a 4-level folder hierarchy including symlinks
92+
let dir = `rmdir-recursive-${count}`;
93+
makeNonEmptyDirectory(4, 10, 2, dir, true);
94+
removeAsync(dir);
95+
96+
// Create a 2-level folder hierarchy without symlinks
97+
count++;
98+
dir = `rmdir-recursive-${count}`;
99+
makeNonEmptyDirectory(2, 10, 2, dir, false);
100+
removeAsync(dir);
101+
102+
// Create a flat folder including symlinks
103+
count++;
104+
dir = `rmdir-recursive-${count}`;
105+
makeNonEmptyDirectory(1, 10, 2, dir, true);
106+
removeAsync(dir);
107+
}
108+
51109
// Test the synchronous version.
52110
{
53-
const dir = makeNonEmptyDirectory();
111+
count++;
112+
const dir = `rmdir-recursive-${count}`;
113+
makeNonEmptyDirectory(4, 10, 2, dir, true);
54114

55115
// Removal should fail without the recursive option set to true.
56116
common.expectsError(() => {
@@ -72,7 +132,9 @@ function makeNonEmptyDirectory() {
72132

73133
// Test the Promises based version.
74134
(async () => {
75-
const dir = makeNonEmptyDirectory();
135+
count++;
136+
const dir = `rmdir-recursive-${count}`;
137+
makeNonEmptyDirectory(4, 10, 2, dir, true);
76138

77139
// Removal should fail without the recursive option set to true.
78140
assert.rejects(fs.promises.rmdir(dir), { syscall: 'rmdir' });

0 commit comments

Comments
 (0)