Permalink
Browse files

Tests for the evented exec.

  • Loading branch information...
1 parent 1e5bb7c commit 541d8e923dded8d58739970de5573c8cbd1df618 mde committed May 20, 2012
Showing with 87 additions and 18 deletions.
  1. +14 −18 lib/utils/index.js
  2. +73 −0 tests/exec.js
View
@@ -60,8 +60,6 @@ _truncate = function (s) {
return str;
};
-
-
/**
@name jake
@namespace jake
@@ -96,8 +94,13 @@ utils = new (function () {
jake.exec(cmds, callback, {stdout: true});
*/
this.exec = function (a, b, c) {
- var e = new Exec(a, b, c);
- e.run();
+ var ex = new Exec(a, b, c);
+ ex.addListener('error', function (msg, code) {
+ if (ex._config.breakOnError) {
+ fail(msg, code);
+ }
+ });
+ ex.run();
};
this.createExec = function (a, b, c) {
@@ -236,12 +239,12 @@ utils.mixin(Exec.prototype, new (function () {
// Spawn a child-process, set up output
sh = spawn(cmd, args);
// Out
- if (config.printStdout) {
- sh.stdout.on('data', function (data) {
+ sh.stdout.on('data', function (data) {
+ if (config.printStdout) {
console.log(_truncate(data.toString()));
- self.emit('stdout', data);
- });
- }
+ }
+ self.emit('stdout', data);
+ });
// Err
sh.stderr.on('data', function (data) {
var d = data.toString();
@@ -261,15 +264,7 @@ utils.mixin(Exec.prototype, new (function () {
msg = _trim(msg);
self.emit('error', msg, code);
}
- if (code != 0 && config.breakOnError) {
- // If there are no error-handlers, fail out. Otherwise,
- // silently stop
- if (!self._events || !self._events.error ||
- (Array.isArray(self._events.error) && !self._events.error.length)) {
- fail(msg, code);
- }
- }
- else {
+ if (code == 0 || !config.breakOnError) {
self.emit('cmdEnd');
_run.call(self);
}
@@ -299,3 +294,4 @@ utils.Exec = Exec;
utils.mixin(utils, fileUtils);
module.exports = utils;
+
View
@@ -0,0 +1,73 @@
+var assert = require('assert')
+ , h = require('./helpers')
+ , jake = {}
+ , utils = require('../lib/utils');
+
+utils.mixin(jake, utils);
+
+process.chdir('./tests');
+
+var tests = new (function () {
+ this.testBasicExec = function () {
+ var ex = jake.createExec('ls', function () {})
+ , evts = { // Events should fire in this order
+ cmdStart: [0, null]
+ , stdout: [1, null]
+ , cmdEnd: [2, null]
+ , end: [3, null]
+ }
+ , incr = 0; // Increment with each event to check order
+ assert.ok(ex instanceof jake.Exec);
+ // Make sure basic events fire and fire in the right order
+ for (var p in evts) {
+ (function (p) {
+ ex.addListener(p, function () {
+ evts[p][1] = incr;
+ incr++;
+ });
+ })(p);
+ }
+ ex.run();
+ ex.addListener('end', function () {
+ for (var p in evts) {
+ assert.equal(evts[p][0], evts[p][1]);
+ }
+ });
+
+ h.next();
+ };
+
+ this.testExecFailure = function () {
+ var ex = jake.createExec('false', function () {});
+ ex.addListener('error', function (msg, code) {
+ assert.equal(1, code);
+ });
+ ex.run();
+ h.next();
+ };
+
+ this.testStdout = function () {
+ var ex = jake.createExec('echo "foo"', function () {});
+ ex.addListener('stdout', function (data) {
+ assert.equal("foo", h.trim(data.toString()));
+ });
+ ex.run();
+ h.next();
+ };
+
+ this.testStderr = function () {
+ var ex = jake.createExec('echo "foo" 1>&2', function () {});
+ ex.addListener('stderr', function (data) {
+ assert.equal("foo", h.trim(data.toString()));
+ });
+ ex.run();
+ h.next();
+ };
+
+})();
+
+h.run(tests, function () {
+ process.chdir('../');
+});
+
+

0 comments on commit 541d8e9

Please sign in to comment.