2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-12-31 00:38:06 +01:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2017-10-07 06:06:18 +02:00
|
|
|
const { execFileSync, execSync, spawnSync } = require('child_process');
|
2019-06-09 15:04:51 +02:00
|
|
|
const { getSystemErrorName } = require('util');
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const TIMER = 200;
|
|
|
|
const SLEEP = 2000;
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2018-01-25 20:47:52 +01:00
|
|
|
const execOpts = { encoding: 'utf8', shell: true };
|
2015-10-13 06:26:50 +02:00
|
|
|
|
2016-05-19 16:13:20 +02:00
|
|
|
// Verify that stderr is not accessed when a bad shell is used
|
|
|
|
assert.throws(
|
2017-07-11 02:55:21 +02:00
|
|
|
function() { execSync('exit -1', { shell: 'bad_shell' }); },
|
2018-07-27 14:36:28 +02:00
|
|
|
/spawnSync bad_shell ENOENT/
|
2016-05-19 16:13:20 +02:00
|
|
|
);
|
|
|
|
assert.throws(
|
2017-07-11 02:55:21 +02:00
|
|
|
function() { execFileSync('exit -1', { shell: 'bad_shell' }); },
|
2018-07-27 14:36:28 +02:00
|
|
|
/spawnSync bad_shell ENOENT/
|
2016-05-19 16:13:20 +02:00
|
|
|
);
|
|
|
|
|
2018-12-26 13:14:29 +01:00
|
|
|
let caught = false;
|
|
|
|
let ret, err;
|
|
|
|
const start = Date.now();
|
2016-01-13 21:42:45 +01:00
|
|
|
try {
|
2018-12-26 13:14:29 +01:00
|
|
|
const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
|
2017-07-11 02:55:21 +02:00
|
|
|
ret = execSync(cmd, { timeout: TIMER });
|
2014-02-10 21:40:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
caught = true;
|
2019-06-09 15:04:51 +02:00
|
|
|
assert.strictEqual(getSystemErrorName(e.errno), 'ETIMEDOUT');
|
2014-02-10 21:40:48 +01:00
|
|
|
err = e;
|
|
|
|
} finally {
|
2017-10-06 19:38:56 +02:00
|
|
|
assert.strictEqual(ret, undefined,
|
|
|
|
`should not have a return value, received ${ret}`);
|
2018-07-27 14:36:28 +02:00
|
|
|
assert.ok(caught, 'execSync should throw');
|
2017-01-08 14:19:00 +01:00
|
|
|
const end = Date.now() - start;
|
2014-02-10 21:40:48 +01:00
|
|
|
assert(end < SLEEP);
|
|
|
|
assert(err.status > 128 || err.signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
execSync('iamabadcommand');
|
|
|
|
}, /Command failed: iamabadcommand/);
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const msg = 'foobar';
|
2017-04-28 03:06:42 +02:00
|
|
|
const msgBuf = Buffer.from(`${msg}\n`);
|
2014-02-10 21:40:48 +01:00
|
|
|
|
|
|
|
// console.log ends every line with just '\n', even on Windows.
|
2015-10-13 06:26:50 +02:00
|
|
|
|
2018-12-26 13:14:29 +01:00
|
|
|
const cmd = `"${process.execPath}" -e "console.log('${msg}');"`;
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2018-12-26 13:14:29 +01:00
|
|
|
{
|
|
|
|
const ret = execSync(cmd);
|
|
|
|
assert.strictEqual(ret.length, msgBuf.length);
|
|
|
|
assert.deepStrictEqual(ret, msgBuf);
|
|
|
|
}
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2018-12-26 13:14:29 +01:00
|
|
|
{
|
|
|
|
const ret = execSync(cmd, { encoding: 'utf8' });
|
|
|
|
assert.strictEqual(ret, `${msg}\n`);
|
|
|
|
}
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const args = [
|
2014-02-10 21:40:48 +01:00
|
|
|
'-e',
|
2015-10-13 06:26:50 +02:00
|
|
|
`console.log("${msg}");`
|
2014-02-10 21:40:48 +01:00
|
|
|
];
|
2018-12-26 13:14:29 +01:00
|
|
|
{
|
|
|
|
const ret = execFileSync(process.execPath, args);
|
|
|
|
assert.deepStrictEqual(ret, msgBuf);
|
|
|
|
}
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2018-12-26 13:14:29 +01:00
|
|
|
{
|
|
|
|
const ret = execFileSync(process.execPath, args, { encoding: 'utf8' });
|
|
|
|
assert.strictEqual(ret, `${msg}\n`);
|
|
|
|
}
|
2014-06-25 05:18:00 +02:00
|
|
|
|
2017-12-31 17:39:30 +01:00
|
|
|
// Verify that the cwd option works.
|
|
|
|
// See https://github.com/nodejs/node-v0.x-archive/issues/7824.
|
2016-07-12 18:17:28 +02:00
|
|
|
{
|
|
|
|
const cwd = common.rootDir;
|
|
|
|
const cmd = common.isWindows ? 'echo %cd%' : 'pwd';
|
2017-07-11 02:55:21 +02:00
|
|
|
const response = execSync(cmd, { cwd });
|
2014-06-25 05:18:00 +02:00
|
|
|
|
|
|
|
assert.strictEqual(response.toString().trim(), cwd);
|
2016-07-12 18:17:28 +02:00
|
|
|
}
|
2014-07-20 04:35:07 +02:00
|
|
|
|
2017-12-31 17:39:30 +01:00
|
|
|
// Verify that stderr is not accessed when stdio = 'ignore'.
|
|
|
|
// See https://github.com/nodejs/node-v0.x-archive/issues/7966.
|
2016-07-13 01:47:32 +02:00
|
|
|
{
|
2014-07-20 04:35:07 +02:00
|
|
|
assert.throws(function() {
|
2017-07-11 02:55:21 +02:00
|
|
|
execSync('exit -1', { stdio: 'ignore' });
|
2014-07-20 04:35:07 +02:00
|
|
|
}, /Command failed: exit -1/);
|
2016-07-13 01:47:32 +02:00
|
|
|
}
|
2016-10-20 19:45:38 +02:00
|
|
|
|
|
|
|
// Verify the execFileSync() behavior when the child exits with a non-zero code.
|
|
|
|
{
|
|
|
|
const args = ['-e', 'process.exit(1)'];
|
2017-10-07 06:06:18 +02:00
|
|
|
const spawnSyncResult = spawnSync(process.execPath, args);
|
|
|
|
const spawnSyncKeys = Object.keys(spawnSyncResult).sort();
|
|
|
|
assert.deepStrictEqual(spawnSyncKeys, [
|
|
|
|
'output',
|
|
|
|
'pid',
|
|
|
|
'signal',
|
|
|
|
'status',
|
|
|
|
'stderr',
|
|
|
|
'stdout'
|
|
|
|
]);
|
2016-10-20 19:45:38 +02:00
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
execFileSync(process.execPath, args);
|
|
|
|
}, (err) => {
|
|
|
|
const msg = `Command failed: ${process.execPath} ${args.join(' ')}`;
|
|
|
|
|
|
|
|
assert(err instanceof Error);
|
2016-10-28 20:02:24 +02:00
|
|
|
assert.strictEqual(err.message, msg);
|
2016-10-20 19:45:38 +02:00
|
|
|
assert.strictEqual(err.status, 1);
|
2017-10-07 06:06:18 +02:00
|
|
|
assert.strictEqual(typeof err.pid, 'number');
|
2018-12-26 13:14:29 +01:00
|
|
|
spawnSyncKeys
|
|
|
|
.filter((key) => key !== 'pid')
|
|
|
|
.forEach((key) => {
|
|
|
|
assert.deepStrictEqual(err[key], spawnSyncResult[key]);
|
|
|
|
});
|
2016-10-20 19:45:38 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2018-01-25 20:47:52 +01:00
|
|
|
|
|
|
|
// Verify the shell option works properly
|
2018-02-09 02:32:04 +01:00
|
|
|
execFileSync(process.execPath, [], execOpts);
|