diff --git a/lib/child_process.js b/lib/child_process.js index b881150f29..3996c954c1 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -10,24 +10,26 @@ var spawn = exports.spawn = function (path, args, env, customFds) { return child; }; -exports.exec = function (command /*, options, callback */) { +exports.exec = function (command /*, options, callback, env */) { if (arguments.length < 3) { return exports.execFile("/bin/sh", ["-c", command], arguments[1]); - } else { + } else if (arguments.length < 4) { return exports.execFile("/bin/sh", ["-c", command], arguments[1], arguments[2]); + } else { + return exports.execFile("/bin/sh", ["-c", command], arguments[1], arguments[2], arguments[3]); } }; -exports.execFile = function (file, args /*, options, callback */) { +exports.execFile = function (file, args /*, options, callback, env */) { var options = { encoding: 'utf8' , timeout: 0 , maxBuffer: 200*1024 , killSignal: 'SIGKILL' }; - var callback = arguments[arguments.length-1]; - - if (typeof arguments[2] == 'object') { + var callback = (arguments.length == 5 ? arguments[3] : arguments[arguments.length-1]); + + if (arguments[2] && typeof arguments[2] == 'object') { var keys = Object.keys(options); for (var i = 0; i < keys.length; i++) { var k = keys[i]; @@ -35,7 +37,7 @@ exports.execFile = function (file, args /*, options, callback */) { } } - var child = spawn(file, args); + var child = arguments[4] ? spawn(file, args, arguments[4]) : spawn(file, args); var stdout = ""; var stderr = ""; var killed = false; diff --git a/test/simple/test-child-process-exec-env.js b/test/simple/test-child-process-exec-env.js new file mode 100644 index 0000000000..58d6d90148 --- /dev/null +++ b/test/simple/test-child-process-exec-env.js @@ -0,0 +1,31 @@ +require('../common'); +var exec = require('child_process').exec, + sys = require('sys'); +success_count = 0; +error_count = 0; +response = ""; + +child = exec('/usr/bin/env', [], function (err, stdout, stderr) { + if (err) { + error_count++; + console.log('error!: ' + err.code); + console.log('stdout: ' + JSON.stringify(stdout)); + console.log('stderr: ' + JSON.stringify(stderr)); + assert.equal(false, err.killed); + } else { + success_count++; + assert.equal(true, stdout != ""); + } +}, {'HELLO' : 'WORLD'}); + +child.stdout.setEncoding('utf8'); + +child.stdout.addListener('data', function (chunk) { + response += chunk; +}); + +process.addListener('exit', function () { + assert.equal(1, success_count); + assert.equal(0, error_count); + assert.ok(response.indexOf('HELLO=WORLD') >= 0); +});