@@ -10,18 +10,56 @@ let count = 0;
10
10
11
11
tmpdir . refresh ( ) ;
12
12
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 } ) ;
16
15
fs . writeFileSync ( path . join ( dirname , 'text.txt' ) , 'hello' , 'utf8' ) ;
17
- count ++ ;
18
- return dirname ;
19
- }
20
16
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
+ }
24
61
62
+ function removeAsync ( dir ) {
25
63
// Removal should fail without the recursive option.
26
64
fs . rmdir ( dir , common . mustCall ( ( err ) => {
27
65
assert . strictEqual ( err . syscall , 'rmdir' ) ;
@@ -48,9 +86,31 @@ function makeNonEmptyDirectory() {
48
86
} ) ) ;
49
87
}
50
88
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
+
51
109
// Test the synchronous version.
52
110
{
53
- const dir = makeNonEmptyDirectory ( ) ;
111
+ count ++ ;
112
+ const dir = `rmdir-recursive-${ count } ` ;
113
+ makeNonEmptyDirectory ( 4 , 10 , 2 , dir , true ) ;
54
114
55
115
// Removal should fail without the recursive option set to true.
56
116
common . expectsError ( ( ) => {
@@ -72,7 +132,9 @@ function makeNonEmptyDirectory() {
72
132
73
133
// Test the Promises based version.
74
134
( async ( ) => {
75
- const dir = makeNonEmptyDirectory ( ) ;
135
+ count ++ ;
136
+ const dir = `rmdir-recursive-${ count } ` ;
137
+ makeNonEmptyDirectory ( 4 , 10 , 2 , dir , true ) ;
76
138
77
139
// Removal should fail without the recursive option set to true.
78
140
assert . rejects ( fs . promises . rmdir ( dir ) , { syscall : 'rmdir' } ) ;
0 commit comments