5ec1b558be920d4a66cfb1b8de1cf9556b4e91bd.svn-base
8.25 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// A facade from the tap-parser to the Mocha "Runner" object.
// Note that pass/fail/suite events need to also mock the "Runnable"
// objects (either "Suite" or "Test") since these have functions
// which are called by the formatters.
module.exports = Runner
// relevant events:
//
// start()
// Start of the top-level test set
//
// end()
// End of the top-level test set.
//
// fail(test, err)
// any "not ok" test that is not the trailing test for a suite
// of >0 test points.
//
// pass(test)
// any "ok" test point that is not the trailing test for a suite
// of >0 tests
//
// pending(test)
// Any "todo" test
//
// suite(suite)
// A suite is a child test with >0 test points. This is a little bit
// tricky, because TAP will provide a "child" event before we know
// that it's a "suite". We see the "# Subtest: name" comment as the
// first thing in the subtest. Then, when we get our first test point,
// we know that it's a suite, and can emit the event with the mock suite.
//
// suite end(suite)
// Emitted when we end the subtest
//
// test(test)
// Any test point which is not the trailing test for a suite.
//
// test end(test)
// Emitted immediately after the "test" event because test points are
// not async in TAP.
var util = require('util')
var Test = require('./test.js')
var Suite = require('./suite.js')
var Writable = require('stream').Writable
var Parser = require('tap-parser')
// $1 = number, $2 = units
var timere = /^#\s*time=((?:0|[1-9][0-9]*?)(?:\.[0-9]+)?)(ms|s)?$/
util.inherits(Runner, Writable)
function Runner (options) {
if (!(this instanceof Runner))
return new Runner(options)
var parser = this.parser = new Parser(options)
this.startTime = new Date()
attachEvents(this, parser, 0)
Writable.call(this, options)
}
Runner.prototype.write = function () {
if (!this.emittedStart) {
this.emittedStart = true
this.emit('start')
}
return this.parser.write.apply(this.parser, arguments)
}
Runner.prototype.end = function () {
return this.parser.end.apply(this.parser, arguments)
}
Parser.prototype.fullTitle = function () {
if (!this.parent)
return this.name || ''
else
return (this.parent.fullTitle() + ' ' + (this.name || '')).trim()
}
function attachEvents (runner, parser, level) {
parser.runner = runner
if (level === 0) {
parser.on('line', function (c) {
runner.emit('line', c)
})
parser.on('version', function (v) {
runner.emit('version', v)
})
parser.on('complete', function (res) {
runner.emit('end')
})
parser.on('comment', function (c) {
var tmatch = c.trim().match(timere)
if (tmatch) {
var t = +tmatch[1]
if (tmatch[2] === 's')
t *= 1000
parser.time = t
if (runner.stats)
runner.stats.duration = t
}
})
}
parser.emittedSuite = false
parser.didAssert = false
parser.name = parser.name || ''
parser.doingChild = null
parser.on('complete', function (res) {
if (!res.ok) {
var fail = { ok: false, diag: {} }
var count = res.count
if (res.plan) {
var plan = res.plan.end - res.plan.start + 1
if (count !== plan) {
fail.name = 'test count !== plan'
fail.diag = {
found: count,
wanted: plan
}
} else {
// probably handled on child parser
return
}
} else {
fail.name = 'missing plan'
}
fail.diag.results = res
emitTest(parser, fail)
}
})
parser.on('child', function (child) {
child.parent = parser
attachEvents(runner, child, level + 1)
// if we're in a suite, but we haven't emitted it yet, then we
// know that an assert will follow this child, even if there are
// no others. That means that we will definitely have a 'suite'
// event to emit.
emitSuite(this)
this.didAssert = true
this.doingChild = child
})
if (!parser.name) {
parser.on('comment', function (c) {
if (!this.name && c.match(/^# Subtest: /)) {
c = c.trim().replace(/^# Subtest: /, '')
this.name = c
}
})
}
// Just dump all non-parsing stuff to stderr
parser.on('extra', function (c) {
process.stderr.write(c)
})
parser.on('assert', function (result) {
emitSuite(this)
// no need to print the trailing assert for subtests
// we've already emitted a 'suite end' event for this.
// UNLESS, there were no other asserts, AND it's root level
if (this.doingChild) {
var suite = this.doingChild.suite
if (this.doingChild.name === result.name) {
if (suite) {
if (result.time)
suite.duration = result.time
// If it's ok so far, but the ending result is not-ok, then
// that means that it exited non-zero. Emit the test so
// that we can print it as a failure.
if (suite.ok && !result.ok)
emitTest(this, result)
}
}
var emitOn = this
var dc = this.doingChild
this.doingChild = null
if (!dc.didAssert && dc.level === 1) {
emitOn = dc
} else if (dc.didAssert) {
if (dc.suite)
runner.emit('suite end', dc.suite)
return
} else {
emitOn = this
}
emitSuite(emitOn)
emitTest(emitOn, result)
if (emitOn !== this && emitOn.suite) {
runner.emit('suite end', emitOn.suite)
delete emitOn.suite
}
if (dc.suite) {
runner.emit('suite end', dc.suite)
}
return
}
this.didAssert = true
this.doingChild = null
emitTest(this, result)
})
parser.on('complete', function (results) {
this.results = results
})
parser.on('bailout', function (reason) {
var suite = this.suite
runner.emit('bailout', reason, suite)
if (suite)
this.suite = suite.parent
})
// proxy all stream events directly
var streamEvents = [
'pipe', 'prefinish', 'finish', 'unpipe', 'close'
]
streamEvents.forEach(function (ev) {
parser.on(ev, function () {
var args = [ev]
args.push.apply(args, arguments)
runner.emit.apply(runner, args)
})
})
}
function emitSuite (parser) {
if (!parser.emittedSuite && parser.name) {
parser.emittedSuite = true
var suite = parser.suite = new Suite(parser)
if (parser.parent && parser.parent.suite)
parser.parent.suite.suites.push(suite)
if (parser.runner.stats)
parser.runner.stats.suites ++
parser.runner.emit('suite', suite)
}
}
function emitTest (parser, result) {
var runner = parser.runner
var test = new Test(result, parser)
if (parser.suite) {
parser.suite.tests.push(test)
if (!result.ok) {
for (var p = parser; p && p.suite; p = p.parent) {
p.suite.ok = false
}
}
parser.suite.ok = parser.suite.ok && result.ok
}
runner.emit('test', test)
if (result.skip || result.todo) {
runner.emit('pending', test)
} else if (result.ok) {
runner.emit('pass', test)
} else {
var error = getError(result)
runner.emit('fail', test, error)
}
runner.emit('test end', test)
}
function getError (result) {
var err
function reviveStack (stack) {
if (!stack)
return null
return stack.trim().split('\n').map(function (line) {
return ' at ' + line
}).join('\n')
}
if (result.diag && result.diag.error) {
err = {
name: result.diag.error.name || 'Error',
message: result.diag.error.message,
toString: function () {
return this.name + ': ' + this.message
},
stack: result.diag.error.stack
}
} else {
err = {
message: (result.name || '(unnamed error)').replace(/^Error: /, ''),
toString: function () {
return 'Error: ' + this.message
},
stack: result.diag && result.diag.stack
}
}
var diag = result.diag
if (err.stack)
err.stack = err.toString() + '\n' + reviveStack(err.stack)
if (diag) {
var hasFound = diag.hasOwnProperty('found')
var hasWanted = diag.hasOwnProperty('wanted')
var hasDiff = diag.hasOwnProperty('diff')
if (hasDiff)
err.diff = diag.diff
if (hasFound)
err.actual = diag.found
if (hasWanted)
err.expected = diag.wanted
if (hasFound && hasWanted || hasDiff)
err.showDiff = true
}
return err
}