From 078a48a97b5b2597443f867e904a4c7e7f061c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9A=D1=80=D1=8B?= =?UTF-8?q?=D0=B6=D0=B0=D0=BD=D0=BE=D0=B2=D1=81=D0=BA=D0=B8=D0=B9?= Date: Fri, 9 Jul 2010 17:05:54 +0400 Subject: [PATCH] added env to child_process.exec --- lib/child_process.js | 16 ++++++----- test/simple/test-child-process-exec-env.js | 31 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 test/simple/test-child-process-exec-env.js 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); +});