This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
/
harness.js
182 lines (155 loc) · 5.11 KB
/
harness.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// a thing that runs tests.
// Every "test" is also a harness. If they do not have a harness,
// then they are attached to the defaut "global harness",
// which writes its results to stdout.
// TODO:
// - Bailout should stop running any tests.
// - "skip" in the test config obj should skip it.
module.exports = Harness
require("inherits")(Harness, require("events").EventEmitter)
var Results = require("tap-results")
, TapProducer = require("tap-producer")
, assert = require("tap-assert")
function Harness (Test) {
if (!(this instanceof Harness)) return new Harness(Test)
this._Test = Test
this._plan = null
this._children = []
this._started = false
this._testCount = 0
this._planSum = 0
this.results = new Results()
var p = this.process.bind(this)
this.process = function () {
this._started = true
process.nextTick(p)
}
this.output = new TapProducer()
Harness.super.call(this)
}
// this function actually only gets called bound to
// the Harness object, and on process.nextTick. Even if
// passed as an event handler, everything *else* will
// happen before it gets called.
Harness.prototype.process = function () {
//console.error("harness process")
// "end" can emit multiple times, so only actually move on
// to the next test if the current one is actually over.
// TODO: multiple in-process tests, if all are marked "async"
if (this._current) {
if (!this._current._ended) return
// handle the current one before moving onto the next.
this.childEnd(this._current)
}
var skip = true
while (skip) {
//console.error("checking for skips")
var current = this._current = this._children.shift()
if (current) {
skip = current.conf.skip
if (skip) {
//console.error("add a failure for the skipping")
this.results.add(assert.fail(current.conf.name
,{skip:true, diag:false}))
}
} else skip = false
}
// keep processing through skipped tests, instead of running them.
if (current && this._bailedOut) {
return this.process()
}
//console.error("got current?", !!current)
if (current) {
current.on("end", this.process)
current.emit("ready")
//console.error("emitted ready")
//console.error("_plan", this._plan, this.constructor.name)
} else {
//console.error("Harness process: no more left. ending")
this.end()
}
}
Harness.prototype.end = function () {
//console.error("harness end", this.constructor.name)
if (this._bailedOut) return
// can't call .end() more than once.
if (this._ended) {
//console.error("adding failure for end calling")
this.results.add(assert.fail("end called more than once"))
}
// see if the plan is completed properly, if there was one.
if (this._plan !== null) {
var total = this._testCount
if (total !== this._plan) {
this.results.add(assert.equal(total, this._plan, "test count != plan"))
}
this._plan = total
}
//console.error("setting ended true", this.constructor.name)
this._ended = true
this.emit("end")
}
Harness.prototype.plan = function (p) {
//console.error("setting plan", new Error().stack)
if (this._plan !== null) {
//console.error("about to add failure for calling plan")
return this.results.add(assert.fail("plan set multiple times"))
}
this._plan = p
if (p === 0 || this.results.testsTotal) {
this.end()
}
}
Harness.prototype.childEnd = function (child) {
//console.error("childEnd")
this._testCount ++
this._planSum += child._plan
//console.error("adding set of child.results")
this.results.addSet(child.results)
this.emit("childEnd", child)
// was this planned?
if (this._plan === this._testCount) {
//console.error("plan", [this._plan, this._testCount])
return this.end()
}
}
Harness.prototype.test = function test (name, conf, cb) {
if (this._bailedOut) return
if (typeof conf === "function") cb = conf, conf = null
if (typeof name === "object") conf = name, name = null
if (typeof name === "function") cb = name, name = null
conf = conf || {}
name = name || ""
//console.error("making test", [name, conf, cb])
// timeout: value in milliseconds. Defaults to 30s
// Set to Infinity to have no timeout.
if (isNaN(conf.timeout)) conf.timeout = 30000
var t = new this._Test(this, name, conf)
if (cb) {
if (!isNaN(conf.timeout) && isFinite(conf.timeout)) {
var timer = setTimeout(t.timeout.bind(t), conf.timeout)
t.on("end", clearTimeout.bind(null, timer))
}
//console.error("attaching cb to ready event")
t.on("ready", cb.bind(t, t))
t.on("result", this.emit.bind(this))
}
return t
}
Harness.prototype.bailout = function (message) {
message = message || ""
//console.error("adding bailout message result")
this.results.add({bailout: message})
this._bailedOut = true
this.emit("bailout", message)
}
Harness.prototype.add = function (child) {
//console.error("adding child")
this._children.push(child)
if (!this._started) this.process()
}
// the tearDown function is *always* guaranteed to happen.
// Even if there's a bailout.
Harness.prototype.tearDown = function (fn) {
this.on("end", fn)
}