a7bbf9f2f5e0eeafc0ec2c30c70f7114c4ed38fd.svn-base
5.99 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
'use strict'
const Test = require('./test.js')
const Stdin = require('./stdin.js')
const Spawn = require('./spawn.js')
const util = require('util')
const objToYaml = require('./obj-to-yaml.js')
const _didPipe = Symbol('_didPipe')
const _unmagicPipe = Symbol('_unmagicPipe')
const Domain = require('async-hook-domain')
let processPatched = false
const rootDomain = new Domain((er, type) => {
if (!processPatched)
throw er
else {
if (!er || typeof er !== 'object')
er = { error: er }
er.tapCaught = type
tap.threw(er)
}
})
/* istanbul ignore next */
if (!process.stdout) {
// Set exitCode in case process.stderr is also invalid
process.exitCode = 1
console.error('Output stream is invalid')
process.exit(1)
}
const monkeypatchEpipe = () => {
const emit = process.stdout.emit
process.stdout.emit = function (ev, er) {
if (ev !== 'error' || er.code !== 'EPIPE')
return emit.apply(process.stdout, arguments)
}
}
const monkeypatchExit = () => {
const exit = process.exit
const reallyExit = process.reallyExit
// ensure that we always get run, even if a user does
// process.on('exit', process.exit)
process.reallyExit = code => reallyExit.call(process, onExitEvent(code))
process.exit = code => exit.call(process, onExitEvent(code))
process.on('beforeExit', onExitEvent)
process.on('exit', onExitEvent)
}
class TAP extends Test {
constructor (options) {
super(options)
this.runOnly = process.env.TAP_ONLY === '1'
this.childId = +process.env.TAP_CHILD_ID
|| /* istanbul ignore next */ 0
this.start = Date.now()
this[_didPipe] = false
}
resume () {
this[_unmagicPipe]()
const ret = this.resume.apply(this, arguments)
this.process()
return ret
}
[_unmagicPipe] () {
this[_didPipe] = true
this.setTimeout(this.options.timeout)
this.pipe = Test.prototype.pipe
this.write = Test.prototype.write
this.resume = Test.prototype.resume
}
setTimeout (n, quiet) {
if (n && typeof n === 'number' && !quiet)
this.write(`# timeout=${n}\n`)
return super.setTimeout(n)
}
pipe () {
this[_unmagicPipe]()
const ret = this.pipe.apply(this, arguments)
this.process()
return ret
}
write (c, e) {
// this resets write and pipe to standard values
this.patchProcess()
this.pipe(process.stdout)
return super.write(c, e)
}
patchProcess () {
if (processPatched)
return
processPatched = true
monkeypatchEpipe()
monkeypatchExit()
}
onbeforeend () {
if (this[_didPipe] && this.time && !this.bailedOut)
this.emit('data', '# time=' + this.time + 'ms\n')
}
ondone () {
return this.emitSubTeardown(this)
}
// Root test runner doesn't have the 'teardown' event, because it
// isn't hooked into any parent Test as a harness.
teardown (fn) {
if (this.options.autoend !== false)
this.autoend(true)
return Test.prototype.teardown.apply(this, arguments)
}
tearDown (fn) {
return this.teardown(fn)
}
}
let didOnExitEvent = false
const onExitEvent = code => {
if (didOnExitEvent)
return process.exitCode
didOnExitEvent = true
if (!tap.results)
tap.endAll()
if (tap.results && !tap.results.ok && code === 0)
process.exitCode = 1
return process.exitCode || code || 0
}
const opt = { name: 'TAP' }
if (process.env.TAP_DEBUG === '1' ||
/\btap\b/.test(process.env.NODE_DEBUG || ''))
opt.debug = true
if (process.env.TAP_GREP) {
opt.grep = process.env.TAP_GREP.split('\n').map(g => {
const p = g.match(/^\/(.*)\/([a-z]*)$/)
g = p ? p[1] : g
const flags = p ? p[2] : ''
return new RegExp(g, flags)
})
}
if (process.env.TAP_GREP_INVERT === '1')
opt.grepInvert = true
if (process.env.TAP_ONLY === '1')
opt.only = true
const tap = new TAP(opt)
module.exports = tap.default = tap.t = tap
tap.mocha = require('./mocha.js')
tap.mochaGlobals = () => tap.mocha.global()
tap.Test = Test
tap.Spawn = Spawn
tap.Stdin = Stdin
tap.synonyms = require('./synonyms.js')
// SIGTERM means being forcibly killed, almost always by timeout
const onExit = require('signal-exit')
let didTimeoutKill = false
onExit((code, signal) => {
if (signal !== 'SIGTERM' || !tap[_didPipe] || didTimeoutKill)
return
const handles = process._getActiveHandles().filter(h =>
h !== process.stdout &&
h !== process.stdin &&
h !== process.stderr
)
const requests = process._getActiveRequests()
// Ignore this because it's really hard to test cover in a way
// that isn't inconsistent and unpredictable.
/* istanbul ignore next */
const extra = {
at: null,
signal: signal
}
if (requests.length) {
extra.requests = requests.map(r => {
const ret = {}
ret.type = r.constructor.name
// most everything in node has a context these days
/* istanbul ignore else */
if (r.context)
ret.context = r.context
return ret
})
}
// Newer node versions don't have this as reliably.
/* istanbul ignore next */
if (handles.length) {
extra.handles = handles.map(h => {
const ret = {}
ret.type = h.constructor.name
// all of this is very internal-ish
/* istanbul ignore next */
if (h.msecs)
ret.msecs = h.msecs
/* istanbul ignore next */
if (h._events)
ret.events = Object.keys(h._events)
/* istanbul ignore next */
if (h._sockname)
ret.sockname = h._sockname
/* istanbul ignore next */
if (h._connectionKey)
ret.connectionKey = h._connectionKey
return ret
})
}
// this is impossible to cover, because it happens after nyc has
// already done its stuff.
/* istanbul ignore else */
if (!tap.results && tap.timeout)
tap.timeout(extra)
else {
console.error('possible timeout: SIGTERM received after tap end')
if (extra.handles || extra.requests) {
delete extra.signal
if (!extra.at) {
delete extra.at
}
console.error(objToYaml(extra))
}
didTimeoutKill = true
process.kill(process.pid, 'SIGTERM')
}
})