Skip to content

Commit

Permalink
test: use fs rimraf to refresh tmpdir
Browse files Browse the repository at this point in the history
Now that the functionality is built into core, use it to
refresh the test suite's tmpdir.

PR-URL: #30569
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
cjihrig authored and targos committed Dec 11, 2019
1 parent 18758ef commit cc4a6ed
Showing 1 changed file with 1 addition and 49 deletions.
50 changes: 1 addition & 49 deletions test/common/tmpdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,12 @@ function rimrafSync(pathname, { spawn = true } = {}) {
}
}

try {
if (st.isDirectory())
rmdirSync(pathname, null);
else
fs.unlinkSync(pathname);
} catch (e) {
debug(e);
switch (e.code) {
case 'ENOENT':
// It's not there anymore. Work is done. Exiting.
return;

case 'EPERM':
// This can happen, try again with `rmdirSync`.
break;

case 'EISDIR':
// Got 'EISDIR' even after testing `st.isDirectory()`...
// Try again with `rmdirSync`.
break;

default:
throw e;
}
rmdirSync(pathname, e);
}
fs.rmdirSync(pathname, { recursive: true, maxRetries: 5 });

if (fs.existsSync(pathname))
throw new Error(`Unable to rimraf ${pathname}`);
}

function rmdirSync(p, originalEr) {
try {
fs.rmdirSync(p);
} catch (e) {
if (e.code === 'ENOTDIR')
throw originalEr;
if (e.code === 'ENOTEMPTY' || e.code === 'EEXIST' || e.code === 'EPERM') {
const enc = process.platform === 'linux' ? 'buffer' : 'utf8';
fs.readdirSync(p, enc).forEach((f) => {
if (f instanceof Buffer) {
const buf = Buffer.concat([Buffer.from(p), Buffer.from(path.sep), f]);
rimrafSync(buf);
} else {
rimrafSync(path.join(p, f));
}
});
fs.rmdirSync(p);
return;
}
throw e;
}
}

const testRoot = process.env.NODE_TEST_DIR ?
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');

Expand Down

0 comments on commit cc4a6ed

Please sign in to comment.