child_process: improve ChildProcess validation · nodejs/node@97a7728
@@ -5,6 +5,50 @@ const assert = require('assert');
55const { ChildProcess } = require('child_process');
66assert.strictEqual(typeof ChildProcess, 'function');
778+{
9+// Verify that invalid options to spawn() throw.
10+const child = new ChildProcess();
11+12+[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
13+assert.throws(() => {
14+child.spawn(options);
15+}, /^TypeError: "options" must be an object$/);
16+});
17+}
18+19+{
20+// Verify that spawn throws if file is not a string.
21+const child = new ChildProcess();
22+23+[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
24+assert.throws(() => {
25+child.spawn({ file });
26+}, /^TypeError: "file" must be a string$/);
27+});
28+}
29+30+{
31+// Verify that spawn throws if envPairs is not an array or undefined.
32+const child = new ChildProcess();
33+34+[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
35+assert.throws(() => {
36+child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
37+}, /^TypeError: "envPairs" must be an array$/);
38+});
39+}
40+41+{
42+// Verify that spawn throws if args is not an array or undefined.
43+const child = new ChildProcess();
44+45+[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
46+assert.throws(() => {
47+child.spawn({ file: 'foo', args });
48+}, /^TypeError: "args" must be an array$/);
49+});
50+}
51+852// test that we can call spawn
953const child = new ChildProcess();
1054child.spawn({