2016-01-08 16:17:50 -05:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const cp = require('child_process');
|
|
|
|
|
|
|
|
// Verify that a shell is, in fact, executed
|
2017-07-10 20:55:21 -04:00
|
|
|
const doesNotExist = cp.spawn('does-not-exist', { shell: true });
|
2016-01-08 16:17:50 -05:00
|
|
|
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.notStrictEqual(doesNotExist.spawnfile, 'does-not-exist');
|
2017-02-03 14:54:19 -05:00
|
|
|
doesNotExist.on('error', common.mustNotCall());
|
2016-01-08 16:17:50 -05:00
|
|
|
doesNotExist.on('exit', common.mustCall((code, signal) => {
|
|
|
|
assert.strictEqual(signal, null);
|
|
|
|
|
|
|
|
if (common.isWindows)
|
|
|
|
assert.strictEqual(code, 1); // Exit code of cmd.exe
|
|
|
|
else
|
|
|
|
assert.strictEqual(code, 127); // Exit code of /bin/sh
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Verify that passing arguments works
|
2025-03-21 09:15:18 -07:00
|
|
|
common.expectWarning(
|
|
|
|
'DeprecationWarning',
|
|
|
|
'Passing args to a child process with shell option true can lead to security ' +
|
|
|
|
'vulnerabilities, as the arguments are not escaped, only concatenated.',
|
|
|
|
'DEP0190');
|
|
|
|
|
2016-01-08 16:17:50 -05:00
|
|
|
const echo = cp.spawn('echo', ['foo'], {
|
|
|
|
encoding: 'utf8',
|
|
|
|
shell: true
|
|
|
|
});
|
|
|
|
let echoOutput = '';
|
|
|
|
|
|
|
|
assert.strictEqual(echo.spawnargs[echo.spawnargs.length - 1].replace(/"/g, ''),
|
|
|
|
'echo foo');
|
2016-01-28 10:21:57 -05:00
|
|
|
echo.stdout.on('data', (data) => {
|
2016-01-08 16:17:50 -05:00
|
|
|
echoOutput += data;
|
|
|
|
});
|
|
|
|
echo.on('close', common.mustCall((code, signal) => {
|
|
|
|
assert.strictEqual(echoOutput.trim(), 'foo');
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Verify that shell features can be used
|
2017-03-21 02:58:54 +02:00
|
|
|
const cmd = 'echo bar | cat';
|
2016-01-08 16:17:50 -05:00
|
|
|
const command = cp.spawn(cmd, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
shell: true
|
|
|
|
});
|
|
|
|
let commandOutput = '';
|
|
|
|
|
2016-01-28 10:21:57 -05:00
|
|
|
command.stdout.on('data', (data) => {
|
2016-01-08 16:17:50 -05:00
|
|
|
commandOutput += data;
|
|
|
|
});
|
|
|
|
command.on('close', common.mustCall((code, signal) => {
|
|
|
|
assert.strictEqual(commandOutput.trim(), 'bar');
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Verify that the environment is properly inherited
|
2024-09-22 15:03:30 +02:00
|
|
|
const env = cp.spawn(`"${common.isWindows ? process.execPath : '$NODE'}" -pe process.env.BAZ`, {
|
|
|
|
env: { ...process.env, BAZ: 'buzz', NODE: process.execPath },
|
2016-01-08 16:17:50 -05:00
|
|
|
encoding: 'utf8',
|
|
|
|
shell: true
|
|
|
|
});
|
|
|
|
let envOutput = '';
|
|
|
|
|
2016-01-28 10:21:57 -05:00
|
|
|
env.stdout.on('data', (data) => {
|
2016-01-08 16:17:50 -05:00
|
|
|
envOutput += data;
|
|
|
|
});
|
|
|
|
env.on('close', common.mustCall((code, signal) => {
|
|
|
|
assert.strictEqual(envOutput.trim(), 'buzz');
|
|
|
|
}));
|