Skip to content

Commit

Permalink
added env to child_process.exec
Browse files Browse the repository at this point in the history
  • Loading branch information
Сергей Крыжановский authored and ry committed Jul 16, 2010
1 parent 98341da commit 078a48a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@ 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];
if (arguments[2][k] !== undefined) options[k] = arguments[2][k];
}
}

var child = spawn(file, args);
var child = arguments[4] ? spawn(file, args, arguments[4]) : spawn(file, args);
var stdout = "";
var stderr = "";
var killed = false;
Expand Down
31 changes: 31 additions & 0 deletions test/simple/test-child-process-exec-env.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 078a48a

Please sign in to comment.