★ wanayoo — archive 1999 https://github.com/nodejs/node/commit/eb252527e5Nouvelle recherche | Portail wanayoo
Skip to content
Permalink
Browse files
test: move tmpdir to submodule of common
Move tmpdir functionality to its own module (common/tmpdir).

PR-URL: #17856
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
Trott authored and MylesBorins committed Feb 21, 2018
1 parent ae372f0 commit eb252527e54a6fe3262133d5c10d48d64c759927
Showing with 645 additions and 456 deletions.
  1. +3 −10 benchmark/http/http_server_for_chunky_client.js
  2. +4 −4 benchmark/module/module-loader.js
  3. +3 −2 test/addons/load-long-path/test.js
  4. +3 −2 test/addons/symlinked-module/test.js
  5. +2 −1 test/async-hooks/test-graph.pipeconnect.js
  6. +2 −1 test/async-hooks/test-pipeconnectwrap.js
  7. +14 −10 test/common/README.md
  8. +2 −64 test/common/index.js
  9. +67 −0 test/common/tmpdir.js
  10. +3 −2 test/es-module/test-esm-preserve-symlinks.js
  11. +3 −2 test/es-module/test-esm-symlink.js
  12. +3 −2 test/known_issues/test-cwd-enoent-file.js
  13. +4 −3 test/known_issues/test-module-deleted-extensions.js
  14. +4 −3 test/parallel/test-benchmark-fs.js
  15. +3 −2 test/parallel/test-child-process-fork-exec-path.js
  16. +3 −2 test/parallel/test-cli-node-options-disallowed.js
  17. +3 −2 test/parallel/test-cli-node-options.js
  18. +5 −4 test/parallel/test-cluster-cwd.js
  19. +2 −1 test/parallel/test-cluster-eaccess.js
  20. +2 −1 test/parallel/test-cluster-http-pipe.js
  21. +4 −2 test/parallel/test-cluster-net-listen-relative-path.js
  22. +4 −3 test/parallel/test-crypto-sign-verify.js
  23. +3 −2 test/parallel/test-cwd-enoent-preload.js
  24. +4 −2 test/parallel/test-cwd-enoent-repl.js
  25. +4 −2 test/parallel/test-cwd-enoent.js
  26. +3 −2 test/parallel/test-file-write-stream.js
  27. +5 −3 test/parallel/test-file-write-stream2.js
  28. +4 −2 test/parallel/test-file-write-stream3.js
  29. +6 −4 test/parallel/test-fs-access.js
  30. +7 −6 test/parallel/test-fs-append-file-sync.js
  31. +9 −7 test/parallel/test-fs-append-file.js
  32. +4 −3 test/parallel/test-fs-buffer.js
  33. +5 −3 test/parallel/test-fs-buffertype-writesync.js
  34. +5 −4 test/parallel/test-fs-chmod.js
  35. +3 −2 test/parallel/test-fs-copyfile.js
  36. +3 −2 test/parallel/test-fs-fsync.js
  37. +4 −3 test/parallel/test-fs-link.js
  38. +5 −3 test/parallel/test-fs-long-path.js
  39. +3 −2 test/parallel/test-fs-make-callback.js
  40. +3 −2 test/parallel/test-fs-mkdir-rmdir.js
  41. +5 −4 test/parallel/test-fs-mkdir.js
  42. +8 −7 test/parallel/test-fs-mkdtemp.js
  43. +3 −2 test/parallel/test-fs-non-number-arguments-throw.js
  44. +3 −2 test/parallel/test-fs-open-flags.js
  45. +4 −3 test/parallel/test-fs-open-numeric-flags.js
  46. +8 −7 test/parallel/test-fs-options-immutable.js
  47. +3 −2 test/parallel/test-fs-promisified.js
  48. +4 −3 test/parallel/test-fs-read-stream-fd.js
  49. +4 −3 test/parallel/test-fs-readdir-ucs2.js
  50. +4 −2 test/parallel/test-fs-readdir.js
  51. +4 −2 test/parallel/test-fs-readfile-pipe-large.js
  52. +5 −3 test/parallel/test-fs-readfile-unlink.js
  53. +4 −2 test/parallel/test-fs-readfilesync-pipe-large.js
  54. +5 −3 test/parallel/test-fs-realpath.js
  55. +5 −3 test/parallel/test-fs-sir-writes-alot.js
  56. +5 −4 test/parallel/test-fs-stream-double-close.js
  57. +5 −3 test/parallel/test-fs-symlink-dir-junction-relative.js
  58. +4 −2 test/parallel/test-fs-symlink-dir-junction.js
  59. +3 −2 test/parallel/test-fs-symlink.js
  60. +3 −2 test/parallel/test-fs-syncwritestream.js
  61. +4 −2 test/parallel/test-fs-truncate-GH-6233.js
  62. +3 −2 test/parallel/test-fs-truncate-fd.js
  63. +4 −3 test/parallel/test-fs-truncate-sync.js
  64. +3 −2 test/parallel/test-fs-truncate.js
  65. +10 −9 test/parallel/test-fs-utimes.js
  66. +6 −5 test/parallel/test-fs-watch-encoding.js
  67. +4 −2 test/parallel/test-fs-watch-recursive.js
  68. +3 −2 test/parallel/test-fs-watch.js
  69. +5 −3 test/parallel/test-fs-watchfile.js
  70. +8 −7 test/parallel/test-fs-write-buffer.js
  71. +4 −3 test/parallel/test-fs-write-file-buffer.js
  72. +4 −3 test/parallel/test-fs-write-file-invalid-path.js
  73. +5 −4 test/parallel/test-fs-write-file-sync.js
  74. +3 −2 test/parallel/test-fs-write-file-uint8array.js
  75. +6 −5 test/parallel/test-fs-write-file.js
  76. +4 −2 test/parallel/test-fs-write-stream-autoclose-option.js
  77. +5 −3 test/parallel/test-fs-write-stream-change-open.js
  78. +4 −3 test/parallel/test-fs-write-stream-close-without-callback.js
  79. +5 −4 test/parallel/test-fs-write-stream-double-close.js
  80. +4 −3 test/parallel/test-fs-write-stream-encoding.js
  81. +4 −3 test/parallel/test-fs-write-stream-end.js
  82. +3 −2 test/parallel/test-fs-write-stream-err.js
  83. +4 −2 test/parallel/test-fs-write-stream-throw-type-error.js
  84. +5 −3 test/parallel/test-fs-write-stream.js
  85. +3 −2 test/parallel/test-fs-write-string-coerce.js
  86. +4 −3 test/parallel/test-fs-write-sync.js
  87. +7 −5 test/parallel/test-fs-write.js
  88. +4 −2 test/parallel/test-http-agent-getname.js
  89. +4 −2 test/parallel/test-http-chunk-problem.js
  90. +2 −1 test/parallel/test-http-client-abort-keep-alive-queued-unix-socket.js
  91. +2 −1 test/parallel/test-http-client-abort-unix-socket.js
  92. +2 −1 test/parallel/test-http-client-pipe-end.js
  93. +2 −1 test/parallel/test-http-client-response-domain.js
  94. +5 −4 test/parallel/test-http-get-pipeline-problem.js
  95. +3 −2 test/parallel/test-http-pipe-fs.js
  96. +2 −1 test/parallel/test-http-unix-socket-keep-alive.js
  97. +2 −1 test/parallel/test-http-unix-socket.js
  98. +3 −2 test/parallel/test-http2-compat-serverrequest-pipe.js
  99. +3 −2 test/parallel/test-http2-pipe.js
  100. +2 −1 test/parallel/test-https-unix-socket-self-signed.js
  101. +3 −2 test/parallel/test-internal-fs-syncwritestream.js
  102. +3 −2 test/parallel/test-module-circular-symlinks.js
  103. +4 −3 test/parallel/test-module-loading-globalpaths.js
  104. +3 −2 test/parallel/test-module-symlinked-peer-modules.js
  105. +2 −1 test/parallel/test-net-connect-options-fd.js
  106. +2 −1 test/parallel/test-net-connect-options-path.js
  107. +2 −1 test/parallel/test-net-pingpong.js
  108. +3 −2 test/parallel/test-net-pipe-connect-errors.js
  109. +2 −1 test/parallel/test-net-server-listen-handle.js
  110. +2 −1 test/parallel/test-net-server-listen-path.js
  111. +4 −3 test/parallel/test-npm-install.js
  112. +2 −1 test/parallel/test-pipe-address.js
  113. +3 −2 test/parallel/test-pipe-file-to-http.js
  114. +2 −1 test/parallel/test-pipe-stream.js
  115. +2 −1 test/parallel/test-pipe-unref.js
  116. +2 −1 test/parallel/test-pipe-writev.js
  117. +6 −4 test/parallel/test-process-chdir.js
  118. +3 −2 test/parallel/test-process-execpath.js
  119. +3 −2 test/parallel/test-process-redirect-warnings-env.js
  120. +3 −2 test/parallel/test-process-redirect-warnings.js
  121. +4 −2 test/parallel/test-regress-GH-3739.js
  122. +3 −2 test/parallel/test-repl-history-perm.js
  123. +7 −6 test/parallel/test-repl-persistent-history.js
  124. +6 −5 test/parallel/test-repl-save-load.js
  125. +2 −1 test/parallel/test-repl.js
  126. +6 −4 test/parallel/test-require-long-path.js
  127. +4 −3 test/parallel/test-require-symlink.js
  128. +4 −3 test/parallel/test-require-unicode.js
  129. +3 −2 test/parallel/test-stdin-from-file.js
  130. +3 −2 test/parallel/test-stdout-to-file.js
  131. +2 −1 test/parallel/test-tls-connect-pipe.js
  132. +2 −1 test/parallel/test-tls-net-connect-prefer-path.js
  133. +2 −1 test/parallel/test-tls-wrap-econnreset-pipe.js
  134. +3 −2 test/parallel/test-trace-events-all.js
  135. +3 −2 test/parallel/test-trace-events-async-hooks.js
  136. +3 −2 test/parallel/test-trace-events-binding.js
  137. +3 −2 test/parallel/test-trace-events-category-used.js
  138. +3 −2 test/parallel/test-trace-events-none.js
  139. +4 −2 test/parallel/test-trace-events-process-exit.js
  140. +3 −2 test/parallel/test-trace-events-v8.js
  141. +3 −2 test/parallel/test-zlib-from-gzip.js
  142. +4 −3 test/pummel/test-fs-largefile.js
  143. +3 −1 test/pummel/test-fs-watch-file-slow.js
  144. +3 −1 test/pummel/test-fs-watch-file.js
  145. +3 −1 test/pummel/test-fs-watch-non-recursive.js
  146. +4 −2 test/pummel/test-regress-GH-814.js
  147. +3 −2 test/pummel/test-regress-GH-814_2.js
  148. +3 −1 test/pummel/test-tls-session-timeout.js
  149. +2 −1 test/sequential/test-async-wrap-getasyncid.js
  150. +3 −2 test/sequential/test-fs-readfile-tostring-fail.js
  151. +4 −2 test/sequential/test-fs-watch.js
  152. +3 −2 test/sequential/test-http2-timeout-large-write-file.js
  153. +2 −1 test/sequential/test-module-loading.js
  154. +3 −2 test/sequential/test-regress-GH-4027.js
  155. +4 −3 test/tick-processor/tick-processor-base.js
@@ -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 = {
@@ -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],
@@ -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++) {
@@ -35,7 +35,7 @@ function main({ thousands, fullPath, useCache }) {
else
measureDir(n, useCache === 'true');

refreshTmpDir();
tmpdir.refresh();
}

function measureFull(n, useCache) {
@@ -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));
@@ -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);
@@ -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();
@@ -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();
@@ -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
@@ -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
@@ -384,11 +380,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
@@ -670,6 +661,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
@@ -30,16 +30,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) {
@@ -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;
@@ -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);
@@ -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);
};
@@ -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');
@@ -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');
@@ -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,
@@ -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'));
@@ -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',
@@ -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 });

0 comments on commit eb25252

Please sign in to comment.