Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
sys.exec() no longer uses Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 20, 2010
1 parent c04b679 commit de7016e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 59 deletions.
12 changes: 7 additions & 5 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.



Expand Down
11 changes: 5 additions & 6 deletions lib/sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};

/**
Expand Down
46 changes: 23 additions & 23 deletions test/mjsunit/test-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "");
}
});


Expand Down
32 changes: 16 additions & 16 deletions test/mjsunit/test-keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 6 additions & 9 deletions test/mjsunit/test-remote-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit de7016e

Please sign in to comment.