Skip to content

Commit

Permalink
Count the number of times test is invoked
Browse files Browse the repository at this point in the history
and only close the out stream once they are all done.
  • Loading branch information
Raynos committed Dec 12, 2012
1 parent ba72d57 commit e436a61
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,53 @@ var canExit = typeof process !== 'undefined' && process
function createHarness (conf_) {
var pending = [];
var running = false;

var count = 0;

var began = false;
var out = new Render();

var test = function (name, conf, cb) {
count++;
var t = new Test(name, conf, cb);
if (!conf || typeof conf !== 'object') conf = conf_ || {};

if (conf.exit !== false && canEmitExit) {
process.on('exit', function (code) {
t._exit();
out.close();
if (code === 0 && !t._ok) process.exit(1);
});
}

process.nextTick(function () {
if (!out.piped) out.pipe(createDefaultStream());
if (!began) out.begin();
began = true;

var run = function () {
running = true;
out.push(t);
t.run();
};

if (running || pending.length) {
pending.push(run);
}
else run();
});

t.on('test', function sub (st) {
st.on('test', sub);
st.on('end', onend);
});

t.on('end', onend);

return t;

function onend () {
running = false;
count--;
if (this._progeny.length) {
var unshifts = this._progeny.map(function (st) {
return function () {
Expand All @@ -73,16 +76,18 @@ function createHarness (conf_) {

process.nextTick(function () {
if (pending.length) return pending.shift()();
out.close();
if (count === 0) {
out.close();
}
if (conf.exit !== false && canExit && !t._ok) {
process.exit(1);
}
});
}
};

test.stream = out;
return test;
}

// vim: set softtabstop=4 shiftwidth=4:
// vim: set softtabstop=4 shiftwidth=4:

0 comments on commit e436a61

Please sign in to comment.