2019-05-12 15:11:13 +08:00
|
|
|
/* eslint-disable node-core/require-common-first, node-core/required-modules */
|
2017-12-25 14:38:11 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2019-07-30 14:50:21 +08:00
|
|
|
const { isMainThread } = require('worker_threads');
|
2017-12-25 14:38:11 +08:00
|
|
|
|
2020-10-04 10:20:28 +08:00
|
|
|
function rmSync(pathname) {
|
|
|
|
fs.rmSync(pathname, { maxRetries: 3, recursive: true, force: true });
|
2017-12-25 14:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const testRoot = process.env.NODE_TEST_DIR ?
|
|
|
|
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
|
|
|
|
|
|
|
|
// Using a `.` prefixed name, which is the convention for "hidden" on POSIX,
|
|
|
|
// gets tools to ignore it by default or by simple rules, especially eslint.
|
2019-07-26 03:20:57 +08:00
|
|
|
const tmpdirName = '.tmp.' +
|
|
|
|
(process.env.TEST_SERIAL_ID || process.env.TEST_THREAD_ID || '0');
|
2018-08-28 06:13:55 +08:00
|
|
|
const tmpPath = path.join(testRoot, tmpdirName);
|
|
|
|
|
2019-07-30 14:50:21 +08:00
|
|
|
let firstRefresh = true;
|
2019-12-12 17:05:38 +08:00
|
|
|
function refresh() {
|
2020-10-04 10:20:28 +08:00
|
|
|
rmSync(this.path);
|
2018-08-28 06:13:55 +08:00
|
|
|
fs.mkdirSync(this.path);
|
2019-07-30 14:50:21 +08:00
|
|
|
|
|
|
|
if (firstRefresh) {
|
|
|
|
firstRefresh = false;
|
|
|
|
// Clean only when a test uses refresh. This allows for child processes to
|
|
|
|
// use the tmpdir and only the parent will clean on exit.
|
|
|
|
process.on('exit', onexit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onexit() {
|
|
|
|
// Change directory to avoid possible EBUSY
|
|
|
|
if (isMainThread)
|
|
|
|
process.chdir(testRoot);
|
|
|
|
|
|
|
|
try {
|
2020-10-04 10:20:28 +08:00
|
|
|
rmSync(tmpPath);
|
2019-07-30 14:50:21 +08:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Can\'t clean tmpdir:', tmpPath);
|
|
|
|
|
|
|
|
const files = fs.readdirSync(tmpPath);
|
|
|
|
console.error('Files blocking:', files);
|
|
|
|
|
|
|
|
if (files.some((f) => f.startsWith('.nfs'))) {
|
|
|
|
// Warn about NFS "silly rename"
|
|
|
|
console.error('Note: ".nfs*" might be files that were open and ' +
|
|
|
|
'unlinked but not closed.');
|
|
|
|
console.error('See http://nfs.sourceforge.net/#faq_d2 for details.');
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-08-28 06:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
path: tmpPath,
|
|
|
|
refresh
|
2017-12-25 14:38:11 +08:00
|
|
|
};
|