File: base.js

package info (click to toggle)
node-tap 12.0.1+ds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,916 kB
  • sloc: makefile: 84; sh: 75
file content (247 lines) | stat: -rw-r--r-- 6,108 bytes parent folder | download | duplicates (2)
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
'use strict'

const MiniPass = require('stream').PassThrough

const extraFromError = require('./extra-from-error.js')
const assert = require('assert')
const cleanYamlObject = require('./clean-yaml-object.js')

const domain = require('domain')
const util = require('util')

/* 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')

class Base extends MiniPass {
  constructor (options) {
    options = options || {}
    super(options)
    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.name = ownOr(options, 'name', '')
    if (!this.name)
      this.name = ''
    else
      this.name = this.name.replace(/[\n\r\s\t]/g, ' ')
    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 = ownOr(options, 'runOnly', false)
    this.setupParser(options)
    this.finished = false
    this.output = ''
    this.results = null
    this.bailedOut = false
    const skip = ownOr(options, 'skip', false)
    const todo = ownOr(options, 'todo', false)
    if (skip || todo)
      this.main = Base.prototype.main

    domain.create().add(this)
    this.domain.on('error', er => this.threw(er))

    const doDebug = typeof options.debug === 'boolean' ? options.debug
      : /\btap\b/i.test(process.env.NODE_DEBUG || '')

    if (doDebug)
      this.debug = debug
  }

  passing () {
    return this.parser.ok
  }

  setTimeout (n) {
    if (!this.hrtime)
      this.hrtime = process.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.unref()
    }
  }

  threw (er, extra, proxy) {
    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))
  }

  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 = process.hrtime(this.hrtime)
      this.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()
    this.emit('end')
    this.ondone()
  }

  onbeforeend () {}
  ondone () {}

  setupParser (options) {
    this.parser = new Parser({
      bail: this.bail,
      strict: this.strict,
      omitVersion: this.omitVersion,
      preserveWhitespace: this.preserveWhitespace
    })
    this.parser.on('line', l => this.online(l))
    this.parser.once('bailout', reason => this.onbail(reason))
    this.parser.on('complete', result => this.oncomplete(result))
  }

  [INSPECT] () {
    return this.constructor.name + ' ' + util.inspect({
      name: this.name,
      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 () {}

}

function debug () {
  const prefix = 'TAP ' + process.pid + ' ' + this.name + ': '
  const msg = util.format.apply(util, arguments).trim()
  console.error(prefix + msg.split('\n').join('\n' + prefix))
}

module.exports = Base