2019-04-01 16:56:06 +08:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
const assert = require('assert');
|
|
|
|
const { fork } = require('child_process');
|
|
|
|
|
|
|
|
// This test check the arguments of `fork` method
|
|
|
|
// Refs: https://github.com/nodejs/node/issues/20749
|
|
|
|
const expectedEnv = { foo: 'bar' };
|
|
|
|
|
|
|
|
// Ensure that first argument `modulePath` must be provided
|
|
|
|
// and be of type string
|
|
|
|
{
|
|
|
|
const invalidModulePath = [
|
|
|
|
0,
|
|
|
|
true,
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
[],
|
|
|
|
{},
|
|
|
|
() => {},
|
2021-03-26 08:51:08 -07:00
|
|
|
Symbol('t'),
|
2019-04-01 16:56:06 +08:00
|
|
|
];
|
|
|
|
invalidModulePath.forEach((modulePath) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => fork(modulePath), {
|
2019-04-01 16:56:06 +08:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2019-04-01 16:56:06 +08:00
|
|
|
message: /^The "modulePath" argument must be of type string/
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const cp = fork(fixtures.path('child-process-echo-options.js'));
|
|
|
|
cp.on(
|
|
|
|
'exit',
|
|
|
|
common.mustCall((code) => {
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the second argument of `fork`
|
|
|
|
// and `fork` should parse options
|
|
|
|
// correctly if args is undefined or null
|
|
|
|
{
|
|
|
|
const invalidSecondArgs = [
|
|
|
|
0,
|
|
|
|
true,
|
|
|
|
() => {},
|
2021-03-26 08:51:08 -07:00
|
|
|
Symbol('t'),
|
2019-04-01 16:56:06 +08:00
|
|
|
];
|
|
|
|
invalidSecondArgs.forEach((arg) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2019-04-01 16:56:06 +08:00
|
|
|
() => {
|
|
|
|
fork(fixtures.path('child-process-echo-options.js'), arg);
|
|
|
|
},
|
|
|
|
{
|
2021-12-23 23:02:13 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2019-04-01 16:56:06 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const argsLists = [undefined, null, []];
|
|
|
|
|
|
|
|
argsLists.forEach((args) => {
|
|
|
|
const cp = fork(fixtures.path('child-process-echo-options.js'), args, {
|
2019-11-12 15:34:47 +00:00
|
|
|
env: { ...process.env, ...expectedEnv }
|
2019-04-01 16:56:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
cp.on(
|
|
|
|
'message',
|
|
|
|
common.mustCall(({ env }) => {
|
|
|
|
assert.strictEqual(env.foo, expectedEnv.foo);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
cp.on(
|
|
|
|
'exit',
|
|
|
|
common.mustCall((code) => {
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the third argument should be type of object if provided
|
|
|
|
{
|
|
|
|
const invalidThirdArgs = [
|
|
|
|
0,
|
|
|
|
true,
|
|
|
|
() => {},
|
2021-03-26 08:51:08 -07:00
|
|
|
Symbol('t'),
|
2019-04-01 16:56:06 +08:00
|
|
|
];
|
|
|
|
invalidThirdArgs.forEach((arg) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2019-04-01 16:56:06 +08:00
|
|
|
() => {
|
|
|
|
fork(fixtures.path('child-process-echo-options.js'), [], arg);
|
|
|
|
},
|
|
|
|
{
|
2021-12-23 23:02:13 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2019-04-01 16:56:06 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|