Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const child = new ChildProcess();
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
common.expectsError(() => {
child.spawn({ file: 'foo', args });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.args" property must be of type Array. ' +
`Received type ${typeName(args)}`
});
});
}
// Test that we can call spawn
const child = new ChildProcess();
child.spawn({
file: process.execPath,
args: ['--interactive'],
cwd: process.cwd(),
stdio: 'pipe'
});
assert.strictEqual(child.hasOwnProperty('pid'), true);
assert(Number.isInteger(child.pid));
// Try killing with invalid signal
common.expectsError(
() => { child.kill('foo'); },
{ code: 'ERR_UNKNOWN_SIGNAL', type: TypeError }
);
'use strict';
const common = require('../common');
const assert = require('assert');
const { ChildProcess } = require('child_process');
assert.strictEqual(typeof ChildProcess, 'function');
function typeName(value) {
return typeof value;
}
{
// Verify that invalid options to spawn() throw.
const child = new ChildProcess();
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
common.expectsError(() => {
child.spawn(options);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type Object. ' +
`Received type ${typeName(options)}`
});
});
}
{
// Verify that spawn throws if file is not a string.
const child = new ChildProcess();
const execa = (file, args, options) => {
const parsed = handleArgs(file, args, options);
const command = joinCommand(file, args);
let spawned;
try {
spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);
} catch (error) {
// Ensure the returned error is always both a promise and a child process
const dummySpawned = new childProcess.ChildProcess();
const errorPromise = Promise.reject(makeError({
error,
stdout: '',
stderr: '',
all: '',
command,
parsed,
timedOut: false,
isCanceled: false,
killed: false
}));
return mergePromise(dummySpawned, errorPromise);
}
const spawnedPromise = getSpawnedPromise(spawned);
const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
const execa = (file, args, options) => {
const parsed = handleArgs(file, args, options);
const command = joinCommand(file, args);
let spawned;
try {
spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);
} catch (error) {
// Ensure the returned error is always both a promise and a child process
const dummySpawned = new childProcess.ChildProcess();
const errorPromise = Promise.reject(makeError({
error,
stdout: '',
stderr: '',
all: '',
command,
parsed,
timedOut: false,
isCanceled: false,
killed: false
}));
return mergePromise(dummySpawned, errorPromise);
}
const spawnedPromise = getSpawnedPromise(spawned);
const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
beforeEach(function() {
sandbox = sinon.createSandbox();
child = new ChildProcess();
child.stdout = new Emitter();
child.stderr = new Emitter();
compile = sandbox.stub(ConsoleAgent.prototype, 'compile').returns(
Promise.resolve(child)
);
sandbox.stub(ConsoleAgent.prototype, 'createChildProcess').returns(
Promise.resolve(child)
);
sandbox.stub(fs, 'writeFile').returns(
Promise.resolve(child)
);
sandbox.stub(fs, 'stat').returns(true);
});