summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/api.txt12
-rw-r--r--lib/sys.js11
-rw-r--r--test/mjsunit/test-exec.js46
-rw-r--r--test/mjsunit/test-keep-alive.js32
-rw-r--r--test/mjsunit/test-remote-module-loading.js15
5 files changed, 57 insertions, 59 deletions
diff --git a/doc/api.txt b/doc/api.txt
index fbbe52704f..dcdc66a966 100644
--- a/doc/api.txt
+++ b/doc/api.txt
@@ -208,19 +208,21 @@ output the string immediately to stdout.
+inspect(object, showHidden)+ ::
Return a string representation of the +object+. (For debugging.) If showHidden is true, then the object's non-enumerable properties will be shown too.
-+exec(command)+::
++exec(command, callback)+::
Executes the command as a child process, buffers the output and returns it
-in a promise callback.
+in a callback.
+
----------------------------------------
var sys = require("sys");
-sys.exec("ls /").addCallback(function (stdout, stderr) {
+sys.exec("ls /", function (err, stdout, stderr) {
+ if (err) throw err;
sys.puts(stdout);
});
----------------------------------------
+
-- on success: stdout buffer, stderr buffer
-- on error: exit code, stdout buffer, stderr buffer
+The callback gets the arguments +(err, stdout, stderr)+. On success +err+
+will be +null+. On error +err+ will be an instance of +Error+ and +err.code+
+will be the exit code of the child process.
diff --git a/lib/sys.js b/lib/sys.js
index 65badcdc05..cbdf0c5788 100644
--- a/lib/sys.js
+++ b/lib/sys.js
@@ -138,11 +138,10 @@ exports.p = function () {
}
};
-exports.exec = function (command) {
+exports.exec = function (command, callback) {
var child = process.createChildProcess("/bin/sh", ["-c", command]);
var stdout = "";
var stderr = "";
- var promise = new events.Promise();
child.addListener("output", function (chunk) {
if (chunk) stdout += chunk;
@@ -154,13 +153,13 @@ exports.exec = function (command) {
child.addListener("exit", function (code) {
if (code == 0) {
- promise.emitSuccess(stdout, stderr);
+ if (callback) callback(null, stdout, stderr);
} else {
- promise.emitError(code, stdout, stderr);
+ var e = new Error("Command failed: " + stderr);
+ e.code = code;
+ if (callback) callback(e, stdout, stderr);
}
});
-
- return promise;
};
/**
diff --git a/test/mjsunit/test-exec.js b/test/mjsunit/test-exec.js
index 2098fdfd67..f817ebd696 100644
--- a/test/mjsunit/test-exec.js
+++ b/test/mjsunit/test-exec.js
@@ -3,32 +3,32 @@ process.mixin(require("./common"));
success_count = 0;
error_count = 0;
-exec("ls /").addCallback(function (out) {
- success_count++;
- p(out);
-}).addErrback(function (code, out, err) {
- error_count++;
- puts("error!: " + code);
- puts("stdout: " + JSON.stringify(out));
- puts("stderr: " + JSON.stringify(err));
+exec("ls /", function (err, stdout, stderr) {
+ if (err) {
+ error_count++;
+ puts("error!: " + err.code);
+ puts("stdout: " + JSON.stringify(stdout));
+ puts("stderr: " + JSON.stringify(stderr));
+ } else {
+ success_count++;
+ p(stdout);
+ }
});
-
-exec("ls /DOES_NOT_EXIST").addCallback(function (out) {
- success_count++;
- p(out);
- assert.equal(true, out != "");
-
-}).addErrback(function (code, out, err) {
- error_count++;
-
- assert.equal("", out);
- assert.equal(true, code != 0);
-
- puts("error!: " + code);
- puts("stdout: " + JSON.stringify(out));
- puts("stderr: " + JSON.stringify(err));
+exec("ls /DOES_NOT_EXIST", function (err, stdout, stderr) {
+ if (err) {
+ error_count++;
+ assert.equal("", stdout);
+ assert.equal(true, err.code != 0);
+ puts("error code: " + err.code);
+ puts("stdout: " + JSON.stringify(stdout));
+ puts("stderr: " + JSON.stringify(stderr));
+ } else {
+ success_count++;
+ p(stdout);
+ assert.equal(true, stdout != "");
+ }
});
diff --git a/test/mjsunit/test-keep-alive.js b/test/mjsunit/test-keep-alive.js
index 2307cd3bf4..b9ec2703f0 100644
--- a/test/mjsunit/test-keep-alive.js
+++ b/test/mjsunit/test-keep-alive.js
@@ -23,22 +23,22 @@ function error (msg) {
}
function runAb(opts, callback) {
- sys.exec("ab " + opts + " http://127.0.0.1:" + PORT + "/")
- .addErrback(error)
- .addCallback(function (out) {
- var matches = /Requests per second:\s*(\d+)\./mi.exec(out);
- var reqSec = parseInt(matches[1]);
-
- matches = /Keep-Alive requests:\s*(\d+)/mi.exec(out);
- var keepAliveRequests;
- if (matches) {
- keepAliveRequests = parseInt(matches[1]);
- } else {
- keepAliveRequests = 0;
- }
-
- callback(reqSec, keepAliveRequests);
- });
+ var command = "ab " + opts + " http://127.0.0.1:" + PORT + "/";
+ sys.exec(command, function (err, stdout, stderr) {
+ if (err) throw err;
+ var matches = /Requests per second:\s*(\d+)\./mi.exec(stdout);
+ var reqSec = parseInt(matches[1]);
+
+ matches = /Keep-Alive requests:\s*(\d+)/mi.exec(stdout);
+ var keepAliveRequests;
+ if (matches) {
+ keepAliveRequests = parseInt(matches[1]);
+ } else {
+ keepAliveRequests = 0;
+ }
+
+ callback(reqSec, keepAliveRequests);
+ });
}
runAb("-k -c 100 -t 2", function (reqSec, keepAliveRequests) {
diff --git a/test/mjsunit/test-remote-module-loading.js b/test/mjsunit/test-remote-module-loading.js
index 6f6f82e080..e49eed70a0 100644
--- a/test/mjsunit/test-remote-module-loading.js
+++ b/test/mjsunit/test-remote-module-loading.js
@@ -26,15 +26,12 @@ assert.throws(function () {
var nodeBinary = process.ARGV[0];
var cmd = 'NODE_PATH='+libDir+' '+nodeBinary+' http://localhost:'+PORT+'/moduleB.js';
-sys
- .exec(cmd)
- .addCallback(function() {
- modulesLoaded++;
- server.close();
- })
- .addErrback(function(code, stdout, stderr) {
- assertUnreachable('node binary could not load module from url: ' + stderr);
- });
+sys.exec(cmd, function (err, stdout, stderr) {
+ if (err) throw err;
+ puts('success!');
+ modulesLoaded++;
+ server.close();
+});
process.addListener('exit', function() {
assert.equal(1, modulesLoaded);