test: do not assume process.execPath contains no spaces

We had a bunch of tests that would fail if run from an executable that
contains any char that should be escaped when run from a shell.

PR-URL: https://github.com/nodejs/node/pull/55028
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Antoine du Hamel
2024-09-22 15:03:30 +02:00
committed by GitHub
parent 7ecc48d061
commit 1fcb128771
16 changed files with 151 additions and 88 deletions

View File

@@ -38,8 +38,6 @@ if (common.isWindows) {
SLEEP = 10000;
}
const execOpts = { encoding: 'utf8', shell: true };
// Verify that stderr is not accessed when a bad shell is used
assert.throws(
function() { execSync('exit -1', { shell: 'bad_shell' }); },
@@ -54,8 +52,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 = `"${common.isWindows ? process.execPath : '$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 +76,17 @@ 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 = `"${common.isWindows ? process.execPath : '$NODE'}" -e "console.log('${msg}');"`;
const env = common.isWindows ? process.env : { ...process.env, NODE: process.execPath };
{
const ret = execSync(cmd);
const ret = execSync(cmd, common.isWindows ? undefined : { env });
assert.strictEqual(ret.length, msgBuf.length);
assert.deepStrictEqual(ret, msgBuf);
}
{
const ret = execSync(cmd, { encoding: 'utf8' });
const ret = execSync(cmd, { encoding: 'utf8', env });
assert.strictEqual(ret, `${msg}\n`);
}
@@ -156,4 +155,6 @@ const args = [
}
// Verify the shell option works properly
execFileSync(process.execPath, [], execOpts);
execFileSync(`"${common.isWindows ? process.execPath : '$NODE'}"`, [], {
encoding: 'utf8', shell: true, env
});

View File

@@ -5,12 +5,19 @@ const assert = require('assert');
const { exec } = require('child_process');
const fixtures = require('../common/fixtures');
const node = process.execPath;
// The execPath might contain chars that should be escaped in a shell context.
// On non-Windows, we can pass the path via the env; `"` is not a valid char on
// Windows, so we can simply pass the path.
const execNode = (flag, file, callback) => exec(
`"${common.isWindows ? process.execPath : '$NODE'}" ${flag} "${common.isWindows ? file : '$FILE'}"`,
common.isWindows ? undefined : { env: { ...process.env, NODE: process.execPath, FILE: file } },
callback,
);
// Test both sets of arguments that check syntax
const syntaxArgs = [
['-c'],
['--check'],
'-c',
'--check',
];
const notFoundRE = /^Error: Cannot find module/m;
@@ -23,10 +30,8 @@ const notFoundRE = /^Error: Cannot find module/m;
file = fixtures.path(file);
// 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) => {
syntaxArgs.forEach(function(flag) {
execNode(flag, file, common.mustCall((err, stdout, stderr) => {
// No stdout should be produced
assert.strictEqual(stdout, '');

View File

@@ -5,12 +5,19 @@ const assert = require('assert');
const { exec } = require('child_process');
const fixtures = require('../common/fixtures');
const node = process.execPath;
// The execPath might contain chars that should be escaped in a shell context.
// On non-Windows, we can pass the path via the env; `"` is not a valid char on
// Windows, so we can simply pass the path.
const execNode = (flag, file, callback) => exec(
`"${common.isWindows ? process.execPath : '$NODE'}" ${flag} "${common.isWindows ? file : '$FILE'}"`,
common.isWindows ? undefined : { env: { ...process.env, NODE: process.execPath, FILE: file } },
callback,
);
// Test both sets of arguments that check syntax
const syntaxArgs = [
['-c'],
['--check'],
'-c',
'--check',
];
// Test good syntax with and without shebang
@@ -25,11 +32,8 @@ const syntaxArgs = [
file = fixtures.path(file);
// 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) => {
syntaxArgs.forEach(function(flag) {
execNode(flag, file, common.mustCall((err, stdout, stderr) => {
if (err) {
console.log('-- stdout --');
console.log(stdout);