Skip to content

Commit 6417cdc

Browse files
author
James Halliday
committed
use a createStream() function instead of .stream
1 parent 6df7fc0 commit 6417cdc

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ var nextTick = typeof setImmediate !== 'undefined'
1616

1717
exports = module.exports = (function () {
1818
var harness = createHarness();
19-
harness.stream.pipe(createDefaultStream());
19+
harness.createStream().pipe(createDefaultStream());
2020
return harness;
2121
})();
22+
2223
exports.createHarness = createHarness;
2324
exports.Test = Test;
2425
exports.test = exports; // tap compat
@@ -27,9 +28,14 @@ var exitInterval;
2728

2829
function createHarness (conf_) {
2930
var exitCode = 0;
30-
var results = createResultStream();
31+
var results;
3132

3233
var test = function (name, conf, cb) {
34+
if (!results) {
35+
results = createResultStream();
36+
results.pause();
37+
}
38+
3339
var t = new Test(name, conf, cb);
3440
(function inspectCode (st) {
3541
st.on('test', function sub (st_) {
@@ -43,6 +49,12 @@ function createHarness (conf_) {
4349
results.push(t);
4450
return t;
4551
};
46-
test.stream = results;
52+
53+
test.createStream = function () {
54+
if (!results) results = createResultStream();
55+
nextTick(function () { results.resume() });
56+
return results;
57+
};
58+
4759
return test;
4860
}

lib/results.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@ module.exports = function (test) {
1212
output.queue('TAP version 13\n');
1313

1414
var results = new Results(output);
15-
var first = true;
16-
output.push = function (t) {
17-
results.push(t);
18-
if (first) {
19-
t.once('run', function () {
20-
output.resume();
21-
});
22-
}
23-
first = false;
24-
};
15+
output.push = function (t) { results.push(t) };
16+
2517
nextTick(function next () {
2618
var t = results.tests.shift();
2719
if (!t) return results.close();
@@ -61,7 +53,7 @@ Results.prototype.push = function (t, parentT) {
6153
st.on('end', function () {
6254
subtests --;
6355
if (subtests === 0 && !plan) t.emit('end');
64-
onend();
56+
nextTick(function () { onend.call(t) });
6557
});
6658
self.push(st, t);
6759
if (subtests === 1) nextTick(function () { st.run() });
@@ -81,17 +73,26 @@ Results.prototype.push = function (t, parentT) {
8173
t.on('end', onend);
8274

8375
function onend () {
76+
if (this.ended) return;
8477
if (subtests !== 0) return;
85-
if (!plan && self.tests.length === 0) self.close();
78+
if (!plan && self.tests.length === 0) {
79+
nextTick(function () {
80+
if (!plan && self.tests.length === 0) {
81+
self.close();
82+
}
83+
});
84+
}
8685
else if (!plan && self.tests.length) {
8786
var t = self.tests.shift();
8887
nextTick(function () { t.run() });
89-
};
88+
}
9089
}
9190
};
9291

9392
Results.prototype.close = function () {
9493
var self = this;
94+
if (self.closed) self.stream.emit('error', new Error('ALREADY CLOSED'));
95+
self.closed = true;
9596
var write = function (s) { self.stream.queue(s) };
9697

9798
write('\n1..' + self.count + '\n');

0 commit comments

Comments
 (0)