From 542ac7f3d204be52c829f25270e840933e397bc0 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 17 Sep 2014 17:26:46 -0400 Subject: [PATCH] child_process: properly support optional args Currently, a TypeError is incorrectly thrown if the second argument is an object. This commit allows the args argument to be properly omitted. Fixes: https://github.com/joyent/node/issues/6068 Reviewed-by: Trevor Norris --- lib/child_process.js | 2 +- test/simple/test-child-process-spawn-typeerror.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index e19f4ff89d2..0c1b4c99c1f 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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 = []; diff --git a/test/simple/test-child-process-spawn-typeerror.js b/test/simple/test-child-process-spawn-typeerror.js index 791adcbc208..d18ce943ec9 100644 --- a/test/simple/test-child-process-spawn-typeerror.js +++ b/test/simple/test-child-process-spawn-typeerror.js @@ -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++; @@ -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); });