Skip to content

Commit 827b59d

Browse files
author
James Halliday
committed
took out lots of complexity, refactoring
1 parent 78c7b77 commit 827b59d

File tree

3 files changed

+42
-140
lines changed

3 files changed

+42
-140
lines changed

index.js

+16-128
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
var createDefaultStream = require('./lib/default_stream');
22
var Render = require('./lib/render');
33
var Test = require('./lib/test');
4+
var through = require('through');
45

56
var canEmitExit = typeof process !== 'undefined' && process
67
&& typeof process.on === 'function'
78
;
89
var canExit = typeof process !== 'undefined' && process
910
&& typeof process.exit === 'function'
1011
;
11-
var onexit = (function () {
12-
var stack = [];
13-
if (canEmitExit) process.on('exit', function (code) {
14-
for (var i = 0; i < stack.length; i++) stack[i](code);
15-
});
16-
return function (cb) { stack.push(cb) };
17-
})();
1812

1913
var nextTick = typeof setImmediate !== 'undefined'
2014
? setImmediate
@@ -28,138 +22,32 @@ exports.Test = Test;
2822
var exitInterval;
2923

3024
function createHarness (conf_) {
31-
var pending = [];
32-
var running = false;
3325
var count = 0;
34-
35-
var began = false;
36-
var only = false;
37-
var closed = false;
38-
var out = new Render();
39-
if (!conf_) conf_ = {};
40-
41-
var tests = [];
42-
if (conf_.exit === false && exitInterval) clearInterval(exitInterval);
43-
44-
exitInterval = !exitInterval && conf_.exit !== false && canEmitExit
45-
&& typeof process._getActiveHandles === 'function'
46-
&& setInterval(function () {
47-
if (/^v0\.8\./.test(process.version)
48-
&& process._getActiveHandles().length === 1) {
49-
tests.forEach(function (t) { t._exit() });
50-
}
51-
}, 200);
52-
5326
var exitCode = 0;
54-
var exit = function (c) { exitCode = c };
55-
56-
out.on('end', function () {
57-
nextTick(function () {
58-
clearInterval(exitInterval);
59-
if (canExit && conf_.exit !== false) process.exit(exitCode);
60-
});
27+
var output = through(null, function () {
28+
if (--count === 0 && !closed) {
29+
closed = true
30+
out.close();
31+
}
6132
});
33+
output.pause();
34+
nextTick(function () { output.resume() });
6235

6336
var test = function (name, conf, cb) {
6437
count++;
6538
var t = new Test(name, conf, cb);
66-
tests.push(t);
67-
if (!conf || typeof conf !== 'object') conf = conf_;
68-
69-
if (conf.exit !== false) {
70-
onexit(function (code) {
71-
t._exit();
72-
if (!closed) {
73-
closed = true
74-
out.close();
75-
}
76-
if (!code && !t._ok && (!only || name === only)) {
77-
exit(1);
78-
}
79-
});
80-
}
81-
82-
nextTick(function () {
83-
if (!out.piped) out.pipe(createDefaultStream());
84-
if (!began) out.begin();
85-
began = true;
86-
87-
var run = function () {
88-
running = true;
89-
out.push(t);
90-
t.run();
91-
};
92-
93-
if (only && name !== only) {
94-
count--;
95-
return;
96-
}
97-
98-
if (running || pending.length) {
99-
pending.push(run);
100-
}
101-
else run();
102-
});
103-
10439
t.on('test', function sub (st) {
105-
count++;
106-
st.on('test', sub);
107-
st.on('end', onend);
40+
console.log('SUBTEST');
41+
});
42+
t.on('result', function (r) {
43+
console.dir(r);
44+
if (!r.ok) exitCode = 1
45+
});
46+
nextTick(function () {
47+
t.run();
10848
});
109-
t.on('result', function (r) { if (!r.ok) exitCode = 1 });
110-
111-
t.on('end', onend);
112-
11349
return t;
114-
115-
function onend () {
116-
count--;
117-
if (this._progeny.length) {
118-
var unshifts = map(this._progeny, function (st) {
119-
return function () {
120-
running = true;
121-
out.push(st);
122-
st.run();
123-
};
124-
});
125-
pending.unshift.apply(pending, unshifts);
126-
}
127-
128-
process.nextTick(function () {
129-
running = false;
130-
if (pending.length) return pending.shift()();
131-
if (count === 0 && !closed) {
132-
closed = true
133-
out.close();
134-
}
135-
if (conf.exit !== false && canExit && !t._ok) {
136-
exit(1);
137-
}
138-
});
139-
}
14050
};
14151

142-
test.only = function (name) {
143-
if (only) {
144-
throw new Error("there can only be one only test");
145-
}
146-
147-
only = name;
148-
149-
return test.apply(null, arguments);
150-
};
151-
152-
test.stream = out;
15352
return test;
15453
}
155-
156-
function map (xs, f) {
157-
if (xs.map) return xs.map(f);
158-
var res = [];
159-
for (var i = 0; i < xs.length; i++) {
160-
res.push(f(xs[i]));
161-
}
162-
return res;
163-
}
164-
165-
// vim: set softtabstop=4 shiftwidth=4:

lib/render.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
var Stream = require('stream');
22
var json = typeof JSON === 'object' ? JSON : require('jsonify');
3+
var through = require('through');
34

4-
module.exports = Render;
5+
module.exports = function (test) {
6+
var out = through();
7+
out.pause();
8+
var r = new Render;
9+
r.pipe(out);
10+
r.push(test);
11+
12+
out.begin = function () { r.begin() };
13+
out.close = function () { r.close() };
14+
return out;
15+
};
516

617
function Render () {
718
Stream.call(this);
@@ -13,11 +24,6 @@ function Render () {
1324

1425
Render.prototype = new Stream;
1526

16-
Render.prototype.pipe = function () {
17-
this.piped = true;
18-
return Stream.prototype.pipe.apply(this, arguments);
19-
};
20-
2127
Render.prototype.begin = function () {
2228
this.emit('data', 'TAP version 13\n');
2329
};
@@ -31,7 +37,7 @@ Render.prototype.push = function (t) {
3137
self.emit('data', '# ' + res + '\n');
3238
return;
3339
}
34-
40+
3541
self.emit('data', encodeResult(res, self.count + 1));
3642
self.count ++;
3743

lib/test.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var EventEmitter = require('events').EventEmitter;
1+
var Stream = require('stream');
22
var deepEqual = require('deep-equal');
33
var defined = require('defined');
44
var path = require('path');
5+
var inherits = require('util').inherits;
6+
var EventEmitter = require('events').EventEmitter;
57

68
module.exports = Test;
79

@@ -10,9 +12,10 @@ var nextTick = typeof setImmediate !== 'undefined'
1012
: process.nextTick
1113
;
1214

13-
Test.prototype = new EventEmitter;
15+
inherits(Test, EventEmitter);
1416

1517
function Test (name_, opts_, cb_) {
18+
var self = this;
1619
var name = '(anonymous)';
1720
var opts = {};
1821
var cb;
@@ -30,8 +33,7 @@ function Test (name_, opts_, cb_) {
3033
}
3134
}
3235

33-
EventEmitter.call(this);
34-
36+
this.readable = true;
3537
this.name = name || '(anonymous)';
3638
this.assertCount = 0;
3739
this._skip = opts.skip || false;
@@ -70,6 +72,13 @@ Test.prototype.plan = function (n) {
7072
};
7173

7274
Test.prototype.end = function () {
75+
var self = this;
76+
if (this._progeny.length) {
77+
var t = this._progeny.shift();
78+
t.on('end', function () { self.end() });
79+
return;
80+
}
81+
7382
if (!this.ended) this.emit('end');
7483
if (this._plan !== undefined &&
7584
!this._planError && this.assertCount !== this._plan) {
@@ -94,7 +103,6 @@ Test.prototype._exit = function () {
94103
else if (!this.ended) {
95104
this.fail('test exited without ending');
96105
}
97-
98106
};
99107

100108
Test.prototype._assert = function assert (ok, opts) {

0 commit comments

Comments
 (0)