Browse Source

added env to child_process.exec

v0.7.4-release
Сергей Крыжановский 15 years ago
committed by Ryan Dahl
parent
commit
078a48a97b
  1. 16
      lib/child_process.js
  2. 31
      test/simple/test-child-process-exec-env.js

16
lib/child_process.js

@ -10,24 +10,26 @@ var spawn = exports.spawn = function (path, args, env, customFds) {
return child; return child;
}; };
exports.exec = function (command /*, options, callback */) { exports.exec = function (command /*, options, callback, env */) {
if (arguments.length < 3) { if (arguments.length < 3) {
return exports.execFile("/bin/sh", ["-c", command], arguments[1]); 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]); 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' var options = { encoding: 'utf8'
, timeout: 0 , timeout: 0
, maxBuffer: 200*1024 , maxBuffer: 200*1024
, killSignal: 'SIGKILL' , killSignal: 'SIGKILL'
}; };
var callback = arguments[arguments.length-1]; var callback = (arguments.length == 5 ? arguments[3] : arguments[arguments.length-1]);
if (typeof arguments[2] == 'object') { if (arguments[2] && typeof arguments[2] == 'object') {
var keys = Object.keys(options); var keys = Object.keys(options);
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
var k = keys[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 stdout = "";
var stderr = ""; var stderr = "";
var killed = false; var killed = false;

31
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);
});
Loading…
Cancel
Save