582fea7b3f6bf571dc78268df1c742ea2782948a.svn-base
8.51 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
343
344
345
346
'use strict'
const MiniPass = require('minipass')
const extraFromError = require('./extra-from-error.js')
const assert = require('assert')
const Domain = require('async-hook-domain')
const {AsyncResource} = require('async_hooks')
const util = require('util')
class TapWrap extends AsyncResource {
constructor (test) {
super('tap.' + test.constructor.name)
this.test = test
// Polyfill for Node.js 8 and before
/* istanbul ignore next */
if (!this.runInAsyncScope)
this.runInAsyncScope = (fn, thisArg, ...args) => {
this.emitBefore()
try {
return Reflect.apply(fn, thisArg, args)
} finally {
this.emitAfter()
}
}
}
}
/* istanbul ignore next */
const INSPECT = util.inspect.custom || 'inspect'
const Parser = require('tap-parser')
const ownOr = require('own-or')
const ownOrEnv = require('own-or-env')
const hrtime = require('browser-process-hrtime')
class Base extends MiniPass {
constructor (options) {
options = options || {}
super(options)
this.started = false
// establish the wrapper resource to limit the domain to this one object
this.hook = new TapWrap(this)
this.hook.runInAsyncScope(() =>
this.hookDomain = new Domain((er, type) => {
if (!er || typeof er !== 'object')
er = { error: er }
er.tapCaught = type
this.threw(er)
}))
this.start = 0
this.hrtime = null
this.time = null
this.timer = null
this.readyToProcess = false
this.options = options
this.grep = ownOr(options, 'grep', [])
this.grepInvert = ownOr(options, 'grepInvert', false)
this.parent = ownOr(options, 'parent', null)
this.bail = ownOrEnv(options, 'bail', 'TAP_BAIL', true)
this.saveFixture = ownOrEnv(options, 'saveFixture', 'TAP_SAVE_FIXTURE', true)
const name = (ownOr(options, 'name', '') || '').replace(/[\n\r\s\t]/g, ' ')
Object.defineProperty(this, 'name', {
value: name,
writable: false,
enumerable: true,
configurable: false,
})
this.indent = ownOr(options, 'indent', '')
this.silent = !!options.silent
this.buffered = !!options.buffered || !!options.silent
this.finished = false
this.strict = ownOrEnv(options, 'strict', 'TAP_STRICT', true)
this.omitVersion = !!options.omitVersion
this.preserveWhitespace = ownOr(options, 'preserveWhitespace', true)
this.jobs = +ownOrEnv(options, 'jobs', 'TAP_JOBS') || 0
this.runOnly = ownOrEnv(options, 'runOnly', 'TAP_ONLY', true)
this.setupParser(options)
this.finished = false
this.output = ''
this.results = null
this.bailedOut = false
this.childId = +ownOrEnv(options, 'childId', 'TAP_CHILD_ID')
|| /* istanbul ignore next */ 0
const skip = ownOr(options, 'skip', false)
const todo = ownOr(options, 'todo', false)
if (skip || todo)
this.main = Base.prototype.main
this.counts = {
total: 0,
pass: 0,
fail: 0,
skip: 0,
todo: 0,
}
const ctx = ownOr(options, 'context', null)
delete options.context
this.context = typeof ctx === 'object' || ctx instanceof Object
? Object.create(ctx) : ctx
this.lists = {
fail: [],
todo: [],
skip: [],
}
const doDebug = typeof options.debug === 'boolean' ? options.debug
: /\btap\b/i.test(process.env.NODE_DEBUG || '')
if (doDebug)
this.debug = debug(this.name)
}
passing () {
return this.parser.ok
}
setTimeout (n, quiet) {
if (!this.hrtime)
this.hrtime = hrtime()
if (!this.start)
this.start = Date.now()
if (!n) {
clearTimeout(this.timer)
this.timer = null
} else {
if (this.timer)
clearTimeout(this.timer)
this.timer = setTimeout(() => this.timeout(), n)
/* istanbul ignore else */
if (this.timer.unref) {
this.timer.duration = n
this.timer.unref()
}
}
}
threw (er, extra, proxy) {
this.hook.emitDestroy()
this.hookDomain.destroy()
if (!er || typeof er !== 'object')
er = { error: er }
if (this.name && !proxy)
er.test = this.name
const message = er.message
if (!extra)
extra = extraFromError(er, extra, this.options)
if (this.results) {
this.results.ok = false
if (this.parent)
this.parent.threw(er, extra, true)
else if (!er.stack)
console.error(er)
else {
if (message)
er.message = message
delete extra.stack
delete extra.at
console.error('%s: %s', er.name || 'Error', message)
console.error(er.stack.split(/\n/).slice(1).join('\n'))
console.error(extra)
}
} else
this.parser.ok = false
return extra
}
timeout (options) {
this.setTimeout(false)
const er = new Error('timeout!')
options = options || {}
options.expired = options.expired || this.name
this.emit('timeout', this.threw(new Error('timeout!'), options))
}
runMain (cb) {
this.started = true
this.hook.runInAsyncScope(this.main, this, cb)
}
main (cb) {
cb()
}
online (line) {
this.debug('LINE %j', line)
return this.write(this.indent + line)
}
write (c, e) {
assert.equal(typeof c, 'string')
assert.equal(c.substr(-1), '\n')
if (this.buffered) {
this.output += c
return true
}
return super.write(c, e)
}
onbail (reason) {
this.bailedOut = reason || true
this.emit('bailout', reason)
}
oncomplete (results) {
if (this.hrtime) {
this.hrtime = hrtime(this.hrtime)
this.time = results.time ||
Math.round(this.hrtime[0] * 1e6 + this.hrtime[1] / 1e3) / 1e3
}
this.debug('ONCOMPLETE %j %j', this.name, results)
if (this.results)
Object.keys(this.results)
.forEach(k => results[k] = this.results[k])
this.results = results
this.emit('complete', results)
const failures = results.failures
.filter(f => f.tapError)
.map(f => {
delete f.diag
delete f.ok
return f
})
if (failures.length)
this.options.failures = failures
this.onbeforeend()
// if we're piping, and buffered, then it means we need to hold off
// on emitting 'end' and calling ondone() until the pipes clear out.
if (this.pipes.length && this.buffer.length)
super.end()
else
this.emit('end')
}
onbeforeend () {}
ondone () {}
emit (ev, data) {
if (ev === 'end') {
const ret = super.emit(ev, data)
this.ondone()
this.hook.emitDestroy()
this.hookDomain.destroy()
return ret
} else
return super.emit(ev, data)
}
setupParser (options) {
this.parser = options.parser || new Parser({
bail: this.bail,
strict: this.strict,
omitVersion: this.omitVersion,
preserveWhitespace: this.preserveWhitespace,
name: this.name,
})
this.parser.on('line', l => this.online(l))
this.parser.once('bailout', reason => this.onbail(reason))
this.parser.on('complete', result => this.oncomplete(result))
this.parser.on('result', () => this.counts.total++)
this.parser.on('pass', res => this.counts.pass++)
this.parser.on('todo', res => {
this.counts.todo++
this.lists.todo.push(res)
})
this.parser.on('skip', res => {
this.counts.skip++
this.lists.skip.push(res)
})
this.parser.on('fail', res => {
this.counts.fail++
this.lists.fail.push(res)
})
}
[INSPECT] () {
return this.constructor.name + ' ' + util.inspect({
name: this.name,
time: this.time,
hrtime: this.hrtime,
jobs: this.jobs,
buffered: this.buffered,
occupied: this.occupied,
pool: this.pool,
queue: this.queue,
subtests: this.subtests,
output: this.output,
skip: ownOr(this.options, 'skip', false),
todo: ownOr(this.options, 'todo', false),
only: ownOr(this.options, 'only', false),
results: this.results,
options: [
'autoend',
'command',
'args',
'stdio',
'env',
'cwd',
'exitCode',
'signal',
'expired',
'timeout',
'at',
'skip',
'todo',
'only',
'runOnly'
].filter(k => this.options[k] !== undefined)
.reduce((s, k) => (s[k] = this.options[k], s), {})
})
}
debug () {}
}
const debug = name => (...args) => {
const prefix = `TAP ${process.pid} ${name}: `
const msg = util.format(...args).trim()
console.error(prefix + msg.split('\n').join(`\n${prefix}`))
}
module.exports = Base