Skip to content

Commit

Permalink
test: move tmpdir to submodule of common
Browse files Browse the repository at this point in the history
Move tmpdir functionality to its own module (common/tmpdir).

PR-URL: nodejs#17856
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
Trott committed Feb 1, 2018
1 parent 6035bee commit bf6ce47
Show file tree
Hide file tree
Showing 154 changed files with 643 additions and 454 deletions.
13 changes: 3 additions & 10 deletions benchmark/http/http_server_for_chunky_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

const assert = require('assert');
const http = require('http');
const fs = require('fs');
const { fork } = require('child_process');
const common = require('../common.js');
const { PIPE, tmpDir } = require('../../test/common');
const { PIPE } = require('../../test/common');
const tmpdir = require('../../test/common/tmpdir');
process.env.PIPE_NAME = PIPE;

try {
fs.accessSync(tmpDir, fs.F_OK);
} catch (e) {
fs.mkdirSync(tmpDir);
}
tmpdir.refresh();

var server;
try {
fs.unlinkSync(process.env.PIPE_NAME);
} catch (e) { /* ignore */ }

server = http.createServer(function(req, res) {
const headers = {
Expand Down
8 changes: 4 additions & 4 deletions benchmark/module/module-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const fs = require('fs');
const path = require('path');
const common = require('../common.js');

const { refreshTmpDir, tmpDir } = require('../../test/common');
const benchmarkDirectory = path.join(tmpDir, 'nodejs-benchmark-module');
const tmpdir = require('../../test/common/tmpdir');
const benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');

const bench = common.createBenchmark(main, {
thousands: [50],
Expand All @@ -15,7 +15,7 @@ const bench = common.createBenchmark(main, {
function main({ thousands, fullPath, useCache }) {
const n = thousands * 1e3;

refreshTmpDir();
tmpdir.refresh();
try { fs.mkdirSync(benchmarkDirectory); } catch (e) {}

for (var i = 0; i <= n; i++) {
Expand All @@ -35,7 +35,7 @@ function main({ thousands, fullPath, useCache }) {
else
measureDir(n, useCache === 'true');

refreshTmpDir();
tmpdir.refresh();
}

function measureFull(n, useCache) {
Expand Down
5 changes: 3 additions & 2 deletions test/addons/load-long-path/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const fs = require('fs');
const path = require('path');
const assert = require('assert');

common.refreshTmpDir();
const tmpdir = require('../../common/tmpdir');
tmpdir.refresh();

// make a path that is more than 260 chars long.
// Any given folder cannot have a name longer than 260 characters,
// so create 10 nested folders each with 30 character long names.
let addonDestinationDir = path.resolve(common.tmpDir);
let addonDestinationDir = path.resolve(tmpdir.path);

for (let i = 0; i < 10; i++) {
addonDestinationDir = path.join(addonDestinationDir, 'x'.repeat(30));
Expand Down
5 changes: 3 additions & 2 deletions test/addons/symlinked-module/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const assert = require('assert');
// This test should pass in Node.js v4 and v5. This test will pass in Node.js
// with https://github.com/nodejs/node/pull/5950 reverted.

common.refreshTmpDir();
const tmpdir = require('../../common/tmpdir');
tmpdir.refresh();

const addonPath = path.join(__dirname, 'build', common.buildType);
const addonLink = path.join(common.tmpDir, 'addon');
const addonLink = path.join(tmpdir.path, 'addon');

try {
fs.symlinkSync(addonPath, addonLink);
Expand Down
3 changes: 2 additions & 1 deletion test/async-hooks/test-graph.pipeconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const verifyGraph = require('./verify-graph');

const net = require('net');

common.refreshTmpDir();
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const hooks = initHooks();
hooks.enable();
Expand Down
3 changes: 2 additions & 1 deletion test/async-hooks/test-pipeconnectwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const { checkInvocations } = require('./hook-checks');

const net = require('net');

common.refreshTmpDir();
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const hooks = initHooks();
hooks.enable();
Expand Down
24 changes: 14 additions & 10 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This directory contains modules used to test the Node.js implementation.
* [Fixtures module](#fixtures-module)
* [HTTP2 module](#http2-module)
* [Internet module](#internet-module)
* [tmpdir module](#tmpdir-module)
* [WPT module](#wpt-module)

## Benchmark Module
Expand Down Expand Up @@ -332,11 +333,6 @@ A port number for tests to use if one is needed.

Logs '1..0 # Skipped: ' + `msg`

### refreshTmpDir()
* return [&lt;String>]

Deletes the testing 'tmp' directory and recreates it.

### restoreStderr()

Restore the original `process.stderr.write`. Used to restore `stderr` to its
Expand Down Expand Up @@ -379,11 +375,6 @@ Platform normalizes the `pwd` command.

Synchronous version of `spawnPwd`.

### tmpDir
* [&lt;String>]

The realpath of the 'tmp' directory.

## Countdown Module

The `Countdown` module provides a simple countdown mechanism for tests that
Expand Down Expand Up @@ -665,6 +656,19 @@ via `NODE_TEST_*` environment variables. For example, to configure
`internet.addresses.INET_HOST`, set the environment
variable `NODE_TEST_INET_HOST` to a specified host.

## tmpdir Module

The `tmpdir` module supports the use of a temporary directory for testing.

### path
* [&lt;String>]

The realpath of the testing temporary directory.

### refresh()

Deletes and recreates the testing temporary directory.

## WPT Module

The wpt.js module is a port of parts of
Expand Down
66 changes: 2 additions & 64 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ const stream = require('stream');
const util = require('util');
const Timer = process.binding('timer_wrap').Timer;
const { fixturesDir } = require('./fixtures');

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

const noop = () => {};

// 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.
let tmpDirName = '.tmp';

Object.defineProperty(exports, 'PORT', {
get: () => {
if (+process.env.TEST_PARALLEL) {
Expand Down Expand Up @@ -120,62 +114,6 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
}).enable();
}

function rimrafSync(p) {
let st;
try {
st = fs.lstatSync(p);
} catch (e) {
if (e.code === 'ENOENT')
return;
}

try {
if (st && st.isDirectory())
rmdirSync(p, null);
else
fs.unlinkSync(p);
} catch (e) {
if (e.code === 'ENOENT')
return;
if (e.code === 'EPERM')
return rmdirSync(p, e);
if (e.code !== 'EISDIR')
throw e;
rmdirSync(p, e);
}
}

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 = exports.isLinux ? '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);
}
}
}

exports.refreshTmpDir = function() {
rimrafSync(exports.tmpDir);
fs.mkdirSync(exports.tmpDir);
};

if (process.env.TEST_THREAD_ID) {
tmpDirName += `.${process.env.TEST_THREAD_ID}`;
}
exports.tmpDir = path.join(testRoot, tmpDirName);

let opensslCli = null;
let inFreeBSDJail = null;
let localhostIPv4 = null;
Expand Down Expand Up @@ -269,7 +207,7 @@ Object.defineProperty(exports, 'hasFipsCrypto', {
});

{
const localRelative = path.relative(process.cwd(), `${exports.tmpDir}/`);
const localRelative = path.relative(process.cwd(), `${tmpdir.path}/`);
const pipePrefix = exports.isWindows ? '\\\\.\\pipe\\' : localRelative;
const pipeName = `node-test.${process.pid}.sock`;
exports.PIPE = path.join(pipePrefix, pipeName);
Expand Down
67 changes: 67 additions & 0 deletions test/common/tmpdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable required-modules */
'use strict';

const fs = require('fs');
const path = require('path');

function rimrafSync(p) {
let st;
try {
st = fs.lstatSync(p);
} catch (e) {
if (e.code === 'ENOENT')
return;
}

try {
if (st && st.isDirectory())
rmdirSync(p, null);
else
fs.unlinkSync(p);
} catch (e) {
if (e.code === 'ENOENT')
return;
if (e.code === 'EPERM')
return rmdirSync(p, e);
if (e.code !== 'EISDIR')
throw e;
rmdirSync(p, e);
}
}

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);
}
}
}

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.
let tmpdirName = '.tmp';
if (process.env.TEST_THREAD_ID) {
tmpdirName += `.${process.env.TEST_THREAD_ID}`;
}
exports.path = path.join(testRoot, tmpdirName);

exports.refresh = () => {
rimrafSync(exports.path);
fs.mkdirSync(exports.path);
};
5 changes: 3 additions & 2 deletions test/es-module/test-esm-preserve-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const assert = require('assert');
const path = require('path');
const fs = require('fs');

common.refreshTmpDir();
const tmpDir = common.tmpDir;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const tmpDir = tmpdir.path;

const entry = path.join(tmpDir, 'entry.js');
const real = path.join(tmpDir, 'real.js');
Expand Down
5 changes: 3 additions & 2 deletions test/es-module/test-esm-symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const assert = require('assert');
const path = require('path');
const fs = require('fs');

common.refreshTmpDir();
const tmpDir = common.tmpDir;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const tmpDir = tmpdir.path;

const entry = path.join(tmpDir, 'entry.mjs');
const real = path.join(tmpDir, 'index.mjs');
Expand Down
5 changes: 3 additions & 2 deletions test/known_issues/test-cwd-enoent-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const fs = require('fs');
if (process.argv[2] === 'child') {
// Do nothing.
} else {
common.refreshTmpDir();
const dir = fs.mkdtempSync(`${common.tmpDir}/`);
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const dir = fs.mkdtempSync(`${tmpdir.path}/`);
process.chdir(dir);
fs.rmdirSync(dir);
assert.throws(process.cwd,
Expand Down
7 changes: 4 additions & 3 deletions test/known_issues/test-module-deleted-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const file = path.join(common.tmpDir, 'test-extensions.foo.bar');
const tmpdir = require('../common/tmpdir');
const file = path.join(tmpdir.path, 'test-extensions.foo.bar');

common.refreshTmpDir();
tmpdir.refresh();
fs.writeFileSync(file, '', 'utf8');
require.extensions['.foo.bar'] = (module, path) => {};
delete require.extensions['.foo.bar'];
require.extensions['.bar'] = common.mustCall((module, path) => {
assert.strictEqual(module.id, file);
assert.strictEqual(path, file);
});
require(path.join(common.tmpDir, 'test-extensions'));
require(path.join(tmpdir.path, 'test-extensions'));
7 changes: 4 additions & 3 deletions test/parallel/test-benchmark-fs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

const common = require('../common');
require('../common');
const runBenchmark = require('../common/benchmark');

common.refreshTmpDir();
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

runBenchmark('fs', [
'n=1',
Expand All @@ -16,4 +17,4 @@ runBenchmark('fs', [
'statSyncType=fstatSync',
'encodingType=buf',
'filesize=1024'
], { NODE_TMPDIR: common.tmpDir, NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
], { NODE_TMPDIR: tmpdir.path, NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
Loading

0 comments on commit bf6ce47

Please sign in to comment.