Skip to content

Commit 175dce6

Browse files
committed
fixup! test: add escapePathsFromShellCommand util
1 parent 1aa18a5 commit 175dce6

38 files changed

+180
-290
lines changed

test/abort/test-abort-fatal-error.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ const assert = require('assert');
2828
const exec = require('child_process').exec;
2929

3030
const cmdline =
31-
'ulimit -c 0; "$NODE" --max-old-space-size=16 --max-semi-space-size=4' +
32-
' -e "a = []; for (i = 0; i < 1e9; i++) { a.push({}) }"';
31+
common.escapePOSIXShell`ulimit -c 0; "${
32+
process.execPath
33+
}" --max-old-space-size=16 --max-semi-space-size=4 -e "a = []; for (i = 0; i < 1e9; i++) { a.push({}) }"`;
3334

34-
exec(cmdline, { env: { NODE: process.execPath } }, common.mustCall((err, stdout, stderr) => {
35+
exec(...cmdline, common.mustCall((err, stdout, stderr) => {
3536
if (err?.code !== 134 && err?.signal !== 'SIGABRT') {
3637
console.log({ err, stdout, stderr });
3738
assert.fail(err?.message);

test/async-hooks/test-callback-error.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ assert.ok(!arg);
6262
let program = process.execPath;
6363
let args = [
6464
'--abort-on-uncaught-exception', __filename, 'test_callback_abort' ];
65-
const options = { encoding: 'utf8' };
65+
let options = { encoding: 'utf8' };
6666
if (!common.isWindows) {
67-
program = `ulimit -c 0 && exec "$NODE" ${args[0]} "$FILE" ${args[2]}`;
67+
[program, options] = common.escapePOSIXShell`ulimit -c 0 && exec "${program}" ${args[0]} "${args[1]}" ${args[2]}`;
6868
args = [];
6969
options.shell = true;
70-
options.env = { NODE: process.execPath, FILE: __filename };
70+
options.encoding = 'utf8';
7171
}
7272
const child = spawnSync(program, args, options);
7373
if (common.isWindows) {

test/common/index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,12 @@ const PIPE = (() => {
249249
// `$node --abort-on-uncaught-exception $file child`
250250
// the process aborts.
251251
function childShouldThrowAndAbort() {
252-
let testCmd = '';
253-
if (!isWindows) {
254-
// Do not create core files, as it can take a lot of disk space on
255-
// continuous testing and developers' machines
256-
testCmd += 'ulimit -c 0 && ';
257-
}
258-
testCmd += '"$NODE" --abort-on-uncaught-exception ';
259-
testCmd += '"$FILE" child';
260-
const child = exec(testCmd, { env: { NODE: process.argv[0], FILE: process.argv[1] } });
252+
// Do not create core files, as it can take a lot of disk space on
253+
// continuous testing and developers' machines
254+
const uclimit = isWindows ?
255+
'' :
256+
'ulimit -c 0 && ';
257+
const child = exec(...escapePOSIXShell`${uclimit}"${process.argv[0]}" --abort-on-uncaught-exception "${process.argv[1]}" child`);
261258
child.on('exit', function onExit(exitCode, signal) {
262259
const errMsg = 'Test should have aborted ' +
263260
`but instead exited with exit code ${exitCode}` +

test/fixtures/snapshot/child-process-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
function spawn() {
99
const { spawnSync, execFileSync, execSync } = require('child_process');
1010
spawnSync(process.execPath, [ __filename, 'spawnSync' ], { stdio: 'inherit' });
11-
execSync(`"$NODE" "$FILE" "execSync"`, { stdio: 'inherit', env: { NODE: process.execPath, FILE: __filename } });
11+
execSync(`"$NODE" "$FILE" "execSync"`, { stdio: 'inherit', env: { ...process.env, NODE: process.execPath, FILE: __filename } });
1212
execFileSync(process.execPath, [ __filename, 'execFileSync' ], { stdio: 'inherit' });
1313
}
1414

test/parallel/test-child-process-bad-stdio.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ ChildProcess.prototype.spawn = function() {
2727
};
2828

2929
function createChild(options, callback) {
30-
const cmd = '"$NODE" "$FILE" child';
31-
options = { ...options, env: { ...options.env, NODE: process.execPath, FILE: __filename } };
30+
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
31+
options = { ...options, env: { ...opts.env, ...options.env } };
3232

3333
return cp.exec(cmd, options, common.mustCall(callback));
3434
}

test/parallel/test-child-process-exec-encoding.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ if (process.argv[2] === 'child') {
1313
const expectedStdout = `${stdoutData}\n`;
1414
const expectedStderr = `${stderrData}\n`;
1515
function run(options, callback) {
16-
const cmd = '"$NODE" "$FILE" child';
17-
options = { ...options, env: { ...options.env, NODE: process.execPath, FILE: __filename } };
16+
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
17+
options = { ...options, env: { ...opts.env, ...options.env } };
1818

1919
cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => {
2020
callback(stdout, stderr);

test/parallel/test-child-process-exec-std-encoding.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ if (process.argv[2] === 'child') {
1212
console.log(stdoutData);
1313
console.error(stderrData);
1414
} else {
15-
const cmd = '"$NODE" "$FILE" child';
16-
const env = { NODE: process.execPath, FILE: __filename };
17-
const child = cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => {
15+
const child = cp.exec(...common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`, common.mustSucceed((stdout, stderr) => {
1816
assert.strictEqual(stdout, expectedStdout);
1917
assert.strictEqual(stderr, expectedStderr);
2018
}));

test/parallel/test-child-process-exec-timeout-expire.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ if (process.argv[2] === 'child') {
1818
return;
1919
}
2020

21-
const cmd = '"$NODE" "$FILE" child';
21+
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
2222

2323
cp.exec(cmd, {
24-
env: { NODE: process.execPath, FILE: __filename },
24+
...opts,
2525
timeout: kExpiringParentTimer,
2626
}, common.mustCall((err, stdout, stderr) => {
2727
console.log('[stdout]', stdout.trim());

test/parallel/test-child-process-exec-timeout-kill.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ if (process.argv[2] === 'child') {
1818
return;
1919
}
2020

21-
const cmd = '"$NODE" "$FILE" child';
21+
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
2222

2323
// Test with a different kill signal.
2424
cp.exec(cmd, {
25-
env: { NODE: process.execPath, FILE: __filename },
25+
...opts,
2626
timeout: kExpiringParentTimer,
2727
killSignal: 'SIGKILL'
2828
}, common.mustCall((err, stdout, stderr) => {

test/parallel/test-child-process-exec-timeout-not-expired.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ if (process.argv[2] === 'child') {
2222
return;
2323
}
2424

25-
const cmd = '"$NODE" "$FILE" child';
25+
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
2626

2727
cp.exec(cmd, {
28-
env: { NODE: process.execPath, FILE: __filename },
29-
timeout: kTimeoutNotSupposedToExpire
28+
...opts,
29+
timeout: kTimeoutNotSupposedToExpire,
3030
}, common.mustSucceed((stdout, stderr) => {
3131
assert.strictEqual(stdout.trim(), 'child stdout');
3232
assert.strictEqual(stderr.trim(), 'child stderr');

0 commit comments

Comments
 (0)