Commit 04824484d6f for nodejs
commit 04824484d6f130192cdda82896e7a23f1b6819be
Author: Livia Medeiros <livia@cirno.name>
Date: Fri Nov 20 13:26:57 2026 +0800
test: restrict `os.tmpdir()` in favour of `common/tmpdir`
Signed-off-by: LiviaMedeiros <livia@cirno.name>
PR-URL: https://github.com/nodejs/node/pull/67567
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
diff --git a/doc/contributing/writing-tests.md b/doc/contributing/writing-tests.md
index f9ca3c77201..3a0ee0969ea 100644
--- a/doc/contributing/writing-tests.md
+++ b/doc/contributing/writing-tests.md
@@ -237,6 +237,33 @@ argument. It is not a good idea to simply pass `common.mustCall()` to those
because `common.mustCall()` will ignore the error. Use `common.mustSucceed()`
instead.
+#### Temporary files
+
+Whenever test has to write files or directories, use the common
+[tmpdir module](https://github.com/nodejs/node/tree/HEAD/test/common#tmpdir-module)
+which handles creating and cleaning test-specific temporary directory. Do not
+create files under OS's temporary directory (`os.tmpdir()`) or anywhere else
+outside of the test-specific `tmpdir.path`.
+
+```js
+const tmpdir = require('../common/tmpdir');
+tmpdir.refresh();
+
+const tempFile = tmpdir.resolve('fileForWriteTest');
+```
+
+If test spawns child processes that can not utilize this module but might
+create files in OS's tmpdir, change the `TMPDIR` environment variable for them:
+
+```js
+spawnSync('/usr/bin/sort', [tempFile], {
+ env: { ...process.env, TMPDIR: tmpdir.path },
+});
+
+// or simply
+process.env.TMPDIR = tmpdir.path;
+```
+
#### Countdown module
The common [Countdown module](https://github.com/nodejs/node/tree/HEAD/test/common#countdown-module)
diff --git a/test/eslint.config_partial.mjs b/test/eslint.config_partial.mjs
index 0ef7d93df29..0710c4cddf0 100644
--- a/test/eslint.config_partial.mjs
+++ b/test/eslint.config_partial.mjs
@@ -121,6 +121,10 @@ export default [
selector: 'VariableDeclarator[init.type="Identifier"][init.name=/^(assert|fixtures)$/]',
message: 'Do not destructure or rename `assert` nor `fixtures`',
},
+ {
+ selector: 'CallExpression[callee.object.name="os"][callee.property.name="tmpdir"], CallExpression[callee.name="tmpdir"]',
+ message: 'Do not use `os.tmpdir`, prefer the `common/tmpdir` module.',
+ },
...((fixturesSpecifier) => [
{
selector: `ImportDeclaration[source.value=${fixturesSpecifier.toString().replace('(\\.js)?', '\\.mjs')}]:not(${[
diff --git a/test/ffi/test-ffi-vfs.js b/test/ffi/test-ffi-vfs.js
index 832deda68ac..aea31b61960 100644
--- a/test/ffi/test-ffi-vfs.js
+++ b/test/ffi/test-ffi-vfs.js
@@ -5,7 +5,6 @@ common.skipIfFFIMissing();
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const fs = require('node:fs');
-const os = require('node:os');
const path = require('node:path');
const { test } = require('node:test');
const tmpdir = require('../common/tmpdir');
@@ -99,7 +98,7 @@ test('closed library images give their fd numbers back', { skip: !common.isLinux
process.execPath, '--disable-warning=ExperimentalWarning', '-e', child,
], {
encoding: 'utf8',
- env: { ...process.env, TMPDIR: path.join(os.tmpdir(), 'no-such-directory') },
+ env: { ...process.env, TMPDIR: tmpdir.resolve('no-such-directory') },
});
assert.strictEqual(status, 0, stderr);
});
diff --git a/test/parallel/test-dlopen-binary.js b/test/parallel/test-dlopen-binary.js
index 8a575a52c97..24c4b07cbfb 100644
--- a/test/parallel/test-dlopen-binary.js
+++ b/test/parallel/test-dlopen-binary.js
@@ -1,6 +1,7 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
+const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const os = require('os');
@@ -22,7 +23,7 @@ const bytes = fs.readFileSync(addonPath);
const flags = (os.constants.dlopen?.RTLD_LAZY) || 0;
// A path that does not exist on disk, as a VFS-resident addon would be.
-const fakePath = path.join(os.tmpdir(), 'nonexistent-vfs-dir', 'binding.node');
+const fakePath = tmpdir.resolve('nonexistent-vfs-dir', 'binding.node');
assert.ok(!fs.existsSync(fakePath));
const m = { exports: {} };
diff --git a/test/parallel/test-dtls-passphrase.mjs b/test/parallel/test-dtls-passphrase.mjs
index d9926401b45..d1687a162dc 100644
--- a/test/parallel/test-dtls-passphrase.mjs
+++ b/test/parallel/test-dtls-passphrase.mjs
@@ -8,13 +8,15 @@
// only, ignored when the key is not encrypted.
import { hasCrypto, mustNotCall, skip } from '../common/index.mjs';
+import tmpdir from '../common/tmpdir.js';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
-import { tmpdir } from 'node:os';
import { join } from 'node:path';
+tmpdir.refresh();
+
if (!hasCrypto) {
skip('missing crypto');
}
@@ -29,7 +31,7 @@ const PASSPHRASE = 'correct horse battery staple';
// Generate an encrypted key and a matching self-signed certificate rather
// than checking a fixture in, so the passphrase is visible in the test.
-const dir = mkdtempSync(join(tmpdir(), 'dtls-passphrase-'));
+const dir = mkdtempSync(tmpdir.resolve('dtls-passphrase-'));
let encryptedKey;
let encryptedCert;
try {
diff --git a/test/parallel/test-fs-negative-zero.js b/test/parallel/test-fs-negative-zero.js
index 09f965eb171..66862762bfb 100644
--- a/test/parallel/test-fs-negative-zero.js
+++ b/test/parallel/test-fs-negative-zero.js
@@ -1,15 +1,14 @@
'use strict';
require('../common');
-
+const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const { spawnSync } = require('child_process');
const fs = require('fs');
-const path = require('path');
-const os = require('os');
-const missing = path.join(
- os.tmpdir(),
+tmpdir.refresh();
+
+const missing = tmpdir.resolve(
`node-fs-negative-zero-${process.pid}`,
'entry',
);
diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js
index f15499d692e..695917230e9 100644
--- a/test/parallel/test-os.js
+++ b/test/parallel/test-os.js
@@ -42,6 +42,8 @@ const is = {
process.env.TMPDIR = '/tmpdir';
process.env.TMP = '/tmp';
process.env.TEMP = '/temp';
+
+/* eslint-disable no-restricted-syntax */
if (common.isWindows) {
assert.strictEqual(os.tmpdir(), '/temp');
process.env.TEMP = '';
@@ -72,6 +74,7 @@ if (common.isWindows) {
process.env.TMPDIR = '/';
assert.strictEqual(os.tmpdir(), '/');
}
+/* eslint-enable no-restricted-syntax */
const endianness = os.endianness();
is.string(endianness);
@@ -250,7 +253,7 @@ assert.strictEqual(`${os.homedir}`, os.homedir());
assert.strictEqual(`${os.release}`, os.release());
assert.strictEqual(`${os.type}`, os.type());
assert.strictEqual(`${os.endianness}`, os.endianness());
-assert.strictEqual(`${os.tmpdir}`, os.tmpdir());
+assert.strictEqual(`${os.tmpdir}`, os.tmpdir()); // eslint-disable-line no-restricted-syntax
assert.strictEqual(`${os.arch}`, os.arch());
assert.strictEqual(`${os.platform}`, os.platform());
assert.strictEqual(`${os.version}`, os.version());