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. 46
      test/mjsunit/test-exec.js
  4. 32
      test/mjsunit/test-keep-alive.js
  5. 15
      test/mjsunit/test-remote-module-loading.js

12
doc/api.txt

@ -208,19 +208,21 @@ output the string immediately to stdout.
+inspect(object, showHidden)+ :: +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. 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 Executes the command as a child process, buffers the output and returns it
in a promise callback. in a callback.
+ +
---------------------------------------- ----------------------------------------
var sys = require("sys"); 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); sys.puts(stdout);
}); });
---------------------------------------- ----------------------------------------
+ +
- on success: stdout buffer, stderr buffer The callback gets the arguments +(err, stdout, stderr)+. On success +err+
- on error: exit code, stdout buffer, stderr buffer 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 child = process.createChildProcess("/bin/sh", ["-c", command]);
var stdout = ""; var stdout = "";
var stderr = ""; var stderr = "";
var promise = new events.Promise();
child.addListener("output", function (chunk) { child.addListener("output", function (chunk) {
if (chunk) stdout += chunk; if (chunk) stdout += chunk;
@ -154,13 +153,13 @@ exports.exec = function (command) {
child.addListener("exit", function (code) { child.addListener("exit", function (code) {
if (code == 0) { if (code == 0) {
promise.emitSuccess(stdout, stderr); if (callback) callback(null, stdout, stderr);
} else { } else {
promise.emitError(code, stdout, stderr); var e = new Error("Command failed: " + stderr);
e.code = code;
if (callback) callback(e, stdout, stderr);
} }
}); });
return promise;
}; };
/** /**

46
test/mjsunit/test-exec.js

@ -3,32 +3,32 @@ process.mixin(require("./common"));
success_count = 0; success_count = 0;
error_count = 0; error_count = 0;
exec("ls /").addCallback(function (out) { exec("ls /", function (err, stdout, stderr) {
success_count++; if (err) {
p(out); error_count++;
}).addErrback(function (code, out, err) { puts("error!: " + err.code);
error_count++; puts("stdout: " + JSON.stringify(stdout));
puts("error!: " + code); puts("stderr: " + JSON.stringify(stderr));
puts("stdout: " + JSON.stringify(out)); } else {
puts("stderr: " + JSON.stringify(err)); success_count++;
p(stdout);
}
}); });
exec("ls /DOES_NOT_EXIST", function (err, stdout, stderr) {
exec("ls /DOES_NOT_EXIST").addCallback(function (out) { if (err) {
success_count++; error_count++;
p(out); assert.equal("", stdout);
assert.equal(true, out != ""); assert.equal(true, err.code != 0);
puts("error code: " + err.code);
}).addErrback(function (code, out, err) { puts("stdout: " + JSON.stringify(stdout));
error_count++; puts("stderr: " + JSON.stringify(stderr));
} else {
assert.equal("", out); success_count++;
assert.equal(true, code != 0); p(stdout);
assert.equal(true, stdout != "");
puts("error!: " + code); }
puts("stdout: " + JSON.stringify(out));
puts("stderr: " + JSON.stringify(err));
}); });

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

@ -23,22 +23,22 @@ function error (msg) {
} }
function runAb(opts, callback) { function runAb(opts, callback) {
sys.exec("ab " + opts + " http://127.0.0.1:" + PORT + "/") var command = "ab " + opts + " http://127.0.0.1:" + PORT + "/";
.addErrback(error) sys.exec(command, function (err, stdout, stderr) {
.addCallback(function (out) { if (err) throw err;
var matches = /Requests per second:\s*(\d+)\./mi.exec(out); var matches = /Requests per second:\s*(\d+)\./mi.exec(stdout);
var reqSec = parseInt(matches[1]); 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; var keepAliveRequests;
if (matches) { if (matches) {
keepAliveRequests = parseInt(matches[1]); keepAliveRequests = parseInt(matches[1]);
} else { } else {
keepAliveRequests = 0; keepAliveRequests = 0;
} }
callback(reqSec, keepAliveRequests); callback(reqSec, keepAliveRequests);
}); });
} }
runAb("-k -c 100 -t 2", function (reqSec, keepAliveRequests) { runAb("-k -c 100 -t 2", function (reqSec, keepAliveRequests) {

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

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

Loading…
Cancel
Save