Browse Source

sys.exec() no longer uses Promise

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
de7016eac5
  1. 12
      doc/api.txt
  2. 11
      lib/sys.js
  3. 42
      test/mjsunit/test-exec.js
  4. 10
      test/mjsunit/test-keep-alive.js
  5. 9
      test/mjsunit/test-remote-module-loading.js

12
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.

11
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;
};
/**

42
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) {
exec("ls /", function (err, stdout, stderr) {
if (err) {
error_count++;
puts("error!: " + code);
puts("stdout: " + JSON.stringify(out));
puts("stderr: " + JSON.stringify(err));
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) {
exec("ls /DOES_NOT_EXIST", function (err, stdout, stderr) {
if (err) {
error_count++;
assert.equal("", out);
assert.equal(true, code != 0);
puts("error!: " + code);
puts("stdout: " + JSON.stringify(out));
puts("stderr: " + JSON.stringify(err));
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 != "");
}
});

10
test/mjsunit/test-keep-alive.js

@ -23,13 +23,13 @@ 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 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(out);
matches = /Keep-Alive requests:\s*(\d+)/mi.exec(stdout);
var keepAliveRequests;
if (matches) {
keepAliveRequests = parseInt(matches[1]);

9
test/mjsunit/test-remote-module-loading.js

@ -26,14 +26,11 @@ assert.throws(function () {
var nodeBinary = process.ARGV[0];
var cmd = 'NODE_PATH='+libDir+' '+nodeBinary+' http://localhost:'+PORT+'/moduleB.js';
sys
.exec(cmd)
.addCallback(function() {
sys.exec(cmd, function (err, stdout, stderr) {
if (err) throw err;
puts('success!');
modulesLoaded++;
server.close();
})
.addErrback(function(code, stdout, stderr) {
assertUnreachable('node binary could not load module from url: ' + stderr);
});
process.addListener('exit', function() {

Loading…
Cancel
Save