|
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 |
10 |
| -assert.throws(function() { |
11 |
| - spawn(cmd, 'this is not an array'); |
12 |
| -}, TypeError); |
| 2 | +var assert = require('assert'), |
| 3 | + child_process = require('child_process'), |
| 4 | + spawn = child_process.spawn, |
| 5 | + fork = child_process.fork, |
| 6 | + execFile = child_process.execFile, |
| 7 | + windows = (process.platform === 'win32'), |
| 8 | + cmd = (windows) ? 'rundll32' : 'ls', |
| 9 | + invalidcmd = 'hopefully_you_dont_have_this_on_your_machine', |
| 10 | + invalidArgsMsg = /Incorrect value of args option/, |
| 11 | + invalidOptionsMsg = /options argument must be an object/, |
| 12 | + empty = require('../common').fixturesDir + '/empty.js', |
| 13 | + errors = 0; |
| 14 | + |
| 15 | +try { |
| 16 | + // Ensure this throws a TypeError |
| 17 | + var child = spawn(invalidcmd, 'this is not an array'); |
| 18 | + |
| 19 | + child.on('error', function(err) { |
| 20 | + errors++; |
| 21 | + }); |
| 22 | + |
| 23 | +} catch (e) { |
| 24 | + assert.equal(e instanceof TypeError, true); |
| 25 | +} |
13 | 26 |
|
14 | 27 | // verify that valid argument combinations do not throw
|
15 | 28 | assert.doesNotThrow(function() {
|
@@ -49,3 +62,87 @@ assert.throws(function() {
|
49 | 62 | spawn(cmd, [], 1);
|
50 | 63 | }, invalidOptionsMsg);
|
51 | 64 |
|
| 65 | +process.on('exit', function() { |
| 66 | + assert.equal(errors, 0); |
| 67 | +}); |
| 68 | + |
| 69 | +// Argument types for combinatorics |
| 70 | +var a = [], |
| 71 | + o = {}, |
| 72 | + c = (function callback() {}), |
| 73 | + s = 'string', |
| 74 | + u = undefined, |
| 75 | + n = null; |
| 76 | + |
| 77 | +// function spawn(file=f [,args=a] [, options=o]) has valid combinations: |
| 78 | +// (f) |
| 79 | +// (f, a) |
| 80 | +// (f, a, o) |
| 81 | +// (f, o) |
| 82 | +assert.doesNotThrow(function() { spawn(cmd); }); |
| 83 | +assert.doesNotThrow(function() { spawn(cmd, a); }); |
| 84 | +assert.doesNotThrow(function() { spawn(cmd, a, o); }); |
| 85 | +assert.doesNotThrow(function() { spawn(cmd, o); }); |
| 86 | + |
| 87 | +// Variants of undefined as explicit 'no argument' at a position |
| 88 | +assert.doesNotThrow(function() { spawn(cmd, u, o); }); |
| 89 | +assert.doesNotThrow(function() { spawn(cmd, a, u); }); |
| 90 | + |
| 91 | +assert.throws(function() { spawn(cmd, n, o); }, TypeError); |
| 92 | +assert.throws(function() { spawn(cmd, a, n); }, TypeError); |
| 93 | + |
| 94 | +assert.throws(function() { spawn(cmd, s); }, TypeError); |
| 95 | +assert.throws(function() { spawn(cmd, a, s); }, TypeError); |
| 96 | + |
| 97 | + |
| 98 | +// verify that execFile has same argument parsing behaviour as spawn |
| 99 | +// |
| 100 | +// function execFile(file=f [,args=a] [, options=o] [, callback=c]) has valid |
| 101 | +// combinations: |
| 102 | +// (f) |
| 103 | +// (f, a) |
| 104 | +// (f, a, o) |
| 105 | +// (f, a, o, c) |
| 106 | +// (f, a, c) |
| 107 | +// (f, o) |
| 108 | +// (f, o, c) |
| 109 | +// (f, c) |
| 110 | +assert.doesNotThrow(function() { execFile(cmd); }); |
| 111 | +assert.doesNotThrow(function() { execFile(cmd, a); }); |
| 112 | +assert.doesNotThrow(function() { execFile(cmd, a, o); }); |
| 113 | +assert.doesNotThrow(function() { execFile(cmd, a, o, c); }); |
| 114 | +assert.doesNotThrow(function() { execFile(cmd, a, c); }); |
| 115 | +assert.doesNotThrow(function() { execFile(cmd, o); }); |
| 116 | +assert.doesNotThrow(function() { execFile(cmd, o, c); }); |
| 117 | +assert.doesNotThrow(function() { execFile(cmd, c); }); |
| 118 | + |
| 119 | +// Variants of undefined as explicit 'no argument' at a position |
| 120 | +assert.doesNotThrow(function() { execFile(cmd, u, o, c); }); |
| 121 | +assert.doesNotThrow(function() { execFile(cmd, a, u, c); }); |
| 122 | +assert.doesNotThrow(function() { execFile(cmd, a, o, u); }); |
| 123 | +assert.doesNotThrow(function() { execFile(cmd, n, o, c); }); |
| 124 | +assert.doesNotThrow(function() { execFile(cmd, a, n, c); }); |
| 125 | +assert.doesNotThrow(function() { execFile(cmd, a, o, n); }); |
| 126 | + |
| 127 | +// string is invalid in arg position (this may seem strange, but is |
| 128 | +// consistent across node API, cf. `net.createServer('not options', 'not |
| 129 | +// callback')` |
| 130 | +assert.throws(function() { execFile(cmd, s, o, c); }, TypeError); |
| 131 | +assert.doesNotThrow(function() { execFile(cmd, a, s, c); }); |
| 132 | +assert.doesNotThrow(function() { execFile(cmd, a, o, s); }); |
| 133 | + |
| 134 | + |
| 135 | +// verify that fork has same argument parsing behaviour as spawn |
| 136 | +// |
| 137 | +// function fork(file=f [,args=a] [, options=o]) has valid combinations: |
| 138 | +// (f) |
| 139 | +// (f, a) |
| 140 | +// (f, a, o) |
| 141 | +// (f, o) |
| 142 | +assert.doesNotThrow(function() { fork(empty); }); |
| 143 | +assert.doesNotThrow(function() { fork(empty, a); }); |
| 144 | +assert.doesNotThrow(function() { fork(empty, a, o); }); |
| 145 | +assert.doesNotThrow(function() { fork(empty, o); }); |
| 146 | + |
| 147 | +assert.throws(function() { fork(empty, s); }, TypeError); |
| 148 | +assert.doesNotThrow(function() { fork(empty, a, s); }, TypeError); |
0 commit comments