Permalink
Browse files

Give formatters a chance to clear buffers before exiting

  • Loading branch information...
1 parent 1f35803 commit 7970444c5eaf35ddc8fe524aaaa8b1aec1e597ff @glenjamin committed Feb 2, 2012
Showing with 26 additions and 4 deletions.
  1. +9 −1 lib/formatters/progress_formatter.js
  2. +17 −3 lib/index.js
@@ -27,14 +27,22 @@ function ProgressFormatter(emitter) {
ProgressFormatter.prototype.write = function(str, wrap) {
if (wrap)
str = wrap(str);
- this.output_stream.write(str);
+ return this.output_stream.write(str);
};
ProgressFormatter.prototype.writeln = function(str, wrap) {
if (!str) str = '';
this.write(str, wrap);
this.write('\n');
};
+ProgressFormatter.prototype.onComplete = function(callback) {
+ if (this.write('')) {
+ callback();
+ } else {
+ this.output_stream.once('drain', callback);
+ }
+}
+
ProgressFormatter.prototype.suiteStart = function() {
this.startTime = new Date();
};
View
@@ -364,9 +364,14 @@ function registerBuiltinFormatters(nodespec) {
}
function initFormatters(nodespec, emitter) {
+ var formatters = [];
nodespec.formatters.forEach(function(f) {
- f.init(emitter);
+ var formatter = f.init(emitter);
+ if (formatter) {
+ formatters.push(formatter);
+ }
});
+ return formatters;
}
/*
@@ -378,7 +383,7 @@ function initFormatters(nodespec, emitter) {
*/
nodespec.exec = function exec() {
var emitter = new EventEmitter();
- initFormatters(this, emitter);
+ var formatters = initFormatters(this, emitter);
emitter.emit('suiteStart');
var overall_result = new this.deps.Result();
async.forEachSeries(
@@ -399,7 +404,16 @@ nodespec.exec = function exec() {
process.exit(3);
}
emitter.emit('suiteComplete', overall_result);
- process.exit(overall_result.exit_code);
+ // Give the formatters chance to flush their buffers
+ async.forEach(formatters, function(formatter, next) {
+ if (typeof formatter.onComplete == 'function') {
+ formatter.onComplete(next);
+ } else {
+ next();
+ }
+ }, function finished(err) {
+ process.exit(overall_result.exit_code);
+ })
}
);
}

0 comments on commit 7970444

Please sign in to comment.