From d87792cfa40681ca2e61d2592b5592591d0fc73b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 13 Aug 2023 13:16:47 +0200 Subject: [PATCH 1/2] test: use env variable to pass value containing user controlled value When tests are run from a cwd that contains special characters in its path, those would be misinterpreted by the shell and make the test fail. --- test/abort/test-abort-fatal-error.js | 24 ++-- test/async-hooks/test-callback-error.js | 3 +- test/common/index.js | 15 ++- test/parallel/test-child-process-bad-stdio.js | 3 +- .../test-child-process-exec-encoding.js | 3 +- .../test-child-process-exec-maxbuf.js | 41 +++--- .../test-child-process-exec-std-encoding.js | 5 +- .../test-child-process-exec-timeout-expire.js | 3 +- .../test-child-process-exec-timeout-kill.js | 3 +- ...-child-process-exec-timeout-not-expired.js | 3 +- test/parallel/test-child-process-execfile.js | 4 +- .../test-child-process-execsync-maxbuf.js | 12 +- .../test-child-process-promisified.js | 4 +- .../test-child-process-spawn-shell.js | 4 +- .../test-child-process-spawnsync-shell.js | 4 +- test/parallel/test-cli-eval.js | 125 +++++++++++------- test/parallel/test-cli-node-print-help.js | 2 +- test/parallel/test-cli-syntax-eval.js | 4 +- test/parallel/test-crypto-sign-verify.js | 17 ++- .../parallel/test-domain-abort-on-uncaught.js | 6 +- ...n-throw-from-uncaught-exception-handler.js | 8 +- ...domain-with-abort-on-uncaught-exception.js | 5 +- test/parallel/test-env-var-no-warnings.js | 4 +- test/parallel/test-error-reporting.js | 4 +- test/parallel/test-fs-read-stream.js | 2 +- 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-write-sigxfsz.js | 4 +- test/parallel/test-http-chunk-problem.js | 10 +- test/parallel/test-npm-install.js | 6 +- ...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-stdin-from-file-spawn.js | 3 +- test/parallel/test-stdin-from-file.js | 6 +- test/parallel/test-stdio-closed.js | 4 +- test/parallel/test-stdout-close-catch.js | 8 +- test/parallel/test-stdout-to-file.js | 9 +- test/parallel/test-stream-pipeline-process.js | 11 +- test/parallel/test-tls-ecdh.js | 4 +- test/parallel/test-worker-init-failure.js | 4 +- test/sequential/test-child-process-emfile.js | 3 +- .../sequential/test-child-process-execsync.js | 14 +- test/sequential/test-cli-syntax-bad.js | 6 +- .../test-cli-syntax-file-not-found.js | 6 +- test/sequential/test-cli-syntax-good.js | 6 +- test/sequential/test-init.js | 3 +- 50 files changed, 250 insertions(+), 208 deletions(-) diff --git a/test/abort/test-abort-fatal-error.js b/test/abort/test-abort-fatal-error.js index b723b5b920849b..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; ${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/common/index.js b/test/common/index.js index c10dea59319264..a86b34afa24efa 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -244,9 +244,9 @@ function childShouldThrowAndAbort() { // continuous testing and developers' machines testCmd += 'ulimit -c 0 && '; } - testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `; - testCmd += `"${process.argv[1]}" child`; - const child = exec(testCmd); + testCmd += '"$NODE" --abort-on-uncaught-exception '; + testCmd += '"$FILE" child'; + const child = exec(testCmd, { env: { NODE: process.argv[0], FILE: process.argv[1] } }); child.on('exit', function onExit(exitCode, signal) { const errMsg = 'Test should have aborted ' + `but instead exited with exit code ${exitCode}` + @@ -1063,6 +1063,15 @@ const common = { get checkoutEOL() { return fs.readFileSync(__filename).includes('\r\n') ? '\r\n' : '\n'; }, + + get isInsideCWDWithUnusualChars() { + const cwd = process.cwd(); + return cwd.includes('%') || + (!isWindows && cwd.includes('\\')) || + cwd.includes('\n') || + cwd.includes('\r') || + cwd.includes('\t'); + }, }; const validProperties = new Set(Object.keys(common)); diff --git a/test/parallel/test-child-process-bad-stdio.js b/test/parallel/test-child-process-bad-stdio.js index 1f382e2966043d..1d56cb81ab9c1f 100644 --- a/test/parallel/test-child-process-bad-stdio.js +++ b/test/parallel/test-child-process-bad-stdio.js @@ -27,7 +27,8 @@ ChildProcess.prototype.spawn = function() { }; function createChild(options, callback) { - const cmd = `"${process.execPath}" "${__filename}" child`; + const cmd = '"$NODE" "$FILE" child'; + options = { ...options, env: { ...options.env, NODE: process.execPath, FILE: __filename } }; return cp.exec(cmd, options, common.mustCall(callback)); } diff --git a/test/parallel/test-child-process-exec-encoding.js b/test/parallel/test-child-process-exec-encoding.js index 0c3178e3f20607..44c4fc7bfddead 100644 --- a/test/parallel/test-child-process-exec-encoding.js +++ b/test/parallel/test-child-process-exec-encoding.js @@ -13,7 +13,8 @@ if (process.argv[2] === 'child') { const expectedStdout = `${stdoutData}\n`; const expectedStderr = `${stderrData}\n`; function run(options, callback) { - const cmd = `"${process.execPath}" "${__filename}" child`; + const cmd = '"$NODE" "$FILE" child'; + options = { ...options, env: { ...options.env, NODE: process.execPath, FILE: __filename } }; cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => { callback(stdout, stderr); diff --git a/test/parallel/test-child-process-exec-maxbuf.js b/test/parallel/test-child-process-exec-maxbuf.js index c434c8531d7ebf..a5d3146256d0de 100644 --- a/test/parallel/test-child-process-exec-maxbuf.js +++ b/test/parallel/test-child-process-exec-maxbuf.js @@ -10,12 +10,14 @@ function runChecks(err, stdio, streamName, expected) { assert.deepStrictEqual(stdio[streamName], expected); } +const env = { NODE: process.execPath }; + // default value { const cmd = - `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`; + '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"'; - cp.exec(cmd, common.mustCall((err) => { + cp.exec(cmd, { env }, common.mustCall((err) => { assert(err instanceof RangeError); assert.strictEqual(err.message, 'stdout maxBuffer length exceeded'); assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER'); @@ -25,17 +27,17 @@ function runChecks(err, stdio, streamName, expected) { // default value { const cmd = - `${process.execPath} -e "console.log('a'.repeat(1024 * 1024 - 1))"`; + '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"'; - cp.exec(cmd, common.mustSucceed((stdout, stderr) => { + cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1)); assert.strictEqual(stderr, ''); })); } { - const cmd = `"${process.execPath}" -e "console.log('hello world');"`; - const options = { maxBuffer: Infinity }; + const cmd = '"$NODE" -e "console.log(\'hello world\');"'; + const options = { env, maxBuffer: Infinity }; cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout.trim(), 'hello world'); @@ -58,10 +60,11 @@ function runChecks(err, stdio, streamName, expected) { // default value { const cmd = - `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`; + '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"'; cp.exec( cmd, + { env }, common.mustCall((err, stdout, stderr) => { runChecks( err, @@ -76,9 +79,9 @@ function runChecks(err, stdio, streamName, expected) { // default value { const cmd = - `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`; + '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"'; - cp.exec(cmd, common.mustSucceed((stdout, stderr) => { + cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1)); assert.strictEqual(stderr, ''); })); @@ -87,11 +90,11 @@ function runChecks(err, stdio, streamName, expected) { const unicode = '中文测试'; // length = 4, byte length = 12 { - const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`; + const cmd = `"$NODE" -e "console.log('${unicode}');"`; cp.exec( cmd, - { maxBuffer: 10 }, + { env, maxBuffer: 10 }, common.mustCall((err, stdout, stderr) => { runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n'); }) @@ -99,11 +102,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12 } { - const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`; + const cmd = `"$NODE" -e "console.error('${unicode}');"`; cp.exec( cmd, - { maxBuffer: 3 }, + { env, maxBuffer: 3 }, common.mustCall((err, stdout, stderr) => { runChecks(err, { stdout, stderr }, 'stderr', '中文测'); }) @@ -111,11 +114,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12 } { - const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`; + const cmd = `"$NODE" -e "console.log('${unicode}');"`; const child = cp.exec( cmd, - { encoding: null, maxBuffer: 10 }, + { encoding: null, env, maxBuffer: 10 }, common.mustCall((err, stdout, stderr) => { runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n'); }) @@ -125,11 +128,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12 } { - const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`; + const cmd = `"$NODE" -e "console.error('${unicode}');"`; const child = cp.exec( cmd, - { encoding: null, maxBuffer: 3 }, + { encoding: null, env, maxBuffer: 3 }, common.mustCall((err, stdout, stderr) => { runChecks(err, { stdout, stderr }, 'stderr', '中文测'); }) @@ -139,11 +142,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12 } { - const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`; + const cmd = `"$NODE" -e "console.error('${unicode}');"`; cp.exec( cmd, - { encoding: null, maxBuffer: 5 }, + { encoding: null, env, maxBuffer: 5 }, common.mustCall((err, stdout, stderr) => { const buf = Buffer.from(unicode).slice(0, 5); runChecks(err, { stdout, stderr }, 'stderr', buf); diff --git a/test/parallel/test-child-process-exec-std-encoding.js b/test/parallel/test-child-process-exec-std-encoding.js index 08187316726e96..2405d29c67a025 100644 --- a/test/parallel/test-child-process-exec-std-encoding.js +++ b/test/parallel/test-child-process-exec-std-encoding.js @@ -12,8 +12,9 @@ if (process.argv[2] === 'child') { console.log(stdoutData); console.error(stderrData); } else { - const cmd = `"${process.execPath}" "${__filename}" child`; - const child = cp.exec(cmd, common.mustSucceed((stdout, stderr) => { + const cmd = '"$NODE" "$FILE" child'; + const env = { NODE: process.execPath, FILE: __filename }; + const child = cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, expectedStdout); assert.strictEqual(stderr, expectedStderr); })); diff --git a/test/parallel/test-child-process-exec-timeout-expire.js b/test/parallel/test-child-process-exec-timeout-expire.js index 08e54544836d98..1d8238098ab51c 100644 --- a/test/parallel/test-child-process-exec-timeout-expire.js +++ b/test/parallel/test-child-process-exec-timeout-expire.js @@ -18,9 +18,10 @@ if (process.argv[2] === 'child') { return; } -const cmd = `"${process.execPath}" "${__filename}" child`; +const cmd = '"$NODE" "$FILE" child'; cp.exec(cmd, { + env: { NODE: process.execPath, FILE: __filename }, timeout: kExpiringParentTimer, }, common.mustCall((err, stdout, stderr) => { console.log('[stdout]', stdout.trim()); diff --git a/test/parallel/test-child-process-exec-timeout-kill.js b/test/parallel/test-child-process-exec-timeout-kill.js index 845fd1eaece24d..ce1059eab8b73d 100644 --- a/test/parallel/test-child-process-exec-timeout-kill.js +++ b/test/parallel/test-child-process-exec-timeout-kill.js @@ -18,10 +18,11 @@ if (process.argv[2] === 'child') { return; } -const cmd = `"${process.execPath}" "${__filename}" child`; +const cmd = '"$NODE" "$FILE" child'; // Test with a different kill signal. cp.exec(cmd, { + env: { NODE: process.execPath, FILE: __filename }, timeout: kExpiringParentTimer, killSignal: 'SIGKILL' }, common.mustCall((err, stdout, stderr) => { diff --git a/test/parallel/test-child-process-exec-timeout-not-expired.js b/test/parallel/test-child-process-exec-timeout-not-expired.js index fb0af5fa8f59d5..dbaaa14f50d71b 100644 --- a/test/parallel/test-child-process-exec-timeout-not-expired.js +++ b/test/parallel/test-child-process-exec-timeout-not-expired.js @@ -22,9 +22,10 @@ if (process.argv[2] === 'child') { return; } -const cmd = `"${process.execPath}" "${__filename}" child`; +const cmd = '"$NODE" "$FILE" child'; cp.exec(cmd, { + env: { NODE: process.execPath, FILE: __filename }, timeout: kTimeoutNotSupposedToExpire }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout.trim(), 'child stdout'); diff --git a/test/parallel/test-child-process-execfile.js b/test/parallel/test-child-process-execfile.js index e6e04ff61f93b8..6b269fe95d6f7e 100644 --- a/test/parallel/test-child-process-execfile.js +++ b/test/parallel/test-child-process-execfile.js @@ -10,7 +10,7 @@ const os = require('os'); const fixture = fixtures.path('exit.js'); const echoFixture = fixtures.path('echo.js'); -const execOpts = { encoding: 'utf8', shell: true }; +const execOpts = { encoding: 'utf8', shell: true, env: { NODE: process.execPath, FIXTURE: fixture } }; { execFile( @@ -46,7 +46,7 @@ const execOpts = { encoding: 'utf8', shell: true }; { // Verify the shell option works properly - execFile(process.execPath, [fixture, 0], execOpts, common.mustSucceed()); + execFile('"$NODE"', ['"$FIXTURE"', 0], execOpts, common.mustSucceed()); } { diff --git a/test/parallel/test-child-process-execsync-maxbuf.js b/test/parallel/test-child-process-execsync-maxbuf.js index 62b211cc3a3de1..7df7c98fadce7b 100644 --- a/test/parallel/test-child-process-execsync-maxbuf.js +++ b/test/parallel/test-child-process-execsync-maxbuf.js @@ -18,7 +18,7 @@ const args = [ // Verify that an error is returned if maxBuffer is surpassed. { assert.throws(() => { - execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 }); + execSync(`"$NODE" ${args.join(' ')}`, { env: { NODE: process.execPath }, maxBuffer: 1 }); }, (e) => { assert.ok(e, 'maxBuffer should error'); assert.strictEqual(e.code, 'ENOBUFS'); @@ -33,8 +33,8 @@ const args = [ // Verify that a maxBuffer size of Infinity works. { const ret = execSync( - `"${process.execPath}" ${args.join(' ')}`, - { maxBuffer: Infinity } + `"$NODE" ${args.join(' ')}`, + { env: { NODE: process.execPath }, maxBuffer: Infinity }, ); assert.deepStrictEqual(ret, msgOutBuf); @@ -44,7 +44,8 @@ const args = [ { assert.throws(() => { execSync( - `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"` + '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"', + { env: { NODE: process.execPath } }, ); }, (e) => { assert.ok(e, 'maxBuffer should error'); @@ -57,7 +58,8 @@ const args = [ // Default maxBuffer size is 1024 * 1024. { const ret = execSync( - `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"` + '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"', + { env: { NODE: process.execPath } }, ); assert.deepStrictEqual( diff --git a/test/parallel/test-child-process-promisified.js b/test/parallel/test-child-process-promisified.js index bc623130994a2e..f5a0306380856d 100644 --- a/test/parallel/test-child-process-promisified.js +++ b/test/parallel/test-child-process-promisified.js @@ -8,7 +8,7 @@ const exec = promisify(child_process.exec); const execFile = promisify(child_process.execFile); { - const promise = exec(`${process.execPath} -p 42`); + const promise = exec('"$NODE" -p 42', { env: { NODE: process.execPath } }); assert(promise.child instanceof child_process.ChildProcess); promise.then(common.mustCall((obj) => { @@ -45,7 +45,7 @@ const execFile = promisify(child_process.execFile); const failingCodeWithStdoutErr = 'console.log(42);console.error(43);process.exit(1)'; { - exec(`${process.execPath} -e "${failingCodeWithStdoutErr}"`) + exec(`"$NODE" -e "${failingCodeWithStdoutErr}"`, { env: { NODE: process.execPath } }) .catch(common.mustCall((err) => { assert.strictEqual(err.code, 1); assert.strictEqual(err.stdout, '42\n'); diff --git a/test/parallel/test-child-process-spawn-shell.js b/test/parallel/test-child-process-spawn-shell.js index 9b8de0507130d6..4c8f525030c12b 100644 --- a/test/parallel/test-child-process-spawn-shell.js +++ b/test/parallel/test-child-process-spawn-shell.js @@ -49,8 +49,8 @@ command.on('close', common.mustCall((code, signal) => { })); // Verify that the environment is properly inherited -const env = cp.spawn(`"${process.execPath}" -pe process.env.BAZ`, { - env: { ...process.env, BAZ: 'buzz' }, +const env = cp.spawn('"$NODE" -pe process.env.BAZ', { + env: { ...process.env, NODE: process.execPath, BAZ: 'buzz' }, encoding: 'utf8', shell: true }); diff --git a/test/parallel/test-child-process-spawnsync-shell.js b/test/parallel/test-child-process-spawnsync-shell.js index 0dd50cedd123b2..2122e0ff453f7b 100644 --- a/test/parallel/test-child-process-spawnsync-shell.js +++ b/test/parallel/test-child-process-spawnsync-shell.js @@ -36,8 +36,8 @@ const command = cp.spawnSync(cmd, { shell: true }); assert.strictEqual(command.stdout.toString().trim(), 'bar'); // Verify that the environment is properly inherited -const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, { - env: { ...process.env, BAZ: 'buzz' }, +const env = cp.spawnSync('"$NODE" -pe process.env.BAZ', { + env: { ...process.env, NODE: process.execPath, BAZ: 'buzz' }, shell: true }); diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index b993dd474149e9..fe78633af5e867 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -32,7 +32,7 @@ const assert = require('assert'); const child = require('child_process'); const path = require('path'); const fixtures = require('../common/fixtures'); -const nodejs = `"${process.execPath}"`; +const env = { NODE: process.execPath }; if (process.argv.length > 2) { console.log(process.argv.slice(2).join(' ')); @@ -40,13 +40,14 @@ if (process.argv.length > 2) { } // Assert that nothing is written to stdout. -child.exec(`${nodejs} --eval 42`, common.mustSucceed((stdout, stderr) => { +child.exec('"$NODE" --eval 42', { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, ''); assert.strictEqual(stderr, ''); })); // Assert that "42\n" is written to stderr. -child.exec(`${nodejs} --eval "console.error(42)"`, +child.exec('"$NODE" --eval "console.error(42)"', + { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, ''); assert.strictEqual(stderr, '42\n'); @@ -54,14 +55,14 @@ child.exec(`${nodejs} --eval "console.error(42)"`, // Assert that the expected output is written to stdout. ['--print', '-p -e', '-pe', '-p'].forEach((s) => { - const cmd = `${nodejs} ${s} `; + const cmd = `"$NODE" ${s} `; - child.exec(`${cmd}42`, common.mustSucceed((stdout, stderr) => { + child.exec(`${cmd}42`, { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, '42\n'); assert.strictEqual(stderr, ''); })); - child.exec(`${cmd} '[]'`, common.mustSucceed((stdout, stderr) => { + child.exec(`${cmd} '[]'`, { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, '[]\n'); assert.strictEqual(stderr, ''); })); @@ -73,7 +74,8 @@ child.exec(`${nodejs} --eval "console.error(42)"`, // interpreted as the escape character when put between quotes. const filename = __filename.replace(/\\/g, '/'); - child.exec(`${nodejs} --eval "require('${filename}')"`, + child.exec('"$NODE" --eval "$SCRIPT"', + { env: { NODE: process.execPath, SCRIPT: `require(${JSON.stringify(filename)})` } }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err.code, 42); assert.strictEqual( @@ -83,15 +85,16 @@ child.exec(`${nodejs} --eval "console.error(42)"`, } // Check that builtin modules are pre-defined. -child.exec(`${nodejs} --print "os.platform()"`, +child.exec('"$NODE" --print "os.platform()"', + { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stderr, ''); assert.strictEqual(stdout.trim(), require('os').platform()); })); // Module path resolve bug regression test. -child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`, - { cwd: path.resolve(__dirname, '../../') }, +child.exec('"$NODE" --eval "require(\'./test/parallel/test-cli-eval.js\')"', + { cwd: path.resolve(__dirname, '../../'), env }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err.code, 42); assert.strictEqual( @@ -100,7 +103,7 @@ child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`, })); // Missing argument should not crash. -child.exec(`${nodejs} -e`, common.mustCall((err, stdout, stderr) => { +child.exec('"$NODE" -e', { env }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err.code, 9); assert.strictEqual(stdout, ''); assert.strictEqual(stderr.trim(), @@ -108,18 +111,19 @@ child.exec(`${nodejs} -e`, common.mustCall((err, stdout, stderr) => { })); // Empty program should do nothing. -child.exec(`${nodejs} -e ""`, common.mustSucceed((stdout, stderr) => { +child.exec('"$NODE" -e ""', { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, ''); assert.strictEqual(stderr, ''); })); // "\\-42" should be interpreted as an escaped expression, not a switch. -child.exec(`${nodejs} -p "\\-42"`, common.mustSucceed((stdout, stderr) => { +child.exec('"$NODE" -p "\\-42"', { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, '-42\n'); assert.strictEqual(stderr, ''); })); -child.exec(`${nodejs} --use-strict -p process.execArgv`, +child.exec('"$NODE" --use-strict -p process.execArgv', + { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual( stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n" @@ -134,7 +138,8 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, emptyFile = emptyFile.replace(/\\/g, '\\\\'); } - child.exec(`${nodejs} -e 'require("child_process").fork("${emptyFile}")'`, + child.exec('"$NODE" -e "$SCRIPT"', + { env: { NODE: process.execPath, SCRIPT: `require("child_process").fork(${JSON.stringify(emptyFile)})` } }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, ''); assert.strictEqual(stderr, ''); @@ -142,13 +147,13 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, // Make sure that monkey-patching process.execArgv doesn't cause child_process // to incorrectly munge execArgv. - child.exec( - `${nodejs} -e "process.execArgv = ['-e', 'console.log(42)', 'thirdArg'];` + - `require('child_process').fork('${emptyFile}')"`, - common.mustSucceed((stdout, stderr) => { - assert.strictEqual(stdout, '42\n'); - assert.strictEqual(stderr, ''); - })); + child.exec('"$NODE" -e "$SCRIPT"', { env: { NODE: process.execPath, SCRIPT: + 'process.execArgv = [\'-e\', \'console.log(42)\', \'thirdArg\'];' + + `require('child_process').fork(${JSON.stringify(emptyFile)})` } }, + common.mustSucceed((stdout, stderr) => { + assert.strictEqual(stdout, '42\n'); + assert.strictEqual(stderr, ''); + })); } // Regression test for https://github.com/nodejs/node/issues/8534. @@ -193,8 +198,8 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, // Ensure that arguments are successfully passed to eval. const opt = ' --eval "console.log(process.argv.slice(1).join(\' \'))"'; - const cmd = `${nodejs}${opt} -- ${args}`; - child.exec(cmd, common.mustCall(function(err, stdout, stderr) { + const cmd = `"$NODE" ${opt} -- ${args}`; + child.exec(cmd, { env }, common.mustCall(function(err, stdout, stderr) { assert.strictEqual(stdout, `${args}\n`); assert.strictEqual(stderr, ''); assert.strictEqual(err, null); @@ -202,8 +207,8 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, // Ensure that arguments are successfully passed to print. const popt = ' --print "process.argv.slice(1).join(\' \')"'; - const pcmd = `${nodejs}${popt} -- ${args}`; - child.exec(pcmd, common.mustCall(function(err, stdout, stderr) { + const pcmd = `"$NODE" ${popt} -- ${args}`; + child.exec(pcmd, { env }, common.mustCall(function(err, stdout, stderr) { assert.strictEqual(stdout, `${args}\n`); assert.strictEqual(stderr, ''); assert.strictEqual(err, null); @@ -212,12 +217,14 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, // Ensure that arguments are successfully passed to a script. // The first argument after '--' should be interpreted as a script // filename. - const filecmd = `${nodejs} -- "${__filename}" ${args}`; - child.exec(filecmd, common.mustCall(function(err, stdout, stderr) { - assert.strictEqual(stdout, `${args}\n`); - assert.strictEqual(stderr, ''); - assert.strictEqual(err, null); - })); + const filecmd = `"$NODE" -- "$FILE" ${args}`; + child.exec(filecmd, + { env: { NODE: process.execPath, FILE: __filename } }, + common.mustCall(function(err, stdout, stderr) { + assert.strictEqual(stdout, `${args}\n`); + assert.strictEqual(stderr, ''); + assert.strictEqual(err, null); + })); }); // ESModule eval tests @@ -226,14 +233,16 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, // Assert that "42\n" is written to stdout on module eval. const execOptions = '--input-type module'; child.exec( - `${nodejs} ${execOptions} --eval "console.log(42)"`, + `"$NODE" ${execOptions} --eval "console.log(42)"`, + { env }, common.mustSucceed((stdout) => { assert.strictEqual(stdout, '42\n'); })); // Assert that "42\n" is written to stdout with print option. child.exec( - `${nodejs} ${execOptions} --print --eval "42"`, + `"$NODE" ${execOptions} --print --eval "42"`, + { env }, common.mustCall((err, stdout, stderr) => { assert.ok(err); assert.strictEqual(stdout, ''); @@ -242,7 +251,8 @@ child.exec( // Assert that error is written to stderr on invalid input. child.exec( - `${nodejs} ${execOptions} --eval "!!!!"`, + `"$NODE" ${execOptions} --eval "!!!!"`, + { env }, common.mustCall((err, stdout, stderr) => { assert.ok(err); assert.strictEqual(stdout, ''); @@ -251,22 +261,25 @@ child.exec( // Assert that require is undefined in ESM support child.exec( - `${nodejs} ${execOptions} --eval "console.log(typeof require);"`, + `"$NODE" ${execOptions} --eval "console.log(typeof require);"`, + { env }, common.mustSucceed((stdout) => { assert.strictEqual(stdout, 'undefined\n'); })); // Assert that import.meta is defined in ESM child.exec( - `${nodejs} ${execOptions} --eval "console.log(typeof import.meta);"`, + `"$NODE" ${execOptions} --eval "console.log(typeof import.meta);"`, + { env }, common.mustSucceed((stdout) => { assert.strictEqual(stdout, 'object\n'); })); // Assert that packages can be imported cwd-relative with --eval child.exec( - `${nodejs} ${execOptions} ` + + `"$NODE" ${execOptions} ` + '--eval "import \'./test/fixtures/es-modules/mjs-file.mjs\'"', + { env }, common.mustSucceed((stdout) => { assert.strictEqual(stdout, '.mjs file\n'); })); @@ -274,17 +287,19 @@ child.exec( // Assert that packages can be dynamic imported initial cwd-relative with --eval child.exec( - `${nodejs} ${execOptions} ` + + `"$NODE" ${execOptions} ` + '--eval "process.chdir(\'..\');' + 'import(\'./test/fixtures/es-modules/mjs-file.mjs\')"', + { env }, common.mustSucceed((stdout) => { assert.strictEqual(stdout, '.mjs file\n'); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '--eval "process.chdir(\'..\');' + 'import(\'./test/fixtures/es-modules/mjs-file.mjs\')"', + { env }, common.mustSucceed((stdout) => { assert.strictEqual(stdout, '.mjs file\n'); })); @@ -292,65 +307,75 @@ child.exec( if (common.hasCrypto) { // Assert that calls to crypto utils work without require. child.exec( - `${nodejs} ` + + '"$NODE" ' + '-e "console.log(crypto.randomBytes(16).toString(\'hex\'))"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /[0-9a-f]{32}/i); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '-p "crypto.randomBytes(16).toString(\'hex\')"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /[0-9a-f]{32}/i); })); } // Assert that overriding crypto works. child.exec( - `${nodejs} ` + + '"$NODE" ' + '-p "crypto=Symbol(\'test\')"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /Symbol\(test\)/i); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '-e "crypto = {};console.log(\'randomBytes\', typeof crypto.randomBytes)"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /randomBytes\sundefined/); })); // Assert that overriding crypto with a local variable works. child.exec( - `${nodejs} ` + + '"$NODE" ' + '-e "const crypto = {};console.log(\'randomBytes\', typeof crypto.randomBytes)"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /randomBytes\sundefined/); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '-e "let crypto = {};console.log(\'randomBytes\', typeof crypto.randomBytes)"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /randomBytes\sundefined/); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '-e "var crypto = {};console.log(\'randomBytes\', typeof crypto.randomBytes)"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /randomBytes\sundefined/); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '-p "const crypto = {randomBytes:1};typeof crypto.randomBytes"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /^number/); })); child.exec( - `${nodejs} ` + + '"$NODE" ' + '-p "let crypto = {randomBytes:1};typeof crypto.randomBytes"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /^number/); })); child.exec( - `${nodejs} --no-experimental-global-webcrypto ` + + '"$NODE" --no-experimental-global-webcrypto ' + '-p "var crypto = {randomBytes:1};typeof crypto.randomBytes"', + { env }, common.mustSucceed((stdout) => { assert.match(stdout, /^number/); })); diff --git a/test/parallel/test-cli-node-print-help.js b/test/parallel/test-cli-node-print-help.js index 7e7c77f53e9657..8ba7cd862d00f4 100644 --- a/test/parallel/test-cli-node-print-help.js +++ b/test/parallel/test-cli-node-print-help.js @@ -12,7 +12,7 @@ let stdOut; function startPrintHelpTest() { - exec(`${process.execPath} --help`, common.mustSucceed((stdout, stderr) => { + exec('"$NODE" --help', { env: { NODE: process.execPath } }, common.mustSucceed((stdout, stderr) => { stdOut = stdout; validateNodePrintHelp(); })); diff --git a/test/parallel/test-cli-syntax-eval.js b/test/parallel/test-cli-syntax-eval.js index 31fe2d3449d824..fa0f87950b086a 100644 --- a/test/parallel/test-cli-syntax-eval.js +++ b/test/parallel/test-cli-syntax-eval.js @@ -10,8 +10,8 @@ const node = process.execPath; ['-c', '--check'].forEach(function(checkFlag) { ['-e', '--eval'].forEach(function(evalFlag) { const args = [checkFlag, evalFlag, 'foo']; - 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, 9); assert( diff --git a/test/parallel/test-crypto-sign-verify.js b/test/parallel/test-crypto-sign-verify.js index 74c0ff53eb18b7..e6d6abeaa336e4 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -622,12 +622,17 @@ assert.throws( const msgfile = path.join(tmpdir.path, 's5.msg'); fs.writeFileSync(msgfile, msg); - const cmd = - `"${common.opensslCli}" dgst -sha256 -verify "${pubfile}" -signature "${ - sigfile}" -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-2 "${ - msgfile}"`; - - exec(cmd, common.mustCall((err, stdout, stderr) => { + const cmd = [ + '"$OPENSSL" dgst -sha256', + '-verify "$PUBFILE"', + '-signature "$SIGFILE" -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-2', + '"$MSGFILE"', + ].join(' '); + + exec(cmd, { env: { OPENSSL: common.opensslCli, + PUBFILE: pubfile, + SIGFILE: sigfile, + MSGFILE: msgfile } }, common.mustCall((err, stdout, stderr) => { assert(stdout.includes('Verified OK')); })); } diff --git a/test/parallel/test-domain-abort-on-uncaught.js b/test/parallel/test-domain-abort-on-uncaught.js index 08551721c1d39b..1f49a4bffcfbbe 100644 --- a/test/parallel/test-domain-abort-on-uncaught.js +++ b/test/parallel/test-domain-abort-on-uncaught.js @@ -209,11 +209,11 @@ if (process.argv[2] === 'child') { testCmd += 'ulimit -c 0 && '; } - testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception ` + - `"${process.argv[1]}" child ${testIndex}`; + testCmd += '"$NODE" --abort-on-uncaught-exception ' + + `"$FILE" child ${testIndex}`; try { - child_process.execSync(testCmd); + child_process.execSync(testCmd, { env: { NODE: process.execPath, FILE: __filename } }); } catch (e) { assert.fail(`Test index ${testIndex} failed: ${e}`); } diff --git a/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js b/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js index a2afebd838f410..f325937428acff 100644 --- a/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js +++ b/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js @@ -13,6 +13,7 @@ const domain = require('domain'); const uncaughtExceptionHandlerErrMsg = 'boom from uncaughtException handler'; const domainErrMsg = 'boom from domain'; +const env = { NODE: process.execPath, FILE: __filename }; const RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE = 42; if (process.argv[2] === 'child') { @@ -51,6 +52,7 @@ if (process.argv[2] === 'child') { function runTestWithoutAbortOnUncaughtException() { child_process.exec( createTestCmdLine(), + { env }, function onTestDone(err, stdout, stderr) { // When _not_ passing --abort-on-uncaught-exception, the process' // uncaughtException handler _must_ be called, and thus the error @@ -72,7 +74,7 @@ function runTestWithoutAbortOnUncaughtException() { function runTestWithAbortOnUncaughtException() { child_process.exec(createTestCmdLine({ withAbortOnUncaughtException: true - }), function onTestDone(err, stdout, stderr) { + }), { env }, function onTestDone(err, stdout, stderr) { assert.notStrictEqual(err.code, RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE, 'child process should not have run its ' + 'uncaughtException event handler'); @@ -90,13 +92,13 @@ function createTestCmdLine(options) { testCmd += 'ulimit -c 0 && '; } - testCmd += `"${process.argv[0]}"`; + testCmd += '"$NODE"'; if (options && options.withAbortOnUncaughtException) { testCmd += ' --abort-on-uncaught-exception'; } - testCmd += ` "${process.argv[1]}" child`; + testCmd += ' "$FILE" child'; return testCmd; } diff --git a/test/parallel/test-domain-with-abort-on-uncaught-exception.js b/test/parallel/test-domain-with-abort-on-uncaught-exception.js index 5f10b19926b955..d9e5370a101376 100644 --- a/test/parallel/test-domain-with-abort-on-uncaught-exception.js +++ b/test/parallel/test-domain-with-abort-on-uncaught-exception.js @@ -102,10 +102,9 @@ if (process.argv[2] === 'child') { if (options.useTryCatch) useTryCatchOpt = 'useTryCatch'; - cmdToExec += `"${process.argv[0]}" ${cmdLineOption ? cmdLineOption : ''} "${ - process.argv[1]}" child ${throwInDomainErrHandlerOpt} ${useTryCatchOpt}`; + cmdToExec += `"$NODE" ${cmdLineOption ? cmdLineOption : ''} "$FILE" child ${throwInDomainErrHandlerOpt} ${useTryCatchOpt}`; - const child = exec(cmdToExec); + const child = exec(cmdToExec, { env: { NODE: process.execPath, FILE: __filename } }); if (child) { child.on('exit', function onChildExited(exitCode, signal) { diff --git a/test/parallel/test-env-var-no-warnings.js b/test/parallel/test-env-var-no-warnings.js index b6164da1039a55..0efd703530f1e3 100644 --- a/test/parallel/test-env-var-no-warnings.js +++ b/test/parallel/test-env-var-no-warnings.js @@ -7,8 +7,8 @@ if (process.argv[2] === 'child') { process.emitWarning('foo'); } else { function test(newEnv) { - const env = { ...process.env, ...newEnv }; - const cmd = `"${process.execPath}" "${__filename}" child`; + const env = { ...process.env, ...newEnv, NODE: process.execPath, FILE: __filename }; + const cmd = '"$NODE" "$FILE" child'; cp.exec(cmd, { env }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err, null); diff --git a/test/parallel/test-error-reporting.js b/test/parallel/test-error-reporting.js index 98abf949fb0c0f..afae1106c35c4b 100644 --- a/test/parallel/test-error-reporting.js +++ b/test/parallel/test-error-reporting.js @@ -28,8 +28,8 @@ const fixtures = require('../common/fixtures'); function errExec(script, option, callback) { callback = typeof option === 'function' ? option : callback; option = typeof option === 'string' ? option : ''; - const cmd = `"${process.argv[0]}" ${option} "${fixtures.path(script)}"`; - return exec(cmd, (err, stdout, stderr) => { + const cmd = `"$NODE" ${option} "$SCRIPT"`; + return exec(cmd, { env: { NODE: process.execPath, SCRIPT: fixtures.path(script) } }, (err, stdout, stderr) => { // There was some error assert.ok(err); diff --git a/test/parallel/test-fs-read-stream.js b/test/parallel/test-fs-read-stream.js index a038eac1efdfa1..bbb9b9baac0d2b 100644 --- a/test/parallel/test-fs-read-stream.js +++ b/test/parallel/test-fs-read-stream.js @@ -198,7 +198,7 @@ if (!common.isWindows) { const filename = `${tmpdir.path}/foo.pipe`; const mkfifoResult = child_process.spawnSync('mkfifo', [filename]); if (!mkfifoResult.error) { - child_process.exec(`echo "xyz foobar" > '${filename}'`); + child_process.exec('echo "xyz foobar" > "$FILE"', { env: { FILE: filename } }); const stream = new fs.createReadStream(filename, common.mustNotMutateObjectDeep({ end: 1 })); stream.data = ''; 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..57809d2db9a205 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..346b1712e8bc11 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..a08ab6f86ffab0 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-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-http-chunk-problem.js b/test/parallel/test-http-chunk-problem.js index ebfd940687947e..b87a7e0ce797da 100644 --- a/test/parallel/test-http-chunk-problem.js +++ b/test/parallel/test-http-chunk-problem.js @@ -43,14 +43,8 @@ const filename = require('path').join(tmpdir.path, 'big'); let server; function executeRequest(cb) { - cp.exec([`"${process.execPath}"`, - `"${__filename}"`, - 'request', - server.address().port, - '|', - `"${process.execPath}"`, - `"${__filename}"`, - 'shasum' ].join(' '), + cp.exec('"$NODE" "$FILE" request "$PORT" | "$NODE" "$FILE" shasum', + { env: { NODE: process.execPath, FILE: __filename, PORT: server.address().port } }, (err, stdout, stderr) => { if (stderr.trim() !== '') { console.log(stderr); diff --git a/test/parallel/test-npm-install.js b/test/parallel/test-npm-install.js index d3ba8ab8b55561..f08cb51178f98b 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)); @@ -61,5 +63,5 @@ function handleExit(error, stdout, stderr) { assert.strictEqual(code, 0, `npm install got error code ${code}`); assert.strictEqual(signalCode, null, `unexpected signal: ${signalCode}`); - assert(fs.existsSync(`${installDir}/node_modules/package-name`)); + assert(fs.existsSync(path.join(installDir, 'node_modules/package-name'))); } 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-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-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..d4f14d7d2406ea 100644 --- a/test/parallel/test-stdout-to-file.js +++ b/test/parallel/test-stdout-to-file.js @@ -14,8 +14,7 @@ const tmpFile = path.join(tmpdir.path, 'stdout.txt'); tmpdir.refresh(); function test(size, useBuffer, cb) { - const cmd = `"${process.argv[0]}" "${ - useBuffer ? scriptBuffer : scriptString}" ${size} > "${tmpFile}"`; + const cmd = `"$NODE" "$SCRIPT" ${size} > "$TMP_FILE"`; try { fs.unlinkSync(tmpFile); @@ -25,7 +24,11 @@ function test(size, useBuffer, cb) { console.log(`${size} chars to ${tmpFile}...`); - childProcess.exec(cmd, common.mustSucceed(() => { + childProcess.exec(cmd, { env: { + NODE: process.execPath, + SCRIPT: useBuffer ? scriptBuffer : scriptString, + TMP_FILE: tmpFile, + } }, common.mustSucceed(() => { console.log('done!'); const stat = fs.statSync(tmpFile); diff --git a/test/parallel/test-stream-pipeline-process.js b/test/parallel/test-stream-pipeline-process.js index a535e7263ebf64..a57f461828fe73 100644 --- a/test/parallel/test-stream-pipeline-process.js +++ b/test/parallel/test-stream-pipeline-process.js @@ -13,14 +13,9 @@ if (process.argv[2] === 'child') { ); } else { const cp = require('child_process'); - cp.exec([ - 'echo', - 'hello', - '|', - `"${process.execPath}"`, - `"${__filename}"`, - 'child', - ].join(' '), common.mustSucceed((stdout) => { + cp.exec('echo hello | "$NODE" "$FILE" child', { + env: { NODE: process.execPath, FILE: __filename } + }, common.mustSucceed((stdout) => { assert.strictEqual(stdout.split(os.EOL).shift().trim(), 'hello'); })); } 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-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/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..b935ac69f41451 100644 --- a/test/sequential/test-child-process-execsync.js +++ b/test/sequential/test-child-process-execsync.js @@ -38,7 +38,7 @@ if (common.isWindows) { SLEEP = 10000; } -const execOpts = { encoding: 'utf8', shell: true }; +const execOpts = { encoding: 'utf8', shell: true, env: { NODE: process.execPath } }; // Verify that stderr is not accessed when a bad shell is used assert.throws( @@ -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`); } @@ -156,4 +156,4 @@ const args = [ } // Verify the shell option works properly -execFileSync(process.execPath, [], execOpts); +execFileSync('"$NODE"', [], execOpts); diff --git a/test/sequential/test-cli-syntax-bad.js b/test/sequential/test-cli-syntax-bad.js index 5c87bf11419af6..5417556fe47103 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -28,9 +28,9 @@ 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 _args = args.concat('"$FILE"'); + const cmd = ['"$NODE"', ..._args].join(' '); + exec(cmd, { env: { NODE: node, FILE: file } }, 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..66bd15afe8135d 100644 --- a/test/sequential/test-cli-syntax-file-not-found.js +++ b/test/sequential/test-cli-syntax-file-not-found.js @@ -24,9 +24,9 @@ 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 _args = args.concat('"$FILE"'); + const cmd = ['"$NODE"', ..._args].join(' '); + exec(cmd, { env: { NODE: node, FILE: file } }, 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..c1812000665742 100644 --- a/test/sequential/test-cli-syntax-good.js +++ b/test/sequential/test-cli-syntax-good.js @@ -26,10 +26,10 @@ const syntaxArgs = [ // Loop each possible option, `-c` or `--check` syntaxArgs.forEach(function(args) { - const _args = args.concat(file); + 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, FILE: file } }, common.mustCall((err, stdout, stderr) => { if (err) { console.log('-- stdout --'); console.log(stdout); 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!`); })); From 9d60dbfb4ca0b901c4b226448ed3168d52cb49b7 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Aug 2023 19:01:20 +0200 Subject: [PATCH 2/2] fixup! test: use env variable to pass value containing user controlled value --- test/abort/test-abort-fatal-error.js | 2 +- test/async-hooks/test-callback-error.js | 2 +- test/common/index.js | 11 +---------- test/parallel/test-child-process-exec-maxbuf.js | 2 +- test/parallel/test-child-process-exec-std-encoding.js | 2 +- .../test-child-process-exec-timeout-expire.js | 2 +- test/parallel/test-child-process-exec-timeout-kill.js | 2 +- .../test-child-process-exec-timeout-not-expired.js | 2 +- test/parallel/test-child-process-execfile.js | 2 +- test/parallel/test-child-process-execsync-maxbuf.js | 8 ++++---- test/parallel/test-child-process-promisified.js | 4 ++-- test/parallel/test-cli-eval.js | 10 +++++----- test/parallel/test-cli-node-print-help.js | 2 +- test/parallel/test-cli-syntax-eval.js | 2 +- test/parallel/test-crypto-sign-verify.js | 3 ++- test/parallel/test-domain-abort-on-uncaught.js | 2 +- ...rror-then-throw-from-uncaught-exception-handler.js | 2 +- .../test-domain-with-abort-on-uncaught-exception.js | 2 +- test/parallel/test-error-reporting.js | 3 ++- test/parallel/test-fs-read-stream.js | 2 +- test/parallel/test-fs-readfile-pipe-large.js | 1 + test/parallel/test-fs-readfile-pipe.js | 2 +- test/parallel/test-fs-readfilesync-pipe-large.js | 2 +- test/parallel/test-fs-write-sigxfsz.js | 3 ++- test/parallel/test-http-chunk-problem.js | 2 +- .../test-permission-allow-child-process-cli.js | 2 +- test/parallel/test-pipe-head.js | 2 +- test/parallel/test-preload-self-referential.js | 3 ++- test/parallel/test-preload-worker.js | 4 +++- test/parallel/test-stdin-from-file-spawn.js | 2 +- test/parallel/test-stdin-from-file.js | 2 +- test/parallel/test-stdio-closed.js | 3 ++- test/parallel/test-stdout-close-catch.js | 2 +- test/parallel/test-stdout-to-file.js | 1 + test/parallel/test-stream-pipeline-process.js | 2 +- test/parallel/test-tls-ecdh.js | 2 +- test/parallel/test-worker-init-failure.js | 2 +- test/sequential/test-child-process-emfile.js | 2 +- test/sequential/test-child-process-execsync.js | 8 ++++---- test/sequential/test-cli-syntax-bad.js | 2 +- test/sequential/test-cli-syntax-file-not-found.js | 2 +- test/sequential/test-cli-syntax-good.js | 2 +- 42 files changed, 60 insertions(+), 60 deletions(-) diff --git a/test/abort/test-abort-fatal-error.js b/test/abort/test-abort-fatal-error.js index 38f336620fa9af..300967749ac79b 100644 --- a/test/abort/test-abort-fatal-error.js +++ b/test/abort/test-abort-fatal-error.js @@ -31,7 +31,7 @@ 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, { env: { NODE: process.execPath } }, common.mustCall((err, stdout, stderr) => { +exec(cmdline, { env: { ...process.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); diff --git a/test/async-hooks/test-callback-error.js b/test/async-hooks/test-callback-error.js index 942393e45c0062..06453cd995b3cb 100644 --- a/test/async-hooks/test-callback-error.js +++ b/test/async-hooks/test-callback-error.js @@ -67,7 +67,7 @@ assert.ok(!arg); program = `ulimit -c 0 && exec "$NODE" ${args[0]} "$FILE" ${args[2]}`; args = []; options.shell = true; - options.env = { NODE: process.execPath, FILE: __filename }; + options.env = { ...process.env, NODE: process.execPath, FILE: __filename }; } const child = spawnSync(program, args, options); if (common.isWindows) { diff --git a/test/common/index.js b/test/common/index.js index a86b34afa24efa..6e3338064d00d7 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -246,7 +246,7 @@ function childShouldThrowAndAbort() { } testCmd += '"$NODE" --abort-on-uncaught-exception '; testCmd += '"$FILE" child'; - const child = exec(testCmd, { env: { NODE: process.argv[0], FILE: process.argv[1] } }); + const child = exec(testCmd, { env: { ...process.env, NODE: process.argv[0], FILE: process.argv[1] } }); child.on('exit', function onExit(exitCode, signal) { const errMsg = 'Test should have aborted ' + `but instead exited with exit code ${exitCode}` + @@ -1063,15 +1063,6 @@ const common = { get checkoutEOL() { return fs.readFileSync(__filename).includes('\r\n') ? '\r\n' : '\n'; }, - - get isInsideCWDWithUnusualChars() { - const cwd = process.cwd(); - return cwd.includes('%') || - (!isWindows && cwd.includes('\\')) || - cwd.includes('\n') || - cwd.includes('\r') || - cwd.includes('\t'); - }, }; const validProperties = new Set(Object.keys(common)); diff --git a/test/parallel/test-child-process-exec-maxbuf.js b/test/parallel/test-child-process-exec-maxbuf.js index a5d3146256d0de..08caead1bf425b 100644 --- a/test/parallel/test-child-process-exec-maxbuf.js +++ b/test/parallel/test-child-process-exec-maxbuf.js @@ -10,7 +10,7 @@ function runChecks(err, stdio, streamName, expected) { assert.deepStrictEqual(stdio[streamName], expected); } -const env = { NODE: process.execPath }; +const env = { ...process.env, NODE: process.execPath }; // default value { diff --git a/test/parallel/test-child-process-exec-std-encoding.js b/test/parallel/test-child-process-exec-std-encoding.js index 2405d29c67a025..0dad5a917d507f 100644 --- a/test/parallel/test-child-process-exec-std-encoding.js +++ b/test/parallel/test-child-process-exec-std-encoding.js @@ -13,7 +13,7 @@ if (process.argv[2] === 'child') { console.error(stderrData); } else { const cmd = '"$NODE" "$FILE" child'; - const env = { NODE: process.execPath, FILE: __filename }; + const env = { ...process.env, NODE: process.execPath, FILE: __filename }; const child = cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, expectedStdout); assert.strictEqual(stderr, expectedStderr); diff --git a/test/parallel/test-child-process-exec-timeout-expire.js b/test/parallel/test-child-process-exec-timeout-expire.js index 1d8238098ab51c..de5dadd69b00d5 100644 --- a/test/parallel/test-child-process-exec-timeout-expire.js +++ b/test/parallel/test-child-process-exec-timeout-expire.js @@ -21,7 +21,7 @@ if (process.argv[2] === 'child') { const cmd = '"$NODE" "$FILE" child'; cp.exec(cmd, { - env: { NODE: process.execPath, FILE: __filename }, + env: { ...process.env, NODE: process.execPath, FILE: __filename }, timeout: kExpiringParentTimer, }, common.mustCall((err, stdout, stderr) => { console.log('[stdout]', stdout.trim()); diff --git a/test/parallel/test-child-process-exec-timeout-kill.js b/test/parallel/test-child-process-exec-timeout-kill.js index ce1059eab8b73d..7a179f36d4ca96 100644 --- a/test/parallel/test-child-process-exec-timeout-kill.js +++ b/test/parallel/test-child-process-exec-timeout-kill.js @@ -22,7 +22,7 @@ const cmd = '"$NODE" "$FILE" child'; // Test with a different kill signal. cp.exec(cmd, { - env: { NODE: process.execPath, FILE: __filename }, + env: { ...process.env, NODE: process.execPath, FILE: __filename }, timeout: kExpiringParentTimer, killSignal: 'SIGKILL' }, common.mustCall((err, stdout, stderr) => { diff --git a/test/parallel/test-child-process-exec-timeout-not-expired.js b/test/parallel/test-child-process-exec-timeout-not-expired.js index dbaaa14f50d71b..793b504036dcd0 100644 --- a/test/parallel/test-child-process-exec-timeout-not-expired.js +++ b/test/parallel/test-child-process-exec-timeout-not-expired.js @@ -25,7 +25,7 @@ if (process.argv[2] === 'child') { const cmd = '"$NODE" "$FILE" child'; cp.exec(cmd, { - env: { NODE: process.execPath, FILE: __filename }, + env: { ...process.env, NODE: process.execPath, FILE: __filename }, timeout: kTimeoutNotSupposedToExpire }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout.trim(), 'child stdout'); diff --git a/test/parallel/test-child-process-execfile.js b/test/parallel/test-child-process-execfile.js index 6b269fe95d6f7e..4937b1ef9c9787 100644 --- a/test/parallel/test-child-process-execfile.js +++ b/test/parallel/test-child-process-execfile.js @@ -10,7 +10,7 @@ const os = require('os'); const fixture = fixtures.path('exit.js'); const echoFixture = fixtures.path('echo.js'); -const execOpts = { encoding: 'utf8', shell: true, env: { NODE: process.execPath, FIXTURE: fixture } }; +const execOpts = { encoding: 'utf8', shell: true, env: { ...process.env, NODE: process.execPath, FIXTURE: fixture } }; { execFile( diff --git a/test/parallel/test-child-process-execsync-maxbuf.js b/test/parallel/test-child-process-execsync-maxbuf.js index 7df7c98fadce7b..ab57e5e8ee40ed 100644 --- a/test/parallel/test-child-process-execsync-maxbuf.js +++ b/test/parallel/test-child-process-execsync-maxbuf.js @@ -18,7 +18,7 @@ const args = [ // Verify that an error is returned if maxBuffer is surpassed. { assert.throws(() => { - execSync(`"$NODE" ${args.join(' ')}`, { env: { NODE: process.execPath }, maxBuffer: 1 }); + execSync(`"$NODE" ${args.join(' ')}`, { env: { ...process.env, NODE: process.execPath }, maxBuffer: 1 }); }, (e) => { assert.ok(e, 'maxBuffer should error'); assert.strictEqual(e.code, 'ENOBUFS'); @@ -34,7 +34,7 @@ const args = [ { const ret = execSync( `"$NODE" ${args.join(' ')}`, - { env: { NODE: process.execPath }, maxBuffer: Infinity }, + { env: { ...process.env, NODE: process.execPath }, maxBuffer: Infinity }, ); assert.deepStrictEqual(ret, msgOutBuf); @@ -45,7 +45,7 @@ const args = [ assert.throws(() => { execSync( '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"', - { env: { NODE: process.execPath } }, + { env: { ...process.env, NODE: process.execPath } }, ); }, (e) => { assert.ok(e, 'maxBuffer should error'); @@ -59,7 +59,7 @@ const args = [ { const ret = execSync( '"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"', - { env: { NODE: process.execPath } }, + { env: { ...process.env, NODE: process.execPath } }, ); assert.deepStrictEqual( diff --git a/test/parallel/test-child-process-promisified.js b/test/parallel/test-child-process-promisified.js index f5a0306380856d..2c6c38a81eed87 100644 --- a/test/parallel/test-child-process-promisified.js +++ b/test/parallel/test-child-process-promisified.js @@ -8,7 +8,7 @@ const exec = promisify(child_process.exec); const execFile = promisify(child_process.execFile); { - const promise = exec('"$NODE" -p 42', { env: { NODE: process.execPath } }); + const promise = exec('"$NODE" -p 42', { env: { ...process.env, NODE: process.execPath } }); assert(promise.child instanceof child_process.ChildProcess); promise.then(common.mustCall((obj) => { @@ -45,7 +45,7 @@ const execFile = promisify(child_process.execFile); const failingCodeWithStdoutErr = 'console.log(42);console.error(43);process.exit(1)'; { - exec(`"$NODE" -e "${failingCodeWithStdoutErr}"`, { env: { NODE: process.execPath } }) + exec(`"$NODE" -e "${failingCodeWithStdoutErr}"`, { env: { ...process.env, NODE: process.execPath } }) .catch(common.mustCall((err) => { assert.strictEqual(err.code, 1); assert.strictEqual(err.stdout, '42\n'); diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index fe78633af5e867..cf31552b6304b9 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -32,7 +32,7 @@ const assert = require('assert'); const child = require('child_process'); const path = require('path'); const fixtures = require('../common/fixtures'); -const env = { NODE: process.execPath }; +const env = { ...process.env, NODE: process.execPath }; if (process.argv.length > 2) { console.log(process.argv.slice(2).join(' ')); @@ -75,7 +75,7 @@ child.exec('"$NODE" --eval "console.error(42)"', const filename = __filename.replace(/\\/g, '/'); child.exec('"$NODE" --eval "$SCRIPT"', - { env: { NODE: process.execPath, SCRIPT: `require(${JSON.stringify(filename)})` } }, + { env: { ...process.env, NODE: process.execPath, SCRIPT: `require(${JSON.stringify(filename)})` } }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err.code, 42); assert.strictEqual( @@ -139,7 +139,7 @@ child.exec('"$NODE" --use-strict -p process.execArgv', } child.exec('"$NODE" -e "$SCRIPT"', - { env: { NODE: process.execPath, SCRIPT: `require("child_process").fork(${JSON.stringify(emptyFile)})` } }, + { env: { ...process.env, NODE: process.execPath, SCRIPT: `require("child_process").fork(${JSON.stringify(emptyFile)})` } }, common.mustSucceed((stdout, stderr) => { assert.strictEqual(stdout, ''); assert.strictEqual(stderr, ''); @@ -147,7 +147,7 @@ child.exec('"$NODE" --use-strict -p process.execArgv', // Make sure that monkey-patching process.execArgv doesn't cause child_process // to incorrectly munge execArgv. - child.exec('"$NODE" -e "$SCRIPT"', { env: { NODE: process.execPath, SCRIPT: + child.exec('"$NODE" -e "$SCRIPT"', { env: { ...process.env, NODE: process.execPath, SCRIPT: 'process.execArgv = [\'-e\', \'console.log(42)\', \'thirdArg\'];' + `require('child_process').fork(${JSON.stringify(emptyFile)})` } }, common.mustSucceed((stdout, stderr) => { @@ -219,7 +219,7 @@ child.exec('"$NODE" --use-strict -p process.execArgv', // filename. const filecmd = `"$NODE" -- "$FILE" ${args}`; child.exec(filecmd, - { env: { NODE: process.execPath, FILE: __filename } }, + { env: { ...process.env, NODE: process.execPath, FILE: __filename } }, common.mustCall(function(err, stdout, stderr) { assert.strictEqual(stdout, `${args}\n`); assert.strictEqual(stderr, ''); diff --git a/test/parallel/test-cli-node-print-help.js b/test/parallel/test-cli-node-print-help.js index 8ba7cd862d00f4..636d2be446e8a3 100644 --- a/test/parallel/test-cli-node-print-help.js +++ b/test/parallel/test-cli-node-print-help.js @@ -12,7 +12,7 @@ let stdOut; function startPrintHelpTest() { - exec('"$NODE" --help', { env: { NODE: process.execPath } }, common.mustSucceed((stdout, stderr) => { + exec('"$NODE" --help', { env: { ...process.env, NODE: process.execPath } }, common.mustSucceed((stdout, stderr) => { stdOut = stdout; validateNodePrintHelp(); })); diff --git a/test/parallel/test-cli-syntax-eval.js b/test/parallel/test-cli-syntax-eval.js index fa0f87950b086a..9d957e7cfe8fdf 100644 --- a/test/parallel/test-cli-syntax-eval.js +++ b/test/parallel/test-cli-syntax-eval.js @@ -11,7 +11,7 @@ const node = process.execPath; ['-e', '--eval'].forEach(function(evalFlag) { const args = [checkFlag, evalFlag, 'foo']; const cmd = ['"$NODE"', ...args].join(' '); - exec(cmd, { env: { NODE: node } }, common.mustCall((err, stdout, stderr) => { + exec(cmd, { env: { ...process.env, NODE: node } }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err instanceof Error, true); assert.strictEqual(err.code, 9); assert( diff --git a/test/parallel/test-crypto-sign-verify.js b/test/parallel/test-crypto-sign-verify.js index e6d6abeaa336e4..09a166870d8030 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -629,7 +629,8 @@ assert.throws( '"$MSGFILE"', ].join(' '); - exec(cmd, { env: { OPENSSL: common.opensslCli, + exec(cmd, { env: { ...process.env, + OPENSSL: common.opensslCli, PUBFILE: pubfile, SIGFILE: sigfile, MSGFILE: msgfile } }, common.mustCall((err, stdout, stderr) => { diff --git a/test/parallel/test-domain-abort-on-uncaught.js b/test/parallel/test-domain-abort-on-uncaught.js index 1f49a4bffcfbbe..a4d4f463ba4785 100644 --- a/test/parallel/test-domain-abort-on-uncaught.js +++ b/test/parallel/test-domain-abort-on-uncaught.js @@ -213,7 +213,7 @@ if (process.argv[2] === 'child') { `"$FILE" child ${testIndex}`; try { - child_process.execSync(testCmd, { env: { NODE: process.execPath, FILE: __filename } }); + child_process.execSync(testCmd, { env: { ...process.env, NODE: process.execPath, FILE: __filename } }); } catch (e) { assert.fail(`Test index ${testIndex} failed: ${e}`); } diff --git a/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js b/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js index f325937428acff..1ceec8e50d9341 100644 --- a/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js +++ b/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js @@ -13,7 +13,7 @@ const domain = require('domain'); const uncaughtExceptionHandlerErrMsg = 'boom from uncaughtException handler'; const domainErrMsg = 'boom from domain'; -const env = { NODE: process.execPath, FILE: __filename }; +const env = { ...process.env, NODE: process.execPath, FILE: __filename }; const RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE = 42; if (process.argv[2] === 'child') { diff --git a/test/parallel/test-domain-with-abort-on-uncaught-exception.js b/test/parallel/test-domain-with-abort-on-uncaught-exception.js index d9e5370a101376..c41ea081675b56 100644 --- a/test/parallel/test-domain-with-abort-on-uncaught-exception.js +++ b/test/parallel/test-domain-with-abort-on-uncaught-exception.js @@ -104,7 +104,7 @@ if (process.argv[2] === 'child') { cmdToExec += `"$NODE" ${cmdLineOption ? cmdLineOption : ''} "$FILE" child ${throwInDomainErrHandlerOpt} ${useTryCatchOpt}`; - const child = exec(cmdToExec, { env: { NODE: process.execPath, FILE: __filename } }); + const child = exec(cmdToExec, { env: { ...process.env, NODE: process.execPath, FILE: __filename } }); if (child) { child.on('exit', function onChildExited(exitCode, signal) { diff --git a/test/parallel/test-error-reporting.js b/test/parallel/test-error-reporting.js index afae1106c35c4b..2de9b74a9608a1 100644 --- a/test/parallel/test-error-reporting.js +++ b/test/parallel/test-error-reporting.js @@ -29,7 +29,8 @@ function errExec(script, option, callback) { callback = typeof option === 'function' ? option : callback; option = typeof option === 'string' ? option : ''; const cmd = `"$NODE" ${option} "$SCRIPT"`; - return exec(cmd, { env: { NODE: process.execPath, SCRIPT: fixtures.path(script) } }, (err, stdout, stderr) => { + const env = { ...process.env, NODE: process.execPath, SCRIPT: fixtures.path(script) }; + return exec(cmd, { env }, (err, stdout, stderr) => { // There was some error assert.ok(err); diff --git a/test/parallel/test-fs-read-stream.js b/test/parallel/test-fs-read-stream.js index bbb9b9baac0d2b..c9cbe787a924d3 100644 --- a/test/parallel/test-fs-read-stream.js +++ b/test/parallel/test-fs-read-stream.js @@ -198,7 +198,7 @@ if (!common.isWindows) { const filename = `${tmpdir.path}/foo.pipe`; const mkfifoResult = child_process.spawnSync('mkfifo', [filename]); if (!mkfifoResult.error) { - child_process.exec('echo "xyz foobar" > "$FILE"', { env: { FILE: filename } }); + child_process.exec('echo "xyz foobar" > "$FILE"', { env: { ...process.env, FILE: filename } }); const stream = new fs.createReadStream(filename, common.mustNotMutateObjectDeep({ end: 1 })); stream.data = ''; diff --git a/test/parallel/test-fs-readfile-pipe-large.js b/test/parallel/test-fs-readfile-pipe-large.js index 57809d2db9a205..b78080b4e0c530 100644 --- a/test/parallel/test-fs-readfile-pipe-large.js +++ b/test/parallel/test-fs-readfile-pipe-large.js @@ -28,6 +28,7 @@ fs.writeFileSync(filename, dataExpected); const exec = require('child_process').exec; const cmd = '"$NODE" "$FILE" child < "$TMP_FILE"'; exec(cmd, { maxBuffer: 1000000, env: { + ...process.env, NODE: process.execPath, FILE: __filename, TMP_FILE: filename, diff --git a/test/parallel/test-fs-readfile-pipe.js b/test/parallel/test-fs-readfile-pipe.js index 346b1712e8bc11..a7c7359bc2e6e9 100644 --- a/test/parallel/test-fs-readfile-pipe.js +++ b/test/parallel/test-fs-readfile-pipe.js @@ -45,7 +45,7 @@ const dataExpected = fs.readFileSync(filename).toString(); const exec = require('child_process').exec; const cmd = '"$NODE" "$FILE" child < "$TMP_FILE"'; exec(cmd, { - env: { NODE: process.execPath, FILE: __filename, TMP_FILE: filename } + env: { ...process.env, NODE: process.execPath, FILE: __filename, TMP_FILE: filename } }, common.mustSucceed((stdout, stderr) => { assert.strictEqual( stdout, diff --git a/test/parallel/test-fs-readfilesync-pipe-large.js b/test/parallel/test-fs-readfilesync-pipe-large.js index a08ab6f86ffab0..1fb8c8605b297e 100644 --- a/test/parallel/test-fs-readfilesync-pipe-large.js +++ b/test/parallel/test-fs-readfilesync-pipe-large.js @@ -26,7 +26,7 @@ const exec = require('child_process').exec; const cmd = '"$NODE" "$FILE" child < "$TMP_FILE"'; exec( cmd, - { maxBuffer: 1000000, env: { NODE: process.execPath, FILE: __filename, TMP_FILE: filename } }, + { maxBuffer: 1000000, env: { ...process.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-write-sigxfsz.js b/test/parallel/test-fs-write-sigxfsz.js index 58bd38223eaaf8..084b90bd7a683f 100644 --- a/test/parallel/test-fs-write-sigxfsz.js +++ b/test/parallel/test-fs-write-sigxfsz.js @@ -21,7 +21,8 @@ if (process.argv[2] === 'child') { fs.writeFileSync(filename, '.'.repeat(1 << 16)); // Exceeds RLIMIT_FSIZE. } else { const cmd = 'ulimit -f 1 && "$NODE" "$FILE" child'; - const result = child_process.spawnSync('/bin/sh', ['-c', cmd], { env: { NODE: process.execPath, FILE: __filename } }); + const env = { ...process.env, NODE: process.execPath, FILE: __filename }; + const result = child_process.spawnSync('/bin/sh', ['-c', cmd], { env }); const haystack = result.stderr.toString(); const needle = 'Error: EFBIG: file too large, write'; const ok = haystack.includes(needle); diff --git a/test/parallel/test-http-chunk-problem.js b/test/parallel/test-http-chunk-problem.js index b87a7e0ce797da..dd50804b3fd495 100644 --- a/test/parallel/test-http-chunk-problem.js +++ b/test/parallel/test-http-chunk-problem.js @@ -44,7 +44,7 @@ let server; function executeRequest(cb) { cp.exec('"$NODE" "$FILE" request "$PORT" | "$NODE" "$FILE" shasum', - { env: { NODE: process.execPath, FILE: __filename, PORT: server.address().port } }, + { env: { ...process.env, NODE: process.execPath, FILE: __filename, PORT: server.address().port } }, (err, stdout, stderr) => { if (stderr.trim() !== '') { console.log(stderr); diff --git a/test/parallel/test-permission-allow-child-process-cli.js b/test/parallel/test-permission-allow-child-process-cli.js index 49309051ac5133..cf6382c3e144cc 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('"$NODE" --version', { env: { NODE: process.execPath } }); + childProcess.execSync('"$NODE" --version', { env: { ...process.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 6166fb8dec7e6d..8ed93ae87f6d5e 100644 --- a/test/parallel/test-pipe-head.js +++ b/test/parallel/test-pipe-head.js @@ -10,7 +10,7 @@ const script = fixtures.path('print-10-lines.js'); const cmd = '"$NODE" "$FILE" | head -2'; -exec(cmd, { env: { NODE: nodePath, FILE: script } }, common.mustSucceed((stdout, stderr) => { +exec(cmd, { env: { ...process.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 51d7c0100428db..acfcb66d4749a8 100644 --- a/test/parallel/test-preload-self-referential.js +++ b/test/parallel/test-preload-self-referential.js @@ -13,7 +13,8 @@ if (!common.isMainThread) const selfRefModule = fixtures.path('self_ref_module'); const fixtureA = fixtures.path('printA.js'); -exec('"$NODE" -r self_ref "$FIXTURE_A"', { cwd: selfRefModule, env: { NODE: nodeBinary, FIXTURE_A: fixtureA } }, +exec('"$NODE" -r self_ref "$FIXTURE_A"', + { cwd: selfRefModule, env: { ...process.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 83579bce28dcfe..a61a46890e3ecb 100644 --- a/test/parallel/test-preload-worker.js +++ b/test/parallel/test-preload-worker.js @@ -7,4 +7,6 @@ const { exec } = require('child_process'); const kNodeBinary = process.argv[0]; -exec('"$NODE" -r "$WORKER" -pe "1+1"', { env: { NODE: kNodeBinary, WORKER: worker } }, common.mustSucceed()); +exec('"$NODE" -r "$WORKER" -pe "1+1"', + { env: { ...process.env, NODE: kNodeBinary, WORKER: worker } }, + common.mustSucceed()); diff --git a/test/parallel/test-stdin-from-file-spawn.js b/test/parallel/test-stdin-from-file-spawn.js index a7ec6ea04906f4..08fec6544cb974 100644 --- a/test/parallel/test-stdin-from-file-spawn.js +++ b/test/parallel/test-stdin-from-file-spawn.js @@ -40,4 +40,4 @@ setTimeout(() => { `); execSync('"$NODE" "$JS_FILE" < "$CMD_FILE"', - { env: { NODE: process.argv0, JS_FILE: tmpJsFile, CMD_FILE: tmpCmdFile } }); + { env: { ...process.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 6dfb5fd68d32c4..bcc03a2207198c 100644 --- a/test/parallel/test-stdin-from-file.js +++ b/test/parallel/test-stdin-from-file.js @@ -32,7 +32,7 @@ console.log(`${cmd}\n\n`); fs.writeFileSync(tmpFile, string); childProcess.exec(cmd, { env: { - NODE: process.argv0, STDOUT_SCRIPT: stdoutScript, TMP_FILE: tmpFile + ...process.env, NODE: process.argv0, STDOUT_SCRIPT: stdoutScript, TMP_FILE: tmpFile, } }, common.mustCall(function(err, stdout, stderr) { fs.unlinkSync(tmpFile); diff --git a/test/parallel/test-stdio-closed.js b/test/parallel/test-stdio-closed.js index a34e7add7bc019..45fcdfa5c82f72 100644 --- a/test/parallel/test-stdio-closed.js +++ b/test/parallel/test-stdio-closed.js @@ -30,7 +30,8 @@ if (process.argv[2] === 'child') { // Run the script in a shell but close stdout and stderr. const cmd = '"$NODE" "$FILE" child 1>&- 2>&-'; -const proc = spawn('/bin/sh', ['-c', cmd], { stdio: 'inherit', env: { NODE: process.execPath, FILE: __filename } }); +const proc = spawn('/bin/sh', ['-c', cmd], + { stdio: 'inherit', env: { ...process.env, NODE: process.execPath, FILE: __filename } }); proc.on('exit', common.mustCall(function(exitCode) { assert.strictEqual(exitCode, 0); diff --git a/test/parallel/test-stdout-close-catch.js b/test/parallel/test-stdout-close-catch.js index 5af400c7d5e9fe..5ca95f170e6473 100644 --- a/test/parallel/test-stdout-close-catch.js +++ b/test/parallel/test-stdout-close-catch.js @@ -12,7 +12,7 @@ const cmd = '"$NODE" ' + '"$NODE" ' + '-pe "process.stdin.on(\'data\' , () => process.exit(1))"'; -const child = child_process.exec(cmd, { env: { NODE: process.execPath, TEST_SCRIPT: testScript } }); +const child = child_process.exec(cmd, { env: { ...process.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 d4f14d7d2406ea..68005ded413709 100644 --- a/test/parallel/test-stdout-to-file.js +++ b/test/parallel/test-stdout-to-file.js @@ -25,6 +25,7 @@ function test(size, useBuffer, cb) { console.log(`${size} chars to ${tmpFile}...`); childProcess.exec(cmd, { env: { + ...process.env, NODE: process.execPath, SCRIPT: useBuffer ? scriptBuffer : scriptString, TMP_FILE: tmpFile, diff --git a/test/parallel/test-stream-pipeline-process.js b/test/parallel/test-stream-pipeline-process.js index a57f461828fe73..47cea14dbb588a 100644 --- a/test/parallel/test-stream-pipeline-process.js +++ b/test/parallel/test-stream-pipeline-process.js @@ -14,7 +14,7 @@ if (process.argv[2] === 'child') { } else { const cp = require('child_process'); cp.exec('echo hello | "$NODE" "$FILE" child', { - env: { NODE: process.execPath, FILE: __filename } + env: { ...process.env, NODE: process.execPath, FILE: __filename } }, common.mustSucceed((stdout) => { assert.strictEqual(stdout.split(os.EOL).shift().trim(), 'hello'); })); diff --git a/test/parallel/test-tls-ecdh.js b/test/parallel/test-tls-ecdh.js index 117931e4e3512e..6aeb53338f3d33 100644 --- a/test/parallel/test-tls-ecdh.js +++ b/test/parallel/test-tls-ecdh.js @@ -52,7 +52,7 @@ server.listen(0, '127.0.0.1', common.mustCall(function() { const cmd = `"$OPENSSL" s_client -cipher ${ options.ciphers} -connect 127.0.0.1:${this.address().port}`; - exec(cmd, { env: { OPENSSL: common.opensslCli } }, common.mustSucceed((stdout, stderr) => { + exec(cmd, { env: { ...process.env, OPENSSL: common.opensslCli } }, common.mustSucceed((stdout, stderr) => { assert(stdout.includes(reply)); server.close(); })); diff --git a/test/parallel/test-worker-init-failure.js b/test/parallel/test-worker-init-failure.js index 9a61edf759511c..b0192b17ec46c2 100644 --- a/test/parallel/test-worker-init-failure.js +++ b/test/parallel/test-worker-init-failure.js @@ -49,7 +49,7 @@ if (process.argv[2] === 'child') { // Limit the number of open files, to force workers to fail. let testCmd = `ulimit -n ${OPENFILES} && `; testCmd += '"$NODE" "$FILE" child'; - const cp = child_process.exec(testCmd, { env: { NODE: process.execPath, FILE: __filename } }); + const cp = child_process.exec(testCmd, { env: { ...process.env, NODE: process.execPath, FILE: __filename } }); // Turn on the child streams for debugging purposes. let stdout = ''; diff --git a/test/sequential/test-child-process-emfile.js b/test/sequential/test-child-process-emfile.js index 7149b909739156..4179391d599a0f 100644 --- a/test/sequential/test-child-process-emfile.js +++ b/test/sequential/test-child-process-emfile.js @@ -36,7 +36,7 @@ if (ulimit > 64 || Number.isNaN(ulimit)) { const result = child_process.spawnSync( '/bin/sh', ['-c', 'ulimit -n 64 && "$NODE" "$FILENAME"'], - { env: { NODE: process.execPath, FILENAME: __filename } } + { env: { ...process.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 b935ac69f41451..ac79e54a65560e 100644 --- a/test/sequential/test-child-process-execsync.js +++ b/test/sequential/test-child-process-execsync.js @@ -38,7 +38,7 @@ if (common.isWindows) { SLEEP = 10000; } -const execOpts = { encoding: 'utf8', shell: true, env: { NODE: process.execPath } }; +const execOpts = { encoding: 'utf8', shell: true, env: { ...process.env, NODE: process.execPath } }; // Verify that stderr is not accessed when a bad shell is used assert.throws( @@ -55,7 +55,7 @@ let ret, err; const start = Date.now(); try { const cmd = `"$NODE" -e "setTimeout(function(){}, ${SLEEP});"`; - ret = execSync(cmd, { env: { NODE: process.execPath }, timeout: TIMER }); + ret = execSync(cmd, { env: { ...process.env, NODE: process.execPath }, timeout: TIMER }); } catch (e) { caught = true; assert.strictEqual(getSystemErrorName(e.errno), 'ETIMEDOUT'); @@ -81,13 +81,13 @@ const msgBuf = Buffer.from(`${msg}\n`); const cmd = `"$NODE" -e 'console.log(${JSON.stringify(msg)})'`; { - const ret = execSync(cmd, { env: { NODE: process.execPath } }); + const ret = execSync(cmd, { env: { ...process.env, NODE: process.execPath } }); assert.strictEqual(ret.length, msgBuf.length); assert.deepStrictEqual(ret, msgBuf); } { - const ret = execSync(cmd, { encoding: 'utf8', env: { NODE: process.execPath } }); + const ret = execSync(cmd, { encoding: 'utf8', env: { ...process.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 5417556fe47103..e48c2d8697f21c 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -30,7 +30,7 @@ const syntaxErrorRE = /^SyntaxError: \b/m; syntaxArgs.forEach(function(args) { const _args = args.concat('"$FILE"'); const cmd = ['"$NODE"', ..._args].join(' '); - exec(cmd, { env: { NODE: node, FILE: file } }, common.mustCall((err, stdout, stderr) => { + exec(cmd, { env: { ...process.env, NODE: node, FILE: file } }, 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 66bd15afe8135d..82eab5e77fc43b 100644 --- a/test/sequential/test-cli-syntax-file-not-found.js +++ b/test/sequential/test-cli-syntax-file-not-found.js @@ -26,7 +26,7 @@ const notFoundRE = /^Error: Cannot find module/m; syntaxArgs.forEach(function(args) { const _args = args.concat('"$FILE"'); const cmd = ['"$NODE"', ..._args].join(' '); - exec(cmd, { env: { NODE: node, FILE: file } }, common.mustCall((err, stdout, stderr) => { + exec(cmd, { env: { ...process.env, NODE: node, FILE: file } }, 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 c1812000665742..3cbfd81b2bbc22 100644 --- a/test/sequential/test-cli-syntax-good.js +++ b/test/sequential/test-cli-syntax-good.js @@ -29,7 +29,7 @@ const syntaxArgs = [ const _args = args.concat('"$FILE"'); const cmd = ['"$NODE"', ..._args].join(' '); - exec(cmd, { env: { NODE: node, FILE: file } }, common.mustCall((err, stdout, stderr) => { + exec(cmd, { env: { ...process.env, NODE: node, FILE: file } }, common.mustCall((err, stdout, stderr) => { if (err) { console.log('-- stdout --'); console.log(stdout);