|
1 | 1 | 'use strict';
|
2 |
| -var assert = require('assert'); |
3 |
| -var child_process = require('child_process'); |
4 |
| -var spawn = child_process.spawn; |
5 |
| -var cmd = require('../common').isWindows ? 'rundll32' : 'ls'; |
6 |
| -var invalidArgsMsg = /Incorrect value of args option/; |
7 |
| -var invalidOptionsMsg = /options argument must be an object/; |
8 |
| - |
9 |
| -// verify that args argument must be an array |
| 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 | + |
10 | 14 | assert.throws(function() {
|
11 |
| - spawn(cmd, 'this is not an array'); |
| 15 | + var child = spawn(invalidcmd, 'this is not an array'); |
| 16 | + child.on('error', assert.fail); |
12 | 17 | }, TypeError);
|
13 | 18 |
|
14 | 19 | // verify that valid argument combinations do not throw
|
@@ -49,3 +54,83 @@ assert.throws(function() {
|
49 | 54 | spawn(cmd, [], 1);
|
50 | 55 | }, invalidOptionsMsg);
|
51 | 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); |
0 commit comments