Skip to content

Commit

Permalink
test: remove custom rimraf
Browse files Browse the repository at this point in the history
Use internal recursive rmdir() in the common/tmpdir module we use in
tests.
  • Loading branch information
Trott committed Dec 10, 2019
1 parent 8a5c7f6 commit b471bd8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 48 deletions.
3 changes: 1 addition & 2 deletions test/async-hooks/test-pipeconnectwrap.js
Expand Up @@ -8,8 +8,7 @@ const { checkInvocations } = require('./hook-checks');
const tmpdir = require('../common/tmpdir');
const net = require('net');

// Spawning messes up `async_hooks` state.
tmpdir.refresh({ spawn: false });
tmpdir.refresh();

const hooks = initHooks();
hooks.enable();
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-statwatcher.js
Expand Up @@ -11,7 +11,7 @@ const path = require('path');
if (!common.isMainThread)
common.skip('Worker bootstrapping works differently -> different async IDs');

tmpdir.refresh({ spawn: false });
tmpdir.refresh();

const file1 = path.join(tmpdir.path, 'file1');
const file2 = path.join(tmpdir.path, 'file2');
Expand Down
6 changes: 1 addition & 5 deletions test/common/README.md
Expand Up @@ -898,11 +898,7 @@ The `tmpdir` module supports the use of a temporary directory for testing.

The realpath of the testing temporary directory.

### refresh(\[opts\])

* `opts` [<Object>][] (optional) Extra options.
* `spawn` [<boolean>][] (default: `true`) Indicates that `refresh` is
allowed to optionally spawn a subprocess.
### refresh()

Deletes and recreates the testing temporary directory.

Expand Down
49 changes: 9 additions & 40 deletions test/common/tmpdir.js
@@ -1,47 +1,10 @@
/* eslint-disable node-core/require-common-first, node-core/required-modules */
'use strict';

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { debuglog } = require('util');
const { isMainThread } = require('worker_threads');

const debug = debuglog('test/tmpdir');

function rimrafSync(pathname, { spawn = true } = {}) {
const st = (() => {
try {
return fs.lstatSync(pathname);
} catch (e) {
if (fs.existsSync(pathname))
throw new Error(`Something wonky happened rimrafing ${pathname}`);
debug(e);
}
})();

// If (!st) then nothing to do.
if (!st) {
return;
}

// On Windows first try to delegate rmdir to a shell.
if (spawn && process.platform === 'win32' && st.isDirectory()) {
try {
// Try `rmdir` first.
execSync(`rmdir /q /s ${pathname}`, { timeout: 1000 });
} catch (e) {
// Attempt failed. Log and carry on.
debug(e);
}
}

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

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

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

Expand All @@ -52,8 +15,14 @@ const tmpdirName = '.tmp.' +
const tmpPath = path.join(testRoot, tmpdirName);

let firstRefresh = true;
function refresh(opts = {}) {
rimrafSync(this.path, opts);
function refresh() {
try {
fs.rmdirSync(this.path, { recursive: true });
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
fs.mkdirSync(this.path);

if (firstRefresh) {
Expand All @@ -70,7 +39,7 @@ function onexit() {
process.chdir(testRoot);

try {
rimrafSync(tmpPath, { spawn: false });
fs.rmdirSync(tmpPath, { recursive: true });
} catch (e) {
console.error('Can\'t clean tmpdir:', tmpPath);

Expand Down

0 comments on commit b471bd8

Please sign in to comment.