Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
child_process: properly support optional args
Browse files Browse the repository at this point in the history
Currently, a TypeError is incorrectly thrown if the second argument is
an object. This commit allows the args argument to be properly omitted.

Fixes: #6068
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
cjihrig authored and trevnorris committed Sep 17, 2014
1 parent 84952da commit 542ac7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ var spawn = exports.spawn = function(file /*, args, options*/) {
if (Array.isArray(arguments[1])) {
args = arguments[1].slice(0);
options = arguments[2];
} else if (arguments[1] && !Array.isArray(arguments[1])) {
} else if (arguments[1] && typeof arguments[1] !== 'object') {
throw new TypeError('Incorrect value of args option');
} else {
args = [];
Expand Down
10 changes: 8 additions & 2 deletions test/simple/test-child-process-spawn-typeerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
var spawn = require('child_process').spawn,
assert = require('assert'),
windows = (process.platform === 'win32'),
cmd = (windows) ? 'ls' : 'dir',
cmd = (windows) ? 'dir' : 'ls',
invalidcmd = (windows) ? 'ls' : 'dir',
errors = 0;

try {
// Ensure this throws a TypeError
var child = spawn(cmd, 'this is not an array');
var child = spawn(invalidcmd, 'this is not an array');

child.on('error', function (err) {
errors++;
Expand All @@ -37,6 +38,11 @@ try {
assert.equal(e instanceof TypeError, true);
}

// verify that args argument is optional
assert.doesNotThrow(function() {
spawn(cmd, {});
});

process.on('exit', function() {
assert.equal(errors, 0);
});

0 comments on commit 542ac7f

Please sign in to comment.