diff --git a/test/abort/test-abort-fatal-error.js b/test/abort/test-abort-fatal-error.js index b723b5b920849b..300967749ac79b 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: { ...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); } - - 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..06453cd995b3cb 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 = { ...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 c10dea59319264..6e3338064d00d7 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: { ...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}` + 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..08caead1bf425b 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 = { ...process.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..0dad5a917d507f 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 = { ...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 08e54544836d98..de5dadd69b00d5 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: { ...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 845fd1eaece24d..7a179f36d4ca96 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: { ...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 fb0af5fa8f59d5..793b504036dcd0 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: { ...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 e6e04ff61f93b8..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 }; +const execOpts = { encoding: 'utf8', shell: true, env: { ...process.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..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(`"${process.execPath}" ${args.join(' ')}`, { 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'); @@ -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: { ...process.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: { ...process.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: { ...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 bc623130994a2e..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(`${process.execPath} -p 42`); + 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(`${process.execPath} -e "${failingCodeWithStdoutErr}"`) + 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-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..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 nodejs = `"${process.execPath}"`; +const env = { ...process.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: { ...process.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: { ...process.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: { ...process.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: { ...process.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..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(`${process.execPath} --help`, 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 31fe2d3449d824..9d957e7cfe8fdf 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: { ...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 74c0ff53eb18b7..09a166870d8030 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -622,12 +622,18 @@ 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: { ...process.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..a4d4f463ba4785 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: { ...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 a2afebd838f410..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,6 +13,7 @@ const domain = require('domain'); const uncaughtExceptionHandlerErrMsg = 'boom from uncaughtException handler'; const domainErrMsg = 'boom from domain'; +const env = { ...process.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..c41ea081675b56 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: { ...process.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..2de9b74a9608a1 100644 --- a/test/parallel/test-error-reporting.js +++ b/test/parallel/test-error-reporting.js @@ -28,8 +28,9 @@ 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"`; + 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 a038eac1efdfa1..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" > '${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-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..b78080b4e0c530 100644 --- a/test/parallel/test-fs-readfile-pipe-large.js +++ b/test/parallel/test-fs-readfile-pipe-large.js @@ -26,10 +26,13 @@ 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: { + ...process.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..a7c7359bc2e6e9 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: { ...process.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..1fb8c8605b297e 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: { ...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 323312fcb943dc..084b90bd7a683f 100644 --- a/test/parallel/test-fs-write-sigxfsz.js +++ b/test/parallel/test-fs-write-sigxfsz.js @@ -20,8 +20,9 @@ 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 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 ebfd940687947e..dd50804b3fd495 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: { ...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-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..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(process.execPath, ['--version']); + 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 1e79249c290500..8ed93ae87f6d5e 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: { ...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 2624527deb3984..acfcb66d4749a8 100644 --- a/test/parallel/test-preload-self-referential.js +++ b/test/parallel/test-preload-self-referential.js @@ -13,8 +13,8 @@ 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: { ...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 3e9134b470cf37..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(`"${kNodeBinary}" -r "${worker}" -pe "1+1"`, 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 12c235fcfeb081..08fec6544cb974 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: { ...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 f4fcb32edbfde5..bcc03a2207198c 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: { + ...process.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..45fcdfa5c82f72 100644 --- a/test/parallel/test-stdio-closed.js +++ b/test/parallel/test-stdio-closed.js @@ -29,8 +29,9 @@ 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: { ...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 924b52715a54f3..5ca95f170e6473 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: { ...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 d66f382af5c1f8..68005ded413709 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,12 @@ function test(size, useBuffer, cb) { console.log(`${size} chars to ${tmpFile}...`); - childProcess.exec(cmd, common.mustSucceed(() => { + childProcess.exec(cmd, { env: { + ...process.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..47cea14dbb588a 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: { ...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 8c879f850c9b8d..6aeb53338f3d33 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: { ...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 078329ee68874f..b0192b17ec46c2 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: { ...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 8091e54a5c5bde..4179391d599a0f 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: { ...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 75acbc34a902bd..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 }; +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( @@ -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: { ...process.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: { ...process.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: { ...process.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..e48c2d8697f21c 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: { ...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 61f84d8e71624d..82eab5e77fc43b 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: { ...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 44051c7a4a3617..3cbfd81b2bbc22 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: { ...process.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!`); }));