forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-fs-rm.js
387 lines (335 loc) · 10.4 KB
/
test-fs-rm.js
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Flags: --expose-internals
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { execSync } = require('child_process');
const { validateRmOptionsSync } = require('internal/fs/utils');
tmpdir.refresh();
let count = 0;
const nextDirPath = (name = 'rm') =>
path.join(tmpdir.path, `${name}-${count++}`);
function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) {
fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8');
const options = { flag: 'wx' };
for (let f = files; f > 0; f--) {
fs.writeFileSync(path.join(dirname, `f-${depth}-${f}`), '', options);
}
if (createSymLinks) {
// Valid symlink
fs.symlinkSync(
`f-${depth}-1`,
path.join(dirname, `link-${depth}-good`),
'file'
);
// Invalid symlink
fs.symlinkSync(
'does-not-exist',
path.join(dirname, `link-${depth}-bad`),
'file'
);
}
// File with a name that looks like a glob
fs.writeFileSync(path.join(dirname, '[a-z0-9].txt'), '', options);
depth--;
if (depth <= 0) {
return;
}
for (let f = folders; f > 0; f--) {
fs.mkdirSync(
path.join(dirname, `folder-${depth}-${f}`),
{ recursive: true }
);
makeNonEmptyDirectory(
depth,
files,
folders,
path.join(dirname, `d-${depth}-${f}`),
createSymLinks
);
}
}
function removeAsync(dir) {
// Removal should fail without the recursive option.
fs.rm(dir, common.mustCall((err) => {
assert.strictEqual(err.syscall, 'rm');
// Removal should fail without the recursive option set to true.
fs.rm(dir, { recursive: false }, common.mustCall((err) => {
assert.strictEqual(err.syscall, 'rm');
// Recursive removal should succeed.
fs.rm(dir, { recursive: true }, common.mustSucceed(() => {
// Attempted removal should fail now because the directory is gone.
fs.rm(dir, common.mustCall((err) => {
assert.strictEqual(err.syscall, 'stat');
}));
}));
}));
}));
}
// Test the asynchronous version
{
// Create a 4-level folder hierarchy including symlinks
let dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
removeAsync(dir);
// Create a 2-level folder hierarchy without symlinks
dir = nextDirPath();
makeNonEmptyDirectory(2, 10, 2, dir, false);
removeAsync(dir);
// Same test using URL instead of a path
dir = nextDirPath();
makeNonEmptyDirectory(2, 10, 2, dir, false);
removeAsync(pathToFileURL(dir));
// Create a flat folder including symlinks
dir = nextDirPath();
makeNonEmptyDirectory(1, 10, 2, dir, true);
removeAsync(dir);
// Should fail if target does not exist
fs.rm(
path.join(tmpdir.path, 'noexist.txt'),
{ recursive: true },
common.mustCall((err) => {
assert.strictEqual(err.code, 'ENOENT');
})
);
// Should delete a file
const filePath = path.join(tmpdir.path, 'rm-async-file.txt');
fs.writeFileSync(filePath, '');
fs.rm(filePath, { recursive: true }, common.mustCall((err) => {
try {
assert.strictEqual(err, null);
assert.strictEqual(fs.existsSync(filePath), false);
} finally {
fs.rmSync(filePath, { force: true });
}
}));
}
// Test the synchronous version.
{
const dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
// Removal should fail without the recursive option set to true.
assert.throws(() => {
fs.rmSync(dir);
}, { syscall: 'rm' });
assert.throws(() => {
fs.rmSync(dir, { recursive: false });
}, { syscall: 'rm' });
// Should fail if target does not exist
assert.throws(() => {
fs.rmSync(path.join(tmpdir.path, 'noexist.txt'), { recursive: true });
}, {
code: 'ENOENT',
name: 'Error',
message: /^ENOENT: no such file or directory, stat/
});
// Should delete a file
const filePath = path.join(tmpdir.path, 'rm-file.txt');
fs.writeFileSync(filePath, '');
try {
fs.rmSync(filePath, { recursive: true });
} finally {
fs.rmSync(filePath, { force: true });
}
// Should accept URL
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt'));
fs.writeFileSync(fileURL, '');
try {
fs.rmSync(fileURL, { recursive: true });
} finally {
fs.rmSync(fileURL, { force: true });
}
// Recursive removal should succeed.
fs.rmSync(dir, { recursive: true });
// Attempted removal should fail now because the directory is gone.
assert.throws(() => fs.rmSync(dir), { syscall: 'stat' });
}
// Test the Promises based version.
(async () => {
const dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
// Removal should fail without the recursive option set to true.
await assert.rejects(fs.promises.rm(dir), { syscall: 'rm' });
await assert.rejects(fs.promises.rm(dir, { recursive: false }), {
syscall: 'rm'
});
// Recursive removal should succeed.
await fs.promises.rm(dir, { recursive: true });
// Attempted removal should fail now because the directory is gone.
await assert.rejects(fs.promises.rm(dir), { syscall: 'stat' });
// Should fail if target does not exist
await assert.rejects(fs.promises.rm(
path.join(tmpdir.path, 'noexist.txt'),
{ recursive: true }
), {
code: 'ENOENT',
name: 'Error',
message: /^ENOENT: no such file or directory, stat/
});
// Should not fail if target does not exist and force option is true
await fs.promises.rm(path.join(tmpdir.path, 'noexist.txt'), { force: true });
// Should delete file
const filePath = path.join(tmpdir.path, 'rm-promises-file.txt');
fs.writeFileSync(filePath, '');
try {
await fs.promises.rm(filePath, { recursive: true });
} finally {
fs.rmSync(filePath, { force: true });
}
// Should accept URL
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt'));
fs.writeFileSync(fileURL, '');
try {
await fs.promises.rm(fileURL, { recursive: true });
} finally {
fs.rmSync(fileURL, { force: true });
}
})().then(common.mustCall());
// Test input validation.
{
const dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
const filePath = (path.join(tmpdir.path, 'rm-args-file.txt'));
fs.writeFileSync(filePath, '');
const defaults = {
retryDelay: 100,
maxRetries: 0,
recursive: false,
force: false
};
const modified = {
retryDelay: 953,
maxRetries: 5,
recursive: true,
force: false
};
assert.deepStrictEqual(validateRmOptionsSync(filePath), defaults);
assert.deepStrictEqual(validateRmOptionsSync(filePath, {}), defaults);
assert.deepStrictEqual(validateRmOptionsSync(filePath, modified), modified);
assert.deepStrictEqual(validateRmOptionsSync(filePath, {
maxRetries: 99
}), {
retryDelay: 100,
maxRetries: 99,
recursive: false,
force: false
});
[null, 'foo', 5, NaN].forEach((bad) => {
assert.throws(() => {
validateRmOptionsSync(filePath, bad);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "options" argument must be of type object\./
});
});
[undefined, null, 'foo', Infinity, function() {}].forEach((bad) => {
assert.throws(() => {
validateRmOptionsSync(filePath, { recursive: bad });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "options\.recursive" property must be of type boolean\./
});
});
[undefined, null, 'foo', Infinity, function() {}].forEach((bad) => {
assert.throws(() => {
validateRmOptionsSync(filePath, { force: bad });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "options\.force" property must be of type boolean\./
});
});
assert.throws(() => {
validateRmOptionsSync(filePath, { retryDelay: -1 });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "options\.retryDelay" is out of range\./
});
assert.throws(() => {
validateRmOptionsSync(filePath, { maxRetries: -1 });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "options\.maxRetries" is out of range\./
});
}
{
// IBMi has a different access permission mechanism
// This test should not be run as `root`
if (!common.isIBMi && (common.isWindows || process.getuid() !== 0)) {
function makeDirectoryReadOnly(dir, mode) {
let accessErrorCode = 'EACCES';
if (common.isWindows) {
accessErrorCode = 'EPERM';
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC)"`);
} else {
fs.chmodSync(dir, mode);
}
return accessErrorCode;
}
function makeDirectoryWritable(dir) {
if (fs.existsSync(dir)) {
if (common.isWindows) {
execSync(`icacls ${dir} /remove:d "everyone"`);
} else {
fs.chmodSync(dir, 0o777);
}
}
}
{
// Check that deleting a file that cannot be accessed using rmsync throws
// https://github.com/nodejs/node/issues/38683
const dirname = nextDirPath();
const filePath = path.join(dirname, 'text.txt');
try {
fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(filePath, 'hello');
const code = makeDirectoryReadOnly(dirname, 0o444);
assert.throws(() => {
fs.rmSync(filePath, { force: true });
}, {
code,
name: 'Error',
});
} finally {
makeDirectoryWritable(dirname);
}
}
{
// Check endless recursion.
// https://github.com/nodejs/node/issues/34580
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
const middle = path.join(root, 'middle');
fs.mkdirSync(middle);
fs.mkdirSync(path.join(middle, 'leaf')); // Make `middle` non-empty
try {
const code = makeDirectoryReadOnly(middle, 0o555);
try {
assert.throws(() => {
fs.rmSync(root, { recursive: true });
}, {
code,
name: 'Error',
});
} catch (err) {
// Only fail the test if the folder was not deleted.
// as in some cases rmSync succesfully deletes read-only folders.
if (fs.existsSync(root)) {
throw err;
}
}
} finally {
makeDirectoryWritable(middle);
}
}
}
}