child_process: check execFile and fork args
[platform/upstream/nodejs.git] / test / parallel / test-child-process-spawn-typeerror.js
1 'use strict';
2 const assert = require('assert');
3 const child_process = require('child_process');
4 const spawn = child_process.spawn;
5 const fork = child_process.fork;
6 const execFile = child_process.execFile;
7 const common = require('../common');
8 const cmd = common.isWindows ? 'rundll32' : 'ls';
9 const invalidcmd = 'hopefully_you_dont_have_this_on_your_machine';
10 const invalidArgsMsg = /Incorrect value of args option/;
11 const invalidOptionsMsg = /options argument must be an object/;
12 const empty = common.fixturesDir + '/empty.js';
13
14 assert.throws(function() {
15   var child = spawn(invalidcmd, 'this is not an array');
16   child.on('error', assert.fail);
17 }, TypeError);
18
19 // verify that valid argument combinations do not throw
20 assert.doesNotThrow(function() {
21   spawn(cmd);
22 });
23
24 assert.doesNotThrow(function() {
25   spawn(cmd, []);
26 });
27
28 assert.doesNotThrow(function() {
29   spawn(cmd, {});
30 });
31
32 assert.doesNotThrow(function() {
33   spawn(cmd, [], {});
34 });
35
36 // verify that invalid argument combinations throw
37 assert.throws(function() {
38   spawn();
39 }, /Bad argument/);
40
41 assert.throws(function() {
42   spawn(cmd, null);
43 }, invalidArgsMsg);
44
45 assert.throws(function() {
46   spawn(cmd, true);
47 }, invalidArgsMsg);
48
49 assert.throws(function() {
50   spawn(cmd, [], null);
51 }, invalidOptionsMsg);
52
53 assert.throws(function() {
54   spawn(cmd, [], 1);
55 }, invalidOptionsMsg);
56
57 // Argument types for combinatorics
58 const a = [];
59 const o = {};
60 const c = function callback() {};
61 const s = 'string';
62 const u = undefined;
63 const n = null;
64
65 // function spawn(file=f [,args=a] [, options=o]) has valid combinations:
66 //   (f)
67 //   (f, a)
68 //   (f, a, o)
69 //   (f, o)
70 assert.doesNotThrow(function() { spawn(cmd); });
71 assert.doesNotThrow(function() { spawn(cmd, a); });
72 assert.doesNotThrow(function() { spawn(cmd, a, o); });
73 assert.doesNotThrow(function() { spawn(cmd, o); });
74
75 // Variants of undefined as explicit 'no argument' at a position
76 assert.doesNotThrow(function() { spawn(cmd, u, o); });
77 assert.doesNotThrow(function() { spawn(cmd, a, u); });
78
79 assert.throws(function() { spawn(cmd, n, o); }, TypeError);
80 assert.throws(function() { spawn(cmd, a, n); }, TypeError);
81
82 assert.throws(function() { spawn(cmd, s); }, TypeError);
83 assert.throws(function() { spawn(cmd, a, s); }, TypeError);
84
85
86 // verify that execFile has same argument parsing behaviour as spawn
87 //
88 // function execFile(file=f [,args=a] [, options=o] [, callback=c]) has valid
89 // combinations:
90 //   (f)
91 //   (f, a)
92 //   (f, a, o)
93 //   (f, a, o, c)
94 //   (f, a, c)
95 //   (f, o)
96 //   (f, o, c)
97 //   (f, c)
98 assert.doesNotThrow(function() { execFile(cmd); });
99 assert.doesNotThrow(function() { execFile(cmd, a); });
100 assert.doesNotThrow(function() { execFile(cmd, a, o); });
101 assert.doesNotThrow(function() { execFile(cmd, a, o, c); });
102 assert.doesNotThrow(function() { execFile(cmd, a, c); });
103 assert.doesNotThrow(function() { execFile(cmd, o); });
104 assert.doesNotThrow(function() { execFile(cmd, o, c); });
105 assert.doesNotThrow(function() { execFile(cmd, c); });
106
107 // Variants of undefined as explicit 'no argument' at a position
108 assert.doesNotThrow(function() { execFile(cmd, u, o, c); });
109 assert.doesNotThrow(function() { execFile(cmd, a, u, c); });
110 assert.doesNotThrow(function() { execFile(cmd, a, o, u); });
111 assert.doesNotThrow(function() { execFile(cmd, n, o, c); });
112 assert.doesNotThrow(function() { execFile(cmd, a, n, c); });
113 assert.doesNotThrow(function() { execFile(cmd, a, o, n); });
114
115 // string is invalid in arg position (this may seem strange, but is
116 // consistent across node API, cf. `net.createServer('not options', 'not
117 // callback')`
118 assert.throws(function() { execFile(cmd, s, o, c); }, TypeError);
119 assert.doesNotThrow(function() { execFile(cmd, a, s, c); });
120 assert.doesNotThrow(function() { execFile(cmd, a, o, s); });
121
122
123 // verify that fork has same argument parsing behaviour as spawn
124 //
125 // function fork(file=f [,args=a] [, options=o]) has valid combinations:
126 //   (f)
127 //   (f, a)
128 //   (f, a, o)
129 //   (f, o)
130 assert.doesNotThrow(function() { fork(empty); });
131 assert.doesNotThrow(function() { fork(empty, a); });
132 assert.doesNotThrow(function() { fork(empty, a, o); });
133 assert.doesNotThrow(function() { fork(empty, o); });
134
135 assert.throws(function() { fork(empty, s); }, TypeError);
136 assert.doesNotThrow(function() { fork(empty, a, s); }, TypeError);