2015-05-22 09:52:05 -05:00
|
|
|
'use strict';
|
|
|
|
|
2017-02-10 12:08:49 -08:00
|
|
|
const common = require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
2017-04-11 16:01:00 -04:00
|
|
|
const { ChildProcess } = require('child_process');
|
2016-12-01 11:22:07 -06:00
|
|
|
assert.strictEqual(typeof ChildProcess, 'function');
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2017-04-11 16:37:07 -04:00
|
|
|
{
|
|
|
|
// Verify that invalid options to spawn() throw.
|
|
|
|
const child = new ChildProcess();
|
|
|
|
|
|
|
|
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-04-11 16:37:07 -04:00
|
|
|
child.spawn(options);
|
2017-06-20 23:20:10 +02:00
|
|
|
}, {
|
2017-06-30 20:46:11 +02:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2019-09-23 08:17:25 +02:00
|
|
|
message: 'The "options" argument must be of type object.' +
|
|
|
|
`${common.invalidArgTypeHelper(options)}`
|
2017-06-20 23:20:10 +02:00
|
|
|
});
|
2017-04-11 16:37:07 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Verify that spawn throws if file is not a string.
|
|
|
|
const child = new ChildProcess();
|
|
|
|
|
|
|
|
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-04-11 16:37:07 -04:00
|
|
|
child.spawn({ file });
|
2017-06-20 23:20:10 +02:00
|
|
|
}, {
|
2017-06-30 20:46:11 +02:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2019-09-23 08:17:25 +02:00
|
|
|
message: 'The "options.file" property must be of type string.' +
|
|
|
|
`${common.invalidArgTypeHelper(file)}`
|
2017-06-20 23:20:10 +02:00
|
|
|
});
|
2017-04-11 16:37:07 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Verify that spawn throws if envPairs is not an array or undefined.
|
|
|
|
const child = new ChildProcess();
|
|
|
|
|
|
|
|
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-04-11 16:37:07 -04:00
|
|
|
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
|
2017-06-20 23:20:10 +02:00
|
|
|
}, {
|
2017-06-30 20:46:11 +02:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2019-09-23 08:17:25 +02:00
|
|
|
message: 'The "options.envPairs" property must be an instance of Array.' +
|
|
|
|
common.invalidArgTypeHelper(envPairs)
|
2017-06-20 23:20:10 +02:00
|
|
|
});
|
2017-04-11 16:37:07 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Verify that spawn throws if args is not an array or undefined.
|
|
|
|
const child = new ChildProcess();
|
|
|
|
|
|
|
|
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-04-11 16:37:07 -04:00
|
|
|
child.spawn({ file: 'foo', args });
|
2017-06-20 23:20:10 +02:00
|
|
|
}, {
|
2017-06-30 20:46:11 +02:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2019-09-23 08:17:25 +02:00
|
|
|
message: 'The "options.args" property must be an instance of Array.' +
|
|
|
|
common.invalidArgTypeHelper(args)
|
2017-06-20 23:20:10 +02:00
|
|
|
});
|
2017-04-11 16:37:07 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Test that we can call spawn
|
2017-01-08 13:19:00 +00:00
|
|
|
const child = new ChildProcess();
|
2015-05-22 09:52:05 -05:00
|
|
|
child.spawn({
|
|
|
|
file: process.execPath,
|
|
|
|
args: ['--interactive'],
|
|
|
|
cwd: process.cwd(),
|
|
|
|
stdio: 'pipe'
|
|
|
|
});
|
|
|
|
|
2022-01-24 23:03:17 -08:00
|
|
|
assert.strictEqual(Object.hasOwn(child, 'pid'), true);
|
2017-04-11 16:01:00 -04:00
|
|
|
assert(Number.isInteger(child.pid));
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Try killing with invalid signal
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2017-06-20 23:20:10 +02:00
|
|
|
() => { child.kill('foo'); },
|
2019-12-25 18:02:16 +01:00
|
|
|
{ code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' }
|
2017-06-20 23:20:10 +02:00
|
|
|
);
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2016-12-01 11:22:07 -06:00
|
|
|
assert.strictEqual(child.kill(), true);
|