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 <bencoe@gmail.com>
1 parent 1dde617 commit 44c581e

1 file changed

Lines changed: 73 additions & 11 deletions

File tree

‎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-
functionmakeNonEmptyDirectory(){
14-
constdirname=`rmdir-recursive-${count}`;
15-
fs.mkdirSync(path.join(dirname,'foo','bar','baz'),{recursive: true});
13+
functionmakeNonEmptyDirectory(depth,files,folders,dirname,createSymLinks){
14+
fs.mkdirSync(dirname,{recursive: true});
1615
fs.writeFileSync(path.join(dirname,'text.txt'),'hello','utf8');
17-
count++;
18-
returndirname;
19-
}
2016

21-
// Test the asynchronous version.
22-
{
23-
constdir=makeNonEmptyDirectory();
17+
constoptions={flag: 'wx'};
18+
19+
for(letf=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(letf=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+
functionremoveAsync(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+
letdir=`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-
constdir=makeNonEmptyDirectory();
111+
count++;
112+
constdir=`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-
constdir=makeNonEmptyDirectory();
135+
count++;
136+
constdir=`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)