test: shell out to rmdir first on Windows

cmd's `rmdir` is hardened to deal with Windows edge cases, like
lingering processes, indexing, and AV checks. So we give it a try first.

* Added `opts = { spawn = true }` to opt-out of spawning
* test-pipeconnectwrap.js - spawning messes up async_hooks state

PR-URL: https://github.com/nodejs/node/pull/28035
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
This commit is contained in:
Refael Ackermann 2019-06-02 16:19:26 -04:00
parent 546d6cdd9e
commit 59f666cd39
3 changed files with 61 additions and 23 deletions

View File

@ -5,11 +5,11 @@ const assert = require('assert');
const tick = require('../common/tick'); const tick = require('../common/tick');
const initHooks = require('./init-hooks'); const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks'); const { checkInvocations } = require('./hook-checks');
const tmpdir = require('../common/tmpdir');
const net = require('net'); const net = require('net');
const tmpdir = require('../common/tmpdir'); // Spawning messes up `async_hooks` state.
tmpdir.refresh(); tmpdir.refresh({ spawn: false });
const hooks = initHooks(); const hooks = initHooks();
hooks.enable(); hooks.enable();

View File

@ -852,7 +852,11 @@ The `tmpdir` module supports the use of a temporary directory for testing.
The realpath of the testing temporary directory. The realpath of the testing temporary directory.
### refresh() ### refresh([opts])
* `opts` [&lt;Object>] (optional) Extra options.
* `spawn` [&lt;boolean>] (default: `true`) Indicates that `refresh` is allowed
to optionally spawn a subprocess.
Deletes and recreates the testing temporary directory. Deletes and recreates the testing temporary directory.

View File

@ -1,31 +1,65 @@
/* eslint-disable node-core/require-common-first, node-core/required-modules */ /* eslint-disable node-core/require-common-first, node-core/required-modules */
'use strict'; 'use strict';
const { execSync } = require('child_process');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { debuglog } = require('util');
function rimrafSync(p) { const debug = debuglog('test/tmpdir');
let st;
try { function rimrafSync(pathname, { spawn = true } = {}) {
st = fs.lstatSync(p); const st = (() => {
} catch (e) { try {
if (e.code === 'ENOENT') return fs.lstatSync(pathname);
return; } 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}`, { timout: 1000 });
} catch (e) {
// Attempt failed. Log and carry on.
debug(e);
}
} }
try { try {
if (st && st.isDirectory()) if (st.isDirectory())
rmdirSync(p, null); rmdirSync(pathname, null);
else else
fs.unlinkSync(p); fs.unlinkSync(pathname);
} catch (e) { } catch (e) {
if (e.code === 'ENOENT') debug(e);
return; switch (e.code) {
if (e.code === 'EPERM') case 'ENOENT':
return rmdirSync(p, e); // It's not there anymore. Work is done. Exiting.
if (e.code !== 'EISDIR') return;
throw e;
rmdirSync(p, e); 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);
} }
} }
@ -62,8 +96,8 @@ if (process.env.TEST_THREAD_ID) {
const tmpPath = path.join(testRoot, tmpdirName); const tmpPath = path.join(testRoot, tmpdirName);
function refresh() { function refresh(opts = {}) {
rimrafSync(this.path); rimrafSync(this.path, opts);
fs.mkdirSync(this.path); fs.mkdirSync(this.path);
} }