From 65b4a7581a635b0bd6204a10bd1e8a7846a1cbae Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 24 Jun 2023 18:37:09 +0200 Subject: [PATCH] fix more tests with wrong assumptions --- test/abort/test-abort-fatal-error.js | 24 +++++++------------ test/async-hooks/test-callback-error.js | 3 ++- test/parallel/test-fs-readfile-error.js | 4 ++-- test/parallel/test-fs-readfile-pipe-large.js | 10 ++++---- test/parallel/test-fs-readfile-pipe.js | 8 +++---- .../test-fs-readfilesync-pipe-large.js | 6 ++--- test/parallel/test-fs-whatwg-url.js | 12 +--------- test/parallel/test-fs-write-sigxfsz.js | 4 ++-- ...test-internal-util-decorate-error-stack.js | 2 +- test/parallel/test-module-create-require.js | 5 ++-- test/parallel/test-npm-install.js | 4 +++- ...test-permission-allow-child-process-cli.js | 2 +- test/parallel/test-pipe-head.js | 4 ++-- .../parallel/test-preload-self-referential.js | 7 +++--- test/parallel/test-preload-worker.js | 2 +- test/parallel/test-repl-require-context.js | 2 +- test/parallel/test-startup-large-pages.js | 4 ++-- test/parallel/test-stdin-from-file-spawn.js | 3 ++- test/parallel/test-stdin-from-file.js | 6 +++-- test/parallel/test-stdio-closed.js | 4 ++-- test/parallel/test-stdio-pipe-stderr.js | 2 +- test/parallel/test-stdout-close-catch.js | 8 +++---- test/parallel/test-stdout-to-file.js | 4 ++-- .../test-timers-immediate-promisified.js | 6 ++--- .../test-timers-interval-promisified.js | 6 ++--- .../test-timers-timeout-promisified.js | 6 ++--- test/parallel/test-tls-ecdh.js | 4 ++-- ...-trace-events-worker-metadata-with-name.js | 2 +- test/parallel/test-url-parse-invalid-input.js | 14 +++++------ test/parallel/test-worker-init-failure.js | 4 ++-- test/pseudo-tty/test-repl-external-module.js | 4 ++-- test/sequential/test-child-process-emfile.js | 3 ++- .../sequential/test-child-process-execsync.js | 10 ++++---- test/sequential/test-cli-syntax-bad.js | 4 ++-- .../test-cli-syntax-file-not-found.js | 4 ++-- test/sequential/test-cli-syntax-good.js | 4 ++-- test/sequential/test-fs-stat-sync-overflow.js | 18 +++++++------- test/sequential/test-init.js | 3 ++- 38 files changed, 102 insertions(+), 120 deletions(-) diff --git a/test/abort/test-abort-fatal-error.js b/test/abort/test-abort-fatal-error.js index 5bfa6eaf3956d3..38f336620fa9af 100644 --- a/test/abort/test-abort-fatal-error.js +++ b/test/abort/test-abort-fatal-error.js @@ -27,21 +27,13 @@ if (common.isWindows) const assert = require('assert'); const exec = require('child_process').exec; -let cmdline = `ulimit -c 0; ${JSON.stringify(process.execPath)}`; -cmdline += ' --max-old-space-size=16 --max-semi-space-size=4'; -cmdline += ' -e "a = []; for (i = 0; i < 1e9; i++) { a.push({}) }"'; +const cmdline = + 'ulimit -c 0; "$NODE" --max-old-space-size=16 --max-semi-space-size=4' + + ' -e "a = []; for (i = 0; i < 1e9; i++) { a.push({}) }"'; -exec(cmdline, function(err, stdout, stderr) { - if (!err) { - console.log(stdout); - console.log(stderr); - assert(false, 'this test should fail'); +exec(cmdline, { env: { NODE: process.execPath } }, common.mustCall((err, stdout, stderr) => { + if (err?.code !== 134 && err?.signal !== 'SIGABRT') { + console.log({ err, stdout, stderr }); + assert.fail(err?.message); } - - if (err.code !== 134 && err.signal !== 'SIGABRT') { - console.log(stdout); - console.log(stderr); - console.log(err); - assert(false, err); - } -}); +})); diff --git a/test/async-hooks/test-callback-error.js b/test/async-hooks/test-callback-error.js index f6af8e0018d534..942393e45c0062 100644 --- a/test/async-hooks/test-callback-error.js +++ b/test/async-hooks/test-callback-error.js @@ -64,9 +64,10 @@ assert.ok(!arg); '--abort-on-uncaught-exception', __filename, 'test_callback_abort' ]; const options = { encoding: 'utf8' }; if (!common.isWindows) { - program = `ulimit -c 0 && exec ${program} ${args.join(' ')}`; + program = `ulimit -c 0 && exec "$NODE" ${args[0]} "$FILE" ${args[2]}`; args = []; options.shell = true; + options.env = { NODE: process.execPath, FILE: __filename }; } const child = spawnSync(program, args, options); if (common.isWindows) { diff --git a/test/parallel/test-fs-readfile-error.js b/test/parallel/test-fs-readfile-error.js index 8c5a3a71c6f530..318c096fa60108 100644 --- a/test/parallel/test-fs-readfile-error.js +++ b/test/parallel/test-fs-readfile-error.js @@ -36,8 +36,8 @@ const fixtures = require('../common/fixtures'); function test(env, cb) { const filename = fixtures.path('test-fs-readfile-error.js'); - const execPath = `"${process.execPath}" "${filename}"`; - const options = { env: { ...process.env, ...env } }; + const execPath = '"$NODE" "$FILE"'; + const options = { env: { ...process.env, ...env, NODE: process.execPath, FILE: filename } }; exec(execPath, options, (err, stdout, stderr) => { assert(err); assert.strictEqual(stdout, ''); diff --git a/test/parallel/test-fs-readfile-pipe-large.js b/test/parallel/test-fs-readfile-pipe-large.js index 14a0793620b7b9..69be9deb9c7f53 100644 --- a/test/parallel/test-fs-readfile-pipe-large.js +++ b/test/parallel/test-fs-readfile-pipe-large.js @@ -26,10 +26,12 @@ tmpdir.refresh(); fs.writeFileSync(filename, dataExpected); const exec = require('child_process').exec; -const f = JSON.stringify(__filename); -const node = JSON.stringify(process.execPath); -const cmd = `cat ${filename} | ${node} ${f} child`; -exec(cmd, { maxBuffer: 1000000 }, common.mustSucceed((stdout, stderr) => { +const cmd = '"$NODE" "$FILE" child < "TMP_FILE"'; +exec(cmd, { maxBuffer: 1000000, env: { + NODE: process.execPath, + FILE: __filename, + TMP_FILE: filename, +} }, common.mustSucceed((stdout, stderr) => { assert.strictEqual( stdout, dataExpected, diff --git a/test/parallel/test-fs-readfile-pipe.js b/test/parallel/test-fs-readfile-pipe.js index 0cffbd0f5aa162..93744c274e654c 100644 --- a/test/parallel/test-fs-readfile-pipe.js +++ b/test/parallel/test-fs-readfile-pipe.js @@ -43,10 +43,10 @@ const filename = fixtures.path('readfile_pipe_test.txt'); const dataExpected = fs.readFileSync(filename).toString(); const exec = require('child_process').exec; -const f = JSON.stringify(__filename); -const node = JSON.stringify(process.execPath); -const cmd = `cat ${filename} | ${node} ${f} child`; -exec(cmd, common.mustSucceed((stdout, stderr) => { +const cmd = '"$NODE" "$FILE" child < "TMP_FILE"'; +exec(cmd, { + env: { NODE: process.execPath, FILE: __filename, TMP_FILE: filename } +}, common.mustSucceed((stdout, stderr) => { assert.strictEqual( stdout, dataExpected, diff --git a/test/parallel/test-fs-readfilesync-pipe-large.js b/test/parallel/test-fs-readfilesync-pipe-large.js index 7c2a5e7fd01ae3..9ff723a01378b9 100644 --- a/test/parallel/test-fs-readfilesync-pipe-large.js +++ b/test/parallel/test-fs-readfilesync-pipe-large.js @@ -23,12 +23,10 @@ tmpdir.refresh(); fs.writeFileSync(filename, dataExpected); const exec = require('child_process').exec; -const f = JSON.stringify(__filename); -const node = JSON.stringify(process.execPath); -const cmd = `cat ${filename} | ${node} ${f} child`; +const cmd = '"$NODE" "$FILE" child < "TMP_FILE"'; exec( cmd, - { maxBuffer: 1000000 }, + { maxBuffer: 1000000, env: { NODE: process.execPath, FILE: __filename, TMP_FILE: filename } }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, dataExpected); assert.strictEqual(stderr, ''); diff --git a/test/parallel/test-fs-whatwg-url.js b/test/parallel/test-fs-whatwg-url.js index 829cfa92fafebd..5e9ea98a5820ed 100644 --- a/test/parallel/test-fs-whatwg-url.js +++ b/test/parallel/test-fs-whatwg-url.js @@ -3,20 +3,10 @@ const common = require('../common'); const fixtures = require('../common/fixtures'); const assert = require('assert'); -const path = require('path'); const fs = require('fs'); const os = require('os'); -function pathToFileURL(p) { - if (!path.isAbsolute(p)) - throw new Error('Path must be absolute'); - if (common.isWindows && p.startsWith('\\\\')) - p = p.slice(2); - return new URL(`file://${p}`); -} - -const p = path.resolve(fixtures.fixturesDir, 'a.js'); -const url = pathToFileURL(p); +const url = fixtures.fileURL('a.js'); assert(url instanceof URL); diff --git a/test/parallel/test-fs-write-sigxfsz.js b/test/parallel/test-fs-write-sigxfsz.js index 323312fcb943dc..58bd38223eaaf8 100644 --- a/test/parallel/test-fs-write-sigxfsz.js +++ b/test/parallel/test-fs-write-sigxfsz.js @@ -20,8 +20,8 @@ if (process.argv[2] === 'child') { tmpdir.refresh(); fs.writeFileSync(filename, '.'.repeat(1 << 16)); // Exceeds RLIMIT_FSIZE. } else { - const cmd = `ulimit -f 1 && '${process.execPath}' '${__filename}' child`; - const result = child_process.spawnSync('/bin/sh', ['-c', cmd]); + const cmd = 'ulimit -f 1 && "$NODE" "$FILE" child'; + const result = child_process.spawnSync('/bin/sh', ['-c', cmd], { env: { NODE: process.execPath, FILE: __filename } }); const haystack = result.stderr.toString(); const needle = 'Error: EFBIG: file too large, write'; const ok = haystack.includes(needle); diff --git a/test/parallel/test-internal-util-decorate-error-stack.js b/test/parallel/test-internal-util-decorate-error-stack.js index 3566d9375fb81c..f3034fbb05ae54 100644 --- a/test/parallel/test-internal-util-decorate-error-stack.js +++ b/test/parallel/test-internal-util-decorate-error-stack.js @@ -58,7 +58,7 @@ checkStack(err.stack); // Verify that the stack is only decorated once for uncaught exceptions. const args = [ '-e', - `require('${badSyntaxPath}')`, + `require(${JSON.stringify(badSyntaxPath)})`, ]; const result = spawnSync(process.argv[0], args, { encoding: 'utf8' }); checkStack(result.stderr); diff --git a/test/parallel/test-module-create-require.js b/test/parallel/test-module-create-require.js index e0e34e9f127bd3..30ebf96652390d 100644 --- a/test/parallel/test-module-create-require.js +++ b/test/parallel/test-module-create-require.js @@ -1,13 +1,12 @@ 'use strict'; require('../common'); +const fixtures = require('../common/fixtures'); const assert = require('assert'); -const path = require('path'); const { createRequire } = require('module'); -const p = path.resolve(__dirname, '..', 'fixtures', 'fake.js'); -const u = new URL(`file://${p}`); +const u = fixtures.fileURL('fake.js'); const reqToo = createRequire(u); assert.deepStrictEqual(reqToo('./experimental'), { ofLife: 42 }); diff --git a/test/parallel/test-npm-install.js b/test/parallel/test-npm-install.js index d3ba8ab8b55561..6799455470cd9e 100644 --- a/test/parallel/test-npm-install.js +++ b/test/parallel/test-npm-install.js @@ -39,6 +39,8 @@ const pkgPath = path.join(installDir, 'package.json'); fs.writeFileSync(pkgPath, pkgContent); const env = { ...process.env, + NODE: process.execPath, + NPM: npmPath, PATH: path.dirname(process.execPath), NPM_CONFIG_PREFIX: path.join(npmSandbox, 'npm-prefix'), NPM_CONFIG_TMP: path.join(npmSandbox, 'npm-tmp'), @@ -46,7 +48,7 @@ const env = { ...process.env, NPM_CONFIG_UPDATE_NOTIFIER: false, HOME: homeDir }; -exec(`${process.execPath} ${npmPath} install`, { +exec('"$NODE" "$NPM" install', { cwd: installDir, env: env }, common.mustCall(handleExit)); diff --git a/test/parallel/test-permission-allow-child-process-cli.js b/test/parallel/test-permission-allow-child-process-cli.js index 6cffc19719350b..49309051ac5133 100644 --- a/test/parallel/test-permission-allow-child-process-cli.js +++ b/test/parallel/test-permission-allow-child-process-cli.js @@ -20,7 +20,7 @@ if (process.argv[2] === 'child') { { // doesNotThrow childProcess.spawnSync(process.execPath, ['--version']); - childProcess.execSync(process.execPath, ['--version']); + childProcess.execSync('"$NODE" --version', { env: { NODE: process.execPath } }); childProcess.fork(__filename, ['child']); childProcess.execFileSync(process.execPath, ['--version']); } diff --git a/test/parallel/test-pipe-head.js b/test/parallel/test-pipe-head.js index 1e79249c290500..6166fb8dec7e6d 100644 --- a/test/parallel/test-pipe-head.js +++ b/test/parallel/test-pipe-head.js @@ -8,9 +8,9 @@ const exec = require('child_process').exec; const nodePath = process.argv[0]; const script = fixtures.path('print-10-lines.js'); -const cmd = `"${nodePath}" "${script}" | head -2`; +const cmd = '"$NODE" "$FILE" | head -2'; -exec(cmd, common.mustSucceed((stdout, stderr) => { +exec(cmd, { env: { NODE: nodePath, FILE: script } }, common.mustSucceed((stdout, stderr) => { const lines = stdout.split('\n'); assert.strictEqual(lines.length, 3); })); diff --git a/test/parallel/test-preload-self-referential.js b/test/parallel/test-preload-self-referential.js index 2624527deb3984..51d7c0100428db 100644 --- a/test/parallel/test-preload-self-referential.js +++ b/test/parallel/test-preload-self-referential.js @@ -13,8 +13,7 @@ if (!common.isMainThread) const selfRefModule = fixtures.path('self_ref_module'); const fixtureA = fixtures.path('printA.js'); -exec(`"${nodeBinary}" -r self_ref "${fixtureA}"`, { cwd: selfRefModule }, - (err, stdout, stderr) => { - assert.ifError(err); +exec('"$NODE" -r self_ref "$FIXTURE_A"', { cwd: selfRefModule, env: { NODE: nodeBinary, FIXTURE_A: fixtureA } }, + common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, 'A\n'); - }); + })); diff --git a/test/parallel/test-preload-worker.js b/test/parallel/test-preload-worker.js index 3e9134b470cf37..83579bce28dcfe 100644 --- a/test/parallel/test-preload-worker.js +++ b/test/parallel/test-preload-worker.js @@ -7,4 +7,4 @@ const { exec } = require('child_process'); const kNodeBinary = process.argv[0]; -exec(`"${kNodeBinary}" -r "${worker}" -pe "1+1"`, common.mustSucceed()); +exec('"$NODE" -r "$WORKER" -pe "1+1"', { env: { NODE: kNodeBinary, WORKER: worker } }, common.mustSucceed()); diff --git a/test/parallel/test-repl-require-context.js b/test/parallel/test-repl-require-context.js index 750235818b8bfc..af09249c2de919 100644 --- a/test/parallel/test-repl-require-context.js +++ b/test/parallel/test-repl-require-context.js @@ -19,6 +19,6 @@ child.on('exit', common.mustCall(() => { child.stdin.write('const isObject = (obj) => obj.constructor === Object;\n'); child.stdin.write('isObject({});\n'); -child.stdin.write(`require('${fixture}').isObject({});\n`); +child.stdin.write(`require(${JSON.stringify(fixture)}).isObject({});\n`); child.stdin.write('.exit'); child.stdin.end(); diff --git a/test/parallel/test-startup-large-pages.js b/test/parallel/test-startup-large-pages.js index ed0542b6c6dc68..9ebe9fa7b66804 100644 --- a/test/parallel/test-startup-large-pages.js +++ b/test/parallel/test-startup-large-pages.js @@ -22,8 +22,8 @@ const { spawnSync } = require('child_process'); [ '--use-largepages=xyzzy', '-p', '42' ]); assert.strictEqual(child.status, 9); assert.strictEqual(child.signal, null); - assert.strictEqual(child.stderr.toString().match(/\S+/g).slice(1).join(' '), - 'invalid value for --use-largepages'); + assert.match(child.stderr.toString().trim(), + /invalid value for --use-largepages$/); } // TODO(gabrielschulhof): Make assertions about the stderr, which may or may not diff --git a/test/parallel/test-stdin-from-file-spawn.js b/test/parallel/test-stdin-from-file-spawn.js index 12c235fcfeb081..a7ec6ea04906f4 100644 --- a/test/parallel/test-stdin-from-file-spawn.js +++ b/test/parallel/test-stdin-from-file-spawn.js @@ -39,4 +39,5 @@ setTimeout(() => { }, 100); `); -execSync(`${process.argv[0]} ${tmpJsFile} < ${tmpCmdFile}`); +execSync('"$NODE" "$JS_FILE" < "$CMD_FILE"', + { env: { NODE: process.argv0, JS_FILE: tmpJsFile, CMD_FILE: tmpCmdFile } }); diff --git a/test/parallel/test-stdin-from-file.js b/test/parallel/test-stdin-from-file.js index f4fcb32edbfde5..6dfb5fd68d32c4 100644 --- a/test/parallel/test-stdin-from-file.js +++ b/test/parallel/test-stdin-from-file.js @@ -10,7 +10,7 @@ const fs = require('fs'); const stdoutScript = fixtures.path('echo-close-check.js'); const tmpFile = join(tmpdir.path, 'stdin.txt'); -const cmd = `"${process.argv[0]}" "${stdoutScript}" < "${tmpFile}"`; +const cmd = '"$NODE" "$STDOUT_SCRIPT" < "$TMP_FILE"'; const string = 'abc\nümlaut.\nsomething else\n' + '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,' + @@ -31,7 +31,9 @@ console.log(`${cmd}\n\n`); fs.writeFileSync(tmpFile, string); -childProcess.exec(cmd, common.mustCall(function(err, stdout, stderr) { +childProcess.exec(cmd, { env: { + NODE: process.argv0, STDOUT_SCRIPT: stdoutScript, TMP_FILE: tmpFile +} }, common.mustCall(function(err, stdout, stderr) { fs.unlinkSync(tmpFile); assert.ifError(err); diff --git a/test/parallel/test-stdio-closed.js b/test/parallel/test-stdio-closed.js index cc9f1e86ccbf6c..a34e7add7bc019 100644 --- a/test/parallel/test-stdio-closed.js +++ b/test/parallel/test-stdio-closed.js @@ -29,8 +29,8 @@ if (process.argv[2] === 'child') { } // Run the script in a shell but close stdout and stderr. -const cmd = `"${process.execPath}" "${__filename}" child 1>&- 2>&-`; -const proc = spawn('/bin/sh', ['-c', cmd], { stdio: 'inherit' }); +const cmd = '"$NODE" "$FILE" child 1>&- 2>&-'; +const proc = spawn('/bin/sh', ['-c', cmd], { stdio: 'inherit', env: { NODE: process.execPath, FILE: __filename } }); proc.on('exit', common.mustCall(function(exitCode) { assert.strictEqual(exitCode, 0); diff --git a/test/parallel/test-stdio-pipe-stderr.js b/test/parallel/test-stdio-pipe-stderr.js index 9ec41b4159fdf6..1737424bb049fc 100644 --- a/test/parallel/test-stdio-pipe-stderr.js +++ b/test/parallel/test-stdio-pipe-stderr.js @@ -22,7 +22,7 @@ fs.writeFileSync(fakeModulePath, '', 'utf8'); stream.on('open', () => { spawnSync(process.execPath, { - input: `require("${fakeModulePath.replace(/\\/g, '/')}")`, + input: `require(${JSON.stringify(fakeModulePath)})`, stdio: ['pipe', 'pipe', stream] }); const stderr = fs.readFileSync(stderrOutputPath, 'utf8').trim(); diff --git a/test/parallel/test-stdout-close-catch.js b/test/parallel/test-stdout-close-catch.js index 924b52715a54f3..5af400c7d5e9fe 100644 --- a/test/parallel/test-stdout-close-catch.js +++ b/test/parallel/test-stdout-close-catch.js @@ -7,12 +7,12 @@ const { getSystemErrorName } = require('util'); const testScript = fixtures.path('catch-stdout-error.js'); -const cmd = `${JSON.stringify(process.execPath)} ` + - `${JSON.stringify(testScript)} | ` + - `${JSON.stringify(process.execPath)} ` + +const cmd = '"$NODE" ' + + '"$TEST_SCRIPT" | ' + + '"$NODE" ' + '-pe "process.stdin.on(\'data\' , () => process.exit(1))"'; -const child = child_process.exec(cmd); +const child = child_process.exec(cmd, { env: { NODE: process.execPath, TEST_SCRIPT: testScript } }); let output = ''; child.stderr.on('data', function(c) { diff --git a/test/parallel/test-stdout-to-file.js b/test/parallel/test-stdout-to-file.js index d66f382af5c1f8..7bbb821d3c9b35 100644 --- a/test/parallel/test-stdout-to-file.js +++ b/test/parallel/test-stdout-to-file.js @@ -14,7 +14,7 @@ const tmpFile = path.join(tmpdir.path, 'stdout.txt'); tmpdir.refresh(); function test(size, useBuffer, cb) { - const cmd = `"${process.argv[0]}" "${ + const cmd = `"$NODE" "${ useBuffer ? scriptBuffer : scriptString}" ${size} > "${tmpFile}"`; try { @@ -25,7 +25,7 @@ function test(size, useBuffer, cb) { console.log(`${size} chars to ${tmpFile}...`); - childProcess.exec(cmd, common.mustSucceed(() => { + childProcess.exec(cmd, { env: { NODE: process.execPath } }, common.mustSucceed(() => { console.log('done!'); const stat = fs.statSync(tmpFile); diff --git a/test/parallel/test-timers-immediate-promisified.js b/test/parallel/test-timers-immediate-promisified.js index b504ce1778e8d9..586fd501273ae3 100644 --- a/test/parallel/test-timers-immediate-promisified.js +++ b/test/parallel/test-timers-immediate-promisified.js @@ -4,7 +4,6 @@ const common = require('../common'); const assert = require('assert'); const timers = require('timers'); const { promisify } = require('util'); -const child_process = require('child_process'); const { getEventListeners } = require('events'); const { NodeEventTarget } = require('internal/event_target'); @@ -12,7 +11,6 @@ const { NodeEventTarget } = require('internal/event_target'); const timerPromises = require('timers/promises'); const setPromiseImmediate = promisify(timers.setImmediate); -const exec = promisify(child_process.exec); assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate); @@ -91,9 +89,9 @@ process.on('multipleResolves', common.mustNotCall()); } { - exec(`${process.execPath} -pe "const assert = require('assert');` + + common.spawnPromisified(process.execPath, ['-pe', "const assert = require('assert');" + 'require(\'timers/promises\').setImmediate(null, { ref: false }).' + - 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { + 'then(assert.fail)']).then(common.mustCall(({ stderr }) => { assert.strictEqual(stderr, ''); })); } diff --git a/test/parallel/test-timers-interval-promisified.js b/test/parallel/test-timers-interval-promisified.js index 46b977ff80a8ef..e9434625e38026 100644 --- a/test/parallel/test-timers-interval-promisified.js +++ b/test/parallel/test-timers-interval-promisified.js @@ -4,7 +4,6 @@ const common = require('../common'); const assert = require('assert'); const timers = require('timers'); const { promisify } = require('util'); -const child_process = require('child_process'); const { getEventListeners } = require('events'); const { NodeEventTarget } = require('internal/event_target'); @@ -12,7 +11,6 @@ const { NodeEventTarget } = require('internal/event_target'); const timerPromises = require('timers/promises'); const setPromiseTimeout = promisify(timers.setTimeout); -const exec = promisify(child_process.exec); const { setInterval } = timerPromises; @@ -154,11 +152,11 @@ process.on('multipleResolves', common.mustNotCall()); } { - exec(`${process.execPath} -pe "const assert = require('assert');` + + common.spawnPromisified(process.execPath, ['-pe', "const assert = require('assert');" + 'const interval = require(\'timers/promises\')' + '.setInterval(1000, null, { ref: false });' + 'interval[Symbol.asyncIterator]().next()' + - '.then(assert.fail)"').then(common.mustCall(({ stderr }) => { + '.then(assert.fail)']).then(common.mustCall(({ stderr }) => { assert.strictEqual(stderr, ''); })); } diff --git a/test/parallel/test-timers-timeout-promisified.js b/test/parallel/test-timers-timeout-promisified.js index 95298c6a42977d..a63923b7feea86 100644 --- a/test/parallel/test-timers-timeout-promisified.js +++ b/test/parallel/test-timers-timeout-promisified.js @@ -4,7 +4,6 @@ const common = require('../common'); const assert = require('assert'); const timers = require('timers'); const { promisify } = require('util'); -const child_process = require('child_process'); const { getEventListeners } = require('events'); const { NodeEventTarget } = require('internal/event_target'); @@ -12,7 +11,6 @@ const { NodeEventTarget } = require('internal/event_target'); const timerPromises = require('timers/promises'); const setPromiseTimeout = promisify(timers.setTimeout); -const exec = promisify(child_process.exec); assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout); @@ -91,9 +89,9 @@ process.on('multipleResolves', common.mustNotCall()); } { - exec(`${process.execPath} -pe "const assert = require('assert');` + + common.spawnPromisified(process.execPath, ['-pe', 'const assert = require(\'assert\');' + 'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' + - 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { + 'then(assert.fail)']).then(common.mustCall(({ stderr }) => { assert.strictEqual(stderr, ''); })); } diff --git a/test/parallel/test-tls-ecdh.js b/test/parallel/test-tls-ecdh.js index 8c879f850c9b8d..117931e4e3512e 100644 --- a/test/parallel/test-tls-ecdh.js +++ b/test/parallel/test-tls-ecdh.js @@ -49,10 +49,10 @@ const server = tls.createServer(options, common.mustCall(function(conn) { })); server.listen(0, '127.0.0.1', common.mustCall(function() { - const cmd = `"${common.opensslCli}" s_client -cipher ${ + const cmd = `"$OPENSSL" s_client -cipher ${ options.ciphers} -connect 127.0.0.1:${this.address().port}`; - exec(cmd, common.mustSucceed((stdout, stderr) => { + exec(cmd, { env: { OPENSSL: common.opensslCli } }, common.mustSucceed((stdout, stderr) => { assert(stdout.includes(reply)); server.close(); })); diff --git a/test/parallel/test-trace-events-worker-metadata-with-name.js b/test/parallel/test-trace-events-worker-metadata-with-name.js index 6c3a44f9566d9c..bf6e1005aa458f 100644 --- a/test/parallel/test-trace-events-worker-metadata-with-name.js +++ b/test/parallel/test-trace-events-worker-metadata-with-name.js @@ -7,7 +7,7 @@ const { isMainThread } = require('worker_threads'); if (isMainThread) { const CODE = 'const { Worker } = require(\'worker_threads\'); ' + - `new Worker('${__filename.replace(/\\/g, '/')}', { name: 'foo' })`; + `new Worker(${JSON.stringify(__filename)}, { name: 'foo' })`; const FILE_NAME = 'node_trace.1.log'; const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); diff --git a/test/parallel/test-url-parse-invalid-input.js b/test/parallel/test-url-parse-invalid-input.js index 023f74ee86a70e..6f655843e51ffd 100644 --- a/test/parallel/test-url-parse-invalid-input.js +++ b/test/parallel/test-url-parse-invalid-input.js @@ -1,7 +1,6 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); -const childProcess = require('child_process'); const url = require('url'); // https://github.com/joyent/node/issues/568 @@ -82,13 +81,12 @@ if (common.hasIntl) { 'git+ssh://git@github.com:npm/npm', ]; badURLs.forEach((badURL) => { - childProcess.exec(`${process.execPath} -e "url.parse('${badURL}')"`, - common.mustCall((err, stdout, stderr) => { - assert.strictEqual(err, null); - assert.strictEqual(stdout, ''); - assert.match(stderr, /\[DEP0170\] DeprecationWarning:/); - }) - ); + common.spawnPromisified(process.execPath, ['-e', `url.parse(${JSON.stringify(badURL)})`]) + .then(common.mustCall(({ code, stdout, stderr }) => { + assert.strictEqual(code, 0); + assert.strictEqual(stdout, ''); + assert.match(stderr, /\[DEP0170\] DeprecationWarning:/); + })); }); // Warning should only happen once per process. diff --git a/test/parallel/test-worker-init-failure.js b/test/parallel/test-worker-init-failure.js index 078329ee68874f..9a61edf759511c 100644 --- a/test/parallel/test-worker-init-failure.js +++ b/test/parallel/test-worker-init-failure.js @@ -48,8 +48,8 @@ if (process.argv[2] === 'child') { } else { // Limit the number of open files, to force workers to fail. let testCmd = `ulimit -n ${OPENFILES} && `; - testCmd += `${process.execPath} ${__filename} child`; - const cp = child_process.exec(testCmd); + testCmd += '"$NODE" "$FILE" child'; + const cp = child_process.exec(testCmd, { env: { NODE: process.execPath, FILE: __filename } }); // Turn on the child streams for debugging purposes. let stdout = ''; diff --git a/test/pseudo-tty/test-repl-external-module.js b/test/pseudo-tty/test-repl-external-module.js index db9ad29712ed70..3f17cd4dee0441 100644 --- a/test/pseudo-tty/test-repl-external-module.js +++ b/test/pseudo-tty/test-repl-external-module.js @@ -2,9 +2,9 @@ require('../common'); const fixtures = require('../common/fixtures'); -const { execSync } = require('child_process'); +const { execFileSync } = require('child_process'); -execSync(process.execPath, { +execFileSync(process.execPath, { encoding: 'utf8', stdio: 'inherit', env: { diff --git a/test/sequential/test-child-process-emfile.js b/test/sequential/test-child-process-emfile.js index 8091e54a5c5bde..7149b909739156 100644 --- a/test/sequential/test-child-process-emfile.js +++ b/test/sequential/test-child-process-emfile.js @@ -35,7 +35,8 @@ if (ulimit > 64 || Number.isNaN(ulimit)) { // ever happens. const result = child_process.spawnSync( '/bin/sh', - ['-c', `ulimit -n 64 && '${process.execPath}' '${__filename}'`] + ['-c', 'ulimit -n 64 && "$NODE" "$FILENAME"'], + { env: { NODE: process.execPath, FILENAME: __filename } } ); assert.strictEqual(result.stdout.toString(), ''); assert.strictEqual(result.stderr.toString(), ''); diff --git a/test/sequential/test-child-process-execsync.js b/test/sequential/test-child-process-execsync.js index 75acbc34a902bd..d1c6a85973447c 100644 --- a/test/sequential/test-child-process-execsync.js +++ b/test/sequential/test-child-process-execsync.js @@ -54,8 +54,8 @@ let caught = false; let ret, err; const start = Date.now(); try { - const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`; - ret = execSync(cmd, { timeout: TIMER }); + const cmd = `"$NODE" -e "setTimeout(function(){}, ${SLEEP});"`; + ret = execSync(cmd, { env: { NODE: process.execPath }, timeout: TIMER }); } catch (e) { caught = true; assert.strictEqual(getSystemErrorName(e.errno), 'ETIMEDOUT'); @@ -78,16 +78,16 @@ const msgBuf = Buffer.from(`${msg}\n`); // console.log ends every line with just '\n', even on Windows. -const cmd = `"${process.execPath}" -e "console.log('${msg}');"`; +const cmd = `"$NODE" -e 'console.log(${JSON.stringify(msg)})'`; { - const ret = execSync(cmd); + const ret = execSync(cmd, { env: { NODE: process.execPath } }); assert.strictEqual(ret.length, msgBuf.length); assert.deepStrictEqual(ret, msgBuf); } { - const ret = execSync(cmd, { encoding: 'utf8' }); + const ret = execSync(cmd, { encoding: 'utf8', env: { NODE: process.execPath } }); assert.strictEqual(ret, `${msg}\n`); } diff --git a/test/sequential/test-cli-syntax-bad.js b/test/sequential/test-cli-syntax-bad.js index 5c87bf11419af6..9e575aa717337e 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -29,8 +29,8 @@ const syntaxErrorRE = /^SyntaxError: \b/m; // Loop each possible option, `-c` or `--check` syntaxArgs.forEach(function(args) { const _args = args.concat(file); - const cmd = [node, ..._args].join(' '); - exec(cmd, common.mustCall((err, stdout, stderr) => { + const cmd = ['"$NODE"', ..._args].join(' '); + exec(cmd, { env: { NODE: node } }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err instanceof Error, true); assert.strictEqual(err.code, 1, `code ${err.code} !== 1 for error:\n\n${err}`); diff --git a/test/sequential/test-cli-syntax-file-not-found.js b/test/sequential/test-cli-syntax-file-not-found.js index 61f84d8e71624d..720cf8fbf5afa3 100644 --- a/test/sequential/test-cli-syntax-file-not-found.js +++ b/test/sequential/test-cli-syntax-file-not-found.js @@ -25,8 +25,8 @@ const notFoundRE = /^Error: Cannot find module/m; // Loop each possible option, `-c` or `--check` syntaxArgs.forEach(function(args) { const _args = args.concat(file); - const cmd = [node, ..._args].join(' '); - exec(cmd, common.mustCall((err, stdout, stderr) => { + const cmd = ['"$NODE"', ..._args].join(' '); + exec(cmd, { env: { NODE: node } }, common.mustCall((err, stdout, stderr) => { // No stdout should be produced assert.strictEqual(stdout, ''); diff --git a/test/sequential/test-cli-syntax-good.js b/test/sequential/test-cli-syntax-good.js index 44051c7a4a3617..af1e0861a709b0 100644 --- a/test/sequential/test-cli-syntax-good.js +++ b/test/sequential/test-cli-syntax-good.js @@ -28,8 +28,8 @@ const syntaxArgs = [ syntaxArgs.forEach(function(args) { const _args = args.concat(file); - const cmd = [node, ..._args].join(' '); - exec(cmd, common.mustCall((err, stdout, stderr) => { + const cmd = ['"$NODE"', ..._args].join(' '); + exec(cmd, { env: { NODE: node } }, common.mustCall((err, stdout, stderr) => { if (err) { console.log('-- stdout --'); console.log(stdout); diff --git a/test/sequential/test-fs-stat-sync-overflow.js b/test/sequential/test-fs-stat-sync-overflow.js index 8855c374bf7d2b..0150ce0c2d43ba 100644 --- a/test/sequential/test-fs-stat-sync-overflow.js +++ b/test/sequential/test-fs-stat-sync-overflow.js @@ -20,8 +20,8 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); -const { fixturesDir } = require('../common/fixtures'); +const common = require('../common'); +const fixtures = require('../common/fixtures'); // Check that the calls to Integer::New() and Date::New() succeed and bail out // if they don't. @@ -31,11 +31,13 @@ const { fixturesDir } = require('../common/fixtures'); // https://github.com/nodejs/node-v0.x-archive/issues/4015 const assert = require('assert'); -const { exec } = require('child_process'); +const { spawn } = require('child_process'); -const cmd = - `"${process.execPath}" "${fixturesDir}/test-fs-stat-sync-overflow.js"`; +const cp = spawn(process.execPath, [fixtures.path('test-fs-stat-sync-overflow.js')]); -exec(cmd, function(err, stdout, stderr) { - assert.match(stderr, /RangeError: Maximum call stack size exceeded/); -}); +const stderr = []; +cp.stderr.on('data', (chunk) => stderr.push(chunk)); + +cp.on('exit', common.mustCall(() => { + assert.match(Buffer.concat(stderr).toString('utf8'), /RangeError: Maximum call stack size exceeded/); +})); diff --git a/test/sequential/test-init.js b/test/sequential/test-init.js index b64fb23daeabc7..7762ec9a9cfd9c 100644 --- a/test/sequential/test-init.js +++ b/test/sequential/test-init.js @@ -33,9 +33,10 @@ if (process.env.TEST_INIT) { } process.env.TEST_INIT = 1; +process.env.NODE = process.execPath; function test(file, expected) { - const path = `"${process.execPath}" ${file}`; + const path = `"$NODE" ${file}`; child.exec(path, { env: process.env }, common.mustSucceed((out) => { assert.strictEqual(out, expected, `'node ${file}' failed!`); }));