|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | +const common = require('../common'); |
| 4 | +const tmpdir = require('../common/tmpdir'); |
| 5 | +const assert = require('assert'); |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const { validateRmOptionsSync } = require('internal/fs/utils'); |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +let count = 0; |
| 13 | +const nextDirPath = (name = 'rm') => |
| 14 | + path.join(tmpdir.path, `${name}-${count++}`); |
| 15 | + |
| 16 | +function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) { |
| 17 | + fs.mkdirSync(dirname, { recursive: true }); |
| 18 | + fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8'); |
| 19 | + |
| 20 | + const options = { flag: 'wx' }; |
| 21 | + |
| 22 | + for (let f = files; f > 0; f--) { |
| 23 | + fs.writeFileSync(path.join(dirname, `f-${depth}-${f}`), '', options); |
| 24 | + } |
| 25 | + |
| 26 | + if (createSymLinks) { |
| 27 | + // Valid symlink |
| 28 | + fs.symlinkSync( |
| 29 | + `f-${depth}-1`, |
| 30 | + path.join(dirname, `link-${depth}-good`), |
| 31 | + 'file' |
| 32 | + ); |
| 33 | + |
| 34 | + // Invalid symlink |
| 35 | + fs.symlinkSync( |
| 36 | + 'does-not-exist', |
| 37 | + path.join(dirname, `link-${depth}-bad`), |
| 38 | + 'file' |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // File with a name that looks like a glob |
| 43 | + fs.writeFileSync(path.join(dirname, '[a-z0-9].txt'), '', options); |
| 44 | + |
| 45 | + depth--; |
| 46 | + if (depth <= 0) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + for (let f = folders; f > 0; f--) { |
| 51 | + fs.mkdirSync( |
| 52 | + path.join(dirname, `folder-${depth}-${f}`), |
| 53 | + { recursive: true } |
| 54 | + ); |
| 55 | + makeNonEmptyDirectory( |
| 56 | + depth, |
| 57 | + files, |
| 58 | + folders, |
| 59 | + path.join(dirname, `d-${depth}-${f}`), |
| 60 | + createSymLinks |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function removeAsync(dir) { |
| 66 | + // Removal should fail without the recursive option. |
| 67 | + fs.rm(dir, common.mustCall((err) => { |
| 68 | + assert.strictEqual(err.syscall, 'rm'); |
| 69 | + |
| 70 | + // Removal should fail without the recursive option set to true. |
| 71 | + fs.rm(dir, { recursive: false }, common.mustCall((err) => { |
| 72 | + assert.strictEqual(err.syscall, 'rm'); |
| 73 | + |
| 74 | + // Recursive removal should succeed. |
| 75 | + fs.rm(dir, { recursive: true }, common.mustCall((err) => { |
| 76 | + assert.ifError(err); |
| 77 | + |
| 78 | + // Attempted removal should fail now because the directory is gone. |
| 79 | + fs.rm(dir, common.mustCall((err) => { |
| 80 | + assert.strictEqual(err.syscall, 'stat'); |
| 81 | + })); |
| 82 | + })); |
| 83 | + })); |
| 84 | + })); |
| 85 | +} |
| 86 | + |
| 87 | +// Test the asynchronous version |
| 88 | +{ |
| 89 | + // Create a 4-level folder hierarchy including symlinks |
| 90 | + let dir = nextDirPath(); |
| 91 | + makeNonEmptyDirectory(4, 10, 2, dir, true); |
| 92 | + removeAsync(dir); |
| 93 | + |
| 94 | + // Create a 2-level folder hierarchy without symlinks |
| 95 | + dir = nextDirPath(); |
| 96 | + makeNonEmptyDirectory(2, 10, 2, dir, false); |
| 97 | + removeAsync(dir); |
| 98 | + |
| 99 | + // Create a flat folder including symlinks |
| 100 | + dir = nextDirPath(); |
| 101 | + makeNonEmptyDirectory(1, 10, 2, dir, true); |
| 102 | + removeAsync(dir); |
| 103 | + |
| 104 | + // Should fail if target does not exist |
| 105 | + fs.rm( |
| 106 | + path.join(tmpdir.path, 'noexist.txt'), |
| 107 | + { recursive: true }, |
| 108 | + common.mustCall((err) => { |
| 109 | + assert.strictEqual(err.code, 'ENOENT'); |
| 110 | + }) |
| 111 | + ); |
| 112 | + |
| 113 | + // Should delete a file |
| 114 | + const filePath = path.join(tmpdir.path, 'rm-async-file.txt'); |
| 115 | + fs.writeFileSync(filePath, ''); |
| 116 | + fs.rm(filePath, { recursive: true }, common.mustCall((err) => { |
| 117 | + try { |
| 118 | + assert.strictEqual(err, null); |
| 119 | + assert.strictEqual(fs.existsSync(filePath), false); |
| 120 | + } finally { |
| 121 | + fs.rmSync(filePath, { force: true }); |
| 122 | + } |
| 123 | + })); |
| 124 | +} |
| 125 | + |
| 126 | +// Test the synchronous version. |
| 127 | +{ |
| 128 | + const dir = nextDirPath(); |
| 129 | + makeNonEmptyDirectory(4, 10, 2, dir, true); |
| 130 | + |
| 131 | + // Removal should fail without the recursive option set to true. |
| 132 | + assert.throws(() => { |
| 133 | + fs.rmSync(dir); |
| 134 | + }, { syscall: 'rm' }); |
| 135 | + assert.throws(() => { |
| 136 | + fs.rmSync(dir, { recursive: false }); |
| 137 | + }, { syscall: 'rm' }); |
| 138 | + |
| 139 | + // Should fail if target does not exist |
| 140 | + assert.throws(() => { |
| 141 | + fs.rmSync(path.join(tmpdir.path, 'noexist.txt'), { recursive: true }); |
| 142 | + }, { |
| 143 | + code: 'ENOENT', |
| 144 | + name: 'Error', |
| 145 | + message: /^ENOENT: no such file or directory, stat/ |
| 146 | + }); |
| 147 | + |
| 148 | + // Should delete a file |
| 149 | + const filePath = path.join(tmpdir.path, 'rm-file.txt'); |
| 150 | + fs.writeFileSync(filePath, ''); |
| 151 | + |
| 152 | + try { |
| 153 | + fs.rmSync(filePath, { recursive: true }); |
| 154 | + } finally { |
| 155 | + fs.rmSync(filePath, { force: true }); |
| 156 | + } |
| 157 | + |
| 158 | + // Recursive removal should succeed. |
| 159 | + fs.rmSync(dir, { recursive: true }); |
| 160 | + |
| 161 | + // Attempted removal should fail now because the directory is gone. |
| 162 | + assert.throws(() => fs.rmSync(dir), { syscall: 'stat' }); |
| 163 | +} |
| 164 | + |
| 165 | +// Test the Promises based version. |
| 166 | +(async () => { |
| 167 | + const dir = nextDirPath(); |
| 168 | + makeNonEmptyDirectory(4, 10, 2, dir, true); |
| 169 | + |
| 170 | + // Removal should fail without the recursive option set to true. |
| 171 | + assert.rejects(fs.promises.rm(dir), { syscall: 'rm' }); |
| 172 | + assert.rejects(fs.promises.rm(dir, { recursive: false }), { |
| 173 | + syscall: 'rm' |
| 174 | + }); |
| 175 | + |
| 176 | + // Recursive removal should succeed. |
| 177 | + await fs.promises.rm(dir, { recursive: true }); |
| 178 | + |
| 179 | + // Attempted removal should fail now because the directory is gone. |
| 180 | + assert.rejects(fs.promises.rm(dir), { syscall: 'stat' }); |
| 181 | + |
| 182 | + // Should fail if target does not exist |
| 183 | + assert.rejects(fs.promises.rm( |
| 184 | + path.join(tmpdir.path, 'noexist.txt'), |
| 185 | + { recursive: true } |
| 186 | + ), { |
| 187 | + code: 'ENOENT', |
| 188 | + name: 'Error', |
| 189 | + message: /^ENOENT: no such file or directory, stat/ |
| 190 | + }); |
| 191 | + |
| 192 | + // Should not fail if target does not exist and force option is true |
| 193 | + fs.promises.rm(path.join(tmpdir.path, 'noexist.txt'), { force: true }); |
| 194 | + |
| 195 | + // Should delete file |
| 196 | + const filePath = path.join(tmpdir.path, 'rm-promises-file.txt'); |
| 197 | + fs.writeFileSync(filePath, ''); |
| 198 | + |
| 199 | + try { |
| 200 | + await fs.promises.rm(filePath, { recursive: true }); |
| 201 | + } finally { |
| 202 | + fs.rmSync(filePath, { force: true }); |
| 203 | + } |
| 204 | +})().then(common.mustCall()); |
| 205 | + |
| 206 | +// Test input validation. |
| 207 | +{ |
| 208 | + const dir = nextDirPath(); |
| 209 | + makeNonEmptyDirectory(4, 10, 2, dir, true); |
| 210 | + const filePath = (path.join(tmpdir.path, 'rm-args-file.txt')); |
| 211 | + fs.writeFileSync(filePath, ''); |
| 212 | + |
| 213 | + const defaults = { |
| 214 | + retryDelay: 100, |
| 215 | + maxRetries: 0, |
| 216 | + recursive: false, |
| 217 | + force: false |
| 218 | + }; |
| 219 | + const modified = { |
| 220 | + retryDelay: 953, |
| 221 | + maxRetries: 5, |
| 222 | + recursive: true, |
| 223 | + force: false |
| 224 | + }; |
| 225 | + |
| 226 | + assert.deepStrictEqual(validateRmOptionsSync(filePath), defaults); |
| 227 | + assert.deepStrictEqual(validateRmOptionsSync(filePath, {}), defaults); |
| 228 | + assert.deepStrictEqual(validateRmOptionsSync(filePath, modified), modified); |
| 229 | + assert.deepStrictEqual(validateRmOptionsSync(filePath, { |
| 230 | + maxRetries: 99 |
| 231 | + }), { |
| 232 | + retryDelay: 100, |
| 233 | + maxRetries: 99, |
| 234 | + recursive: false, |
| 235 | + force: false |
| 236 | + }); |
| 237 | + |
| 238 | + [null, 'foo', 5, NaN].forEach((bad) => { |
| 239 | + assert.throws(() => { |
| 240 | + validateRmOptionsSync(filePath, bad); |
| 241 | + }, { |
| 242 | + code: 'ERR_INVALID_ARG_TYPE', |
| 243 | + name: 'TypeError', |
| 244 | + message: /^The "options" argument must be of type object\./ |
| 245 | + }); |
| 246 | + }); |
| 247 | + |
| 248 | + [undefined, null, 'foo', Infinity, function() {}].forEach((bad) => { |
| 249 | + assert.throws(() => { |
| 250 | + validateRmOptionsSync(filePath, { recursive: bad }); |
| 251 | + }, { |
| 252 | + code: 'ERR_INVALID_ARG_TYPE', |
| 253 | + name: 'TypeError', |
| 254 | + message: /^The "recursive" argument must be of type boolean\./ |
| 255 | + }); |
| 256 | + }); |
| 257 | + |
| 258 | + [undefined, null, 'foo', Infinity, function() {}].forEach((bad) => { |
| 259 | + assert.throws(() => { |
| 260 | + validateRmOptionsSync(filePath, { force: bad }); |
| 261 | + }, { |
| 262 | + code: 'ERR_INVALID_ARG_TYPE', |
| 263 | + name: 'TypeError', |
| 264 | + message: /^The "force" argument must be of type boolean\./ |
| 265 | + }); |
| 266 | + }); |
| 267 | + |
| 268 | + assert.throws(() => { |
| 269 | + validateRmOptionsSync(filePath, { retryDelay: -1 }); |
| 270 | + }, { |
| 271 | + code: 'ERR_OUT_OF_RANGE', |
| 272 | + name: 'RangeError', |
| 273 | + message: /^The value of "retryDelay" is out of range\./ |
| 274 | + }); |
| 275 | + |
| 276 | + assert.throws(() => { |
| 277 | + validateRmOptionsSync(filePath, { maxRetries: -1 }); |
| 278 | + }, { |
| 279 | + code: 'ERR_OUT_OF_RANGE', |
| 280 | + name: 'RangeError', |
| 281 | + message: /^The value of "maxRetries" is out of range\./ |
| 282 | + }); |
| 283 | +} |
0 commit comments