| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fe4b309 commit 609db5a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,8 @@ exports.fork = function(modulePath /*, args, options*/) { | |||
| 23 | 23 | if (Array.isArray(arguments[1])) { | |
| 24 | 24 | args = arguments[1]; | |
| 25 | 25 | options = util._extend({}, arguments[2]); | |
| 26 | + } else if (arguments[1] && typeof arguments[1] !== 'object') { | ||
| 27 | + throw new TypeError('Incorrect value of args option'); | ||
| 26 | 28 | } else { | |
| 27 | 29 | args = []; | |
| 28 | 30 | options = util._extend({}, arguments[1]); | |
@@ -104,7 +106,7 @@ exports.exec = function(command /*, options, callback*/) { | |||
| 104 | 106 | ||
| 105 | 107 | ||
| 106 | 108 | exports.execFile = function(file /*, args, options, callback*/) { | |
| 107 | - var args, callback; | ||
| 109 | + var args = [], callback; | ||
| 108 | 110 | var options = { | |
| 109 | 111 | encoding: 'utf8', | |
| 110 | 112 | timeout: 0, | |
@@ -114,18 +116,26 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 114 | 116 | env: null | |
| 115 | 117 | }; | |
| 116 | 118 | ||
| 117 | - // Parse the parameters. | ||
| 119 | + // Parse the optional positional parameters. | ||
| 120 | + var pos = 1; | ||
| 121 | + if (pos < arguments.length && Array.isArray(arguments[pos])) { | ||
| 122 | + args = arguments[pos++]; | ||
| 123 | + } else if (pos < arguments.length && arguments[pos] == null) { | ||
| 124 | + pos++; | ||
| 125 | + } | ||
| 118 | 126 | ||
| 119 | - if (typeof arguments[arguments.length - 1] === 'function') { | ||
| 120 | - callback = arguments[arguments.length - 1]; | ||
| 127 | + if (pos < arguments.length && typeof arguments[pos] === 'object') { | ||
| 128 | + options = util._extend(options, arguments[pos++]); | ||
| 129 | + } else if (pos < arguments.length && arguments[pos] == null) { | ||
| 130 | + pos++; | ||
| 121 | 131 | } | |
| 122 | 132 | ||
| 123 | - if (Array.isArray(arguments[1])) { | ||
| 124 | - args = arguments[1]; | ||
| 125 | - options = util._extend(options, arguments[2]); | ||
| 126 | - } else { | ||
| 127 | - args = []; | ||
| 128 | - options = util._extend(options, arguments[1]); | ||
| 133 | + if (pos < arguments.length && typeof arguments[pos] === 'function') { | ||
| 134 | + callback = arguments[pos++]; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + if (pos === 1 && arguments.length > 1) { | ||
| 138 | + throw new TypeError('Incorrect value of args option'); | ||
| 129 | 139 | } | |
| 130 | 140 | ||
| 131 | 141 | var child = spawn(file, args, { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,19 @@ | |||
| 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); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments