[Pkg-javascript-commits] [node-tap] 84/186: unit tests for tap global

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:46 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-tap.

commit 3df105a8a115b64dcce141775fd56a55be792ad8
Author: isaacs <i at izs.me>
Date:   Fri Nov 10 17:10:08 2017 -0800

    unit tests for tap global
---
 lib/tap.js                            |  21 +-
 lib/test.js                           |   7 +-
 tap-snapshots/unit-tap.js-TAP.test.js | 474 ++++++++++++++++++++++++++++++++++
 unit/tap.js                           | 151 +++++++++++
 4 files changed, 643 insertions(+), 10 deletions(-)

diff --git a/lib/tap.js b/lib/tap.js
index 31b54c4..67effd2 100644
--- a/lib/tap.js
+++ b/lib/tap.js
@@ -126,13 +126,11 @@ if (process.env.TAP_GREP) {
   })
 }
 
-if (process.env.TAP_GREP_INVERT === '1') {
+if (process.env.TAP_GREP_INVERT === '1')
   opt.grepInvert = true
-}
 
-if (process.env.TAP_ONLY === '1') {
+if (process.env.TAP_ONLY === '1')
   opt.only = true
-}
 
 const tap = new TAP(opt)
 module.exports = tap
@@ -167,25 +165,36 @@ onExit((code, signal) => {
   }
   if (requests.length) {
     extra.requests = requests.map(r => {
-      const ret = { type: r.constructor.name }
+      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
     })
   }
   if (handles.length) {
     extra.handles = handles.map(h => {
-      const ret = { type: h.constructor.name }
+      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
 
diff --git a/lib/test.js b/lib/test.js
index 85ae72a..f86508e 100644
--- a/lib/test.js
+++ b/lib/test.js
@@ -78,11 +78,8 @@ class Test extends Base {
       options, 'snapshot', 'TAP_SNAPSHOT', true)
     if (this.parent && this.parent[_snapshot])
       this[_snapshot] = this.parent[_snapshot]
-    else {
+    else
       this[_snapshot] = new Snapshot(this)
-      if (this.writeSnapshot)
-        this.teardown(_ => this[_snapshot].save())
-    }
 
     this.noparallel = false
     if (options.cb)
@@ -302,6 +299,8 @@ class Test extends Base {
       } else if (p === EOF) {
         this.debug(' > EOF', this.name)
         // I AM BECOME EOF, DESTROYER OF STREAMS
+        if (this.writeSnapshot)
+          this[_snapshot].save()
         this.parser.end()
       } else if (p instanceof TestPoint) {
         this.debug(' > TESTPOINT')
diff --git a/tap-snapshots/unit-tap.js-TAP.test.js b/tap-snapshots/unit-tap.js-TAP.test.js
new file mode 100644
index 0000000..56344c1
--- /dev/null
+++ b/tap-snapshots/unit-tap.js-TAP.test.js
@@ -0,0 +1,474 @@
+'use strict'
+exports[`unit/tap.js TAP ok > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP ok > stdout 1`] = `
+TAP version 13
+ok 1 - fine
+1..1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP ok > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP notOk > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP notOk > stdout 1`] = `
+TAP version 13
+not ok 1 - expected
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/tap.js
+    function: notOk
+  stack: |
+    {STACK}
+  source: |
+    notOk: t => t.fail('expected'),
+  ...
+
+1..1
+# failed 1 test
+# {time}
+
+`
+
+exports[`unit/tap.js TAP notOk > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP bail > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP bail > stdout 1`] = `
+TAP version 13
+Bail out! cannot proceed
+
+`
+
+exports[`unit/tap.js TAP bail > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP plan 0 > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP plan 0 > stdout 1`] = `
+TAP version 13
+1..0 # skip it all
+# {time}
+
+`
+
+exports[`unit/tap.js TAP plan 0 > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP plan unsatisied > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP plan unsatisied > stdout 1`] = `
+TAP version 13
+1..99
+# test count(0) != plan(99)
+# failed 1 test
+# {time}
+
+`
+
+exports[`unit/tap.js TAP plan unsatisied > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP too much > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP too much > stdout 1`] = `
+TAP version 13
+1..1
+ok 1 - a little
+# {time}
+
+`
+
+exports[`unit/tap.js TAP too much > stderr 1`] = `
+
+Error: test count exceeds plan
+    {STACK}
+{ name: 'TAP', test: 'TAP', plan: 1 }
+
+`
+
+exports[`unit/tap.js TAP stdout epipe > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP stdout epipe > stdout 1`] = `
+TAP version 13
+ok 1 - this is fine
+1..1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP stdout epipe > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP close even if exiting hard > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP close even if exiting hard > stdout 1`] = `
+TAP version 13
+ok 1 - make sure, really
+1..1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP close even if exiting hard > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP unhandled promise > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP unhandled promise > stdout 1`] = `
+TAP version 13
+ok 1 - fine, i promise
+not ok 2 - broken
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/tap.js
+    function: unhandled promise
+  stack: |
+    {STACK}
+  test: TAP
+  source: |
+    Promise.reject(new Error('broken'))
+  ...
+
+1..2
+# failed 1 of 2 tests
+# {time}
+
+`
+
+exports[`unit/tap.js TAP unhandled promise > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP teardown event loop > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP teardown event loop > stdout 1`] = `
+TAP version 13
+ok 1 - fine
+1..1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP teardown event loop > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP teardown throw > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP teardown throw > stdout 1`] = `
+TAP version 13
+ok 1 - x
+1..1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP teardown throw > stderr 1`] = `
+
+Error: poop
+    {STACK}
+{ name: 'TAP', test: 'TAP' }
+
+`
+
+exports[`unit/tap.js TAP process.exitCode polyfill > exit status 1`] = `
+{ code: 1, signal: null }
+`
+
+exports[`unit/tap.js TAP process.exitCode polyfill > stdout 1`] = `
+TAP version 13
+not ok 1 - v0.10.420
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/tap.js
+    function: process.exitCode polyfill
+  stack: |
+    {STACK}
+  source: |
+    t.fail(process.version)
+  ...
+
+1..1
+# failed 1 test
+# {time}
+
+`
+
+exports[`unit/tap.js TAP process.exitCode polyfill > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP TAP_DEBUG=1 > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP TAP_DEBUG=1 > stdout 1`] = `
+TAP version 13
+# this is fine
+1..0
+# {time}
+
+`
+
+exports[`unit/tap.js TAP TAP_DEBUG=1 > stderr 1`] = `
+TAP {PID} TAP: PROCESSING(TAP) 2
+TAP {PID} TAP: > STRING
+TAP {PID} TAP: LINE "TAP version 13\\n"
+TAP {PID} TAP: < already processing
+TAP {PID} TAP: > STRING
+TAP {PID} TAP: LINE "# this is fine\\n"
+TAP {PID} TAP: done processing [] false
+TAP {PID} TAP: PROCESSING(TAP) 1
+TAP {PID} TAP: > METHOD
+TAP {PID} TAP: END implicit=true
+TAP {PID} TAP: END(TAP) implicit plan 0
+TAP {PID} TAP: END implicit=true
+TAP {PID} TAP: < already processing
+TAP {PID} TAP: > STRING
+TAP {PID} TAP: LINE "1..0\\n"
+TAP {PID} TAP: > EOF TAP
+TAP {PID} TAP: ONCOMPLETE "TAP" {"ok":true,"count":0,"pass":0,"fail":0,"bailout":false,"todo":0,"skip":0,"plan":{"start":1,"end":0,"skipAll":true,"skipReason":"","comment":""},"failures":[]}
+TAP {PID} TAP: done processing [] false
+
+`
+
+exports[`unit/tap.js TAP NODE_DEBUG=tap > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP NODE_DEBUG=tap > stdout 1`] = `
+TAP version 13
+1..0
+# {time}
+
+`
+
+exports[`unit/tap.js TAP NODE_DEBUG=tap > stderr 1`] = `
+TAP {PID} TAP: END implicit=true
+TAP {PID} TAP: PROCESSING(TAP) 3
+TAP {PID} TAP: > STRING
+TAP {PID} TAP: LINE "TAP version 13\\n"
+TAP {PID} TAP: < already processing
+TAP {PID} TAP: > STRING
+TAP {PID} TAP: LINE "1..0\\n"
+TAP {PID} TAP: > METHOD
+TAP {PID} TAP: END implicit=true
+TAP {PID} TAP: < already processing
+TAP {PID} TAP: > EOF TAP
+TAP {PID} TAP: ONCOMPLETE "TAP" {"ok":true,"count":0,"pass":0,"fail":0,"bailout":false,"todo":0,"skip":0,"plan":{"start":1,"end":0,"skipAll":true,"skipReason":"","comment":""},"failures":[]}
+TAP {PID} TAP: done processing [] false
+
+`
+
+exports[`unit/tap.js TAP TAP_GREP > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP TAP_GREP > stdout 1`] = `
+TAP version 13
+# Subtest: axo
+    ok 1 - yellow # SKIP filter: /^y$/i
+    # Subtest: Y
+        1..0
+    ok 2 - Y # {time}
+    
+    # Subtest: y
+        # Subtest: this too
+            1..0
+        ok 1 - this too # {time}
+        
+        1..1
+    ok 3 - y # {time}
+    
+    1..3
+    # skip: 1
+ok 1 - axo # {time}
+
+ok 2 - nope # SKIP filter: /x/
+1..2
+# skip: 1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP TAP_GREP > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP TAP_GREP_INVERT > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP TAP_GREP_INVERT > stdout 1`] = `
+TAP version 13
+# Subtest: yes this one
+    ok 1 - Y # SKIP filter out: /^y$/i
+    # Subtest: yellow
+        1..0
+    ok 2 - yellow # {time}
+    
+    # Subtest: apple
+        # Subtest: this too
+            1..0
+        ok 1 - this too # {time}
+        
+        1..1
+    ok 3 - apple # {time}
+    
+    1..3
+    # skip: 1
+ok 1 - yes this one # {time}
+
+ok 2 - axo # SKIP filter out: /x/
+1..2
+# skip: 1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP TAP_GREP_INVERT > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP TAP_ONLY > exit status 1`] = `
+{ code: 0, signal: null }
+`
+
+exports[`unit/tap.js TAP TAP_ONLY > stdout 1`] = `
+TAP version 13
+# Subtest: only this one
+    1..0
+ok 1 - only this one # {time}
+
+ok 2 - not this one # SKIP filter: only
+1..2
+# skip: 1
+# {time}
+
+`
+
+exports[`unit/tap.js TAP TAP_ONLY > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP timeout sigterm > exit status 1`] = `
+{ code: null, signal: 'SIGTERM' }
+`
+
+exports[`unit/tap.js TAP timeout sigterm > stdout 1`] = `
+TAP version 13
+ok 1 - fine
+not ok 2 - timeout!
+  ---
+  signal: SIGTERM
+  expired: TAP
+  stack: |
+    {STACK}
+  test: TAP
+  ...
+
+1..2
+# failed 1 of 2 tests
+# {time}
+
+`
+
+exports[`unit/tap.js TAP timeout sigterm > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP timeout sigterm with handle > exit status 1`] = `
+{ code: null, signal: 'SIGTERM' }
+`
+
+exports[`unit/tap.js TAP timeout sigterm with handle > stdout 1`] = `
+TAP version 13
+ok 1 - fine
+not ok 2 - timeout!
+  ---
+  signal: SIGTERM
+  handles:
+    - type: Timer
+  expired: TAP
+  stack: |
+    {STACK}
+  test: TAP
+  ...
+
+1..2
+# failed 1 of 2 tests
+# {time}
+
+`
+
+exports[`unit/tap.js TAP timeout sigterm with handle > stderr 1`] = `
+
+`
+
+exports[`unit/tap.js TAP timeout sigterm many times > exit status 1`] = `
+{ code: null, signal: 'SIGTERM' }
+`
+
+exports[`unit/tap.js TAP timeout sigterm many times > stdout 1`] = `
+TAP version 13
+ok 1 - fine
+not ok 2 - timeout!
+  ---
+  signal: SIGTERM
+  requests:
+    {REQUESTS}
+  expired: TAP
+  stack: |
+    {STACK}
+  test: TAP
+  ...
+
+1..2
+# failed 1 of 2 tests
+# {time}
+
+`
+
+exports[`unit/tap.js TAP timeout sigterm many times > stderr 1`] = `
+
+`
diff --git a/unit/tap.js b/unit/tap.js
index e69de29..39e53dd 100644
--- a/unit/tap.js
+++ b/unit/tap.js
@@ -0,0 +1,151 @@
+'use strict'
+const node = process.execPath
+
+const clean = out => out
+  .replace(/(^|\n)TAP [0-9]+ /g, '$1TAP {PID} ')
+  .replace(
+    /\n((?:  )+)requests:\n(\1  - type:(.*)\n\1 {4}context:\n(\1 {6}.*\n)+)+/g,
+    '\n$1requests:\n$1  {REQUESTS}\n')
+  .replace(/\bfd: [0-9]+/g, 'fd: {FD}')
+  .replace(/# time=[0-9\.]+m?s( \{.*)?\n/g, '# {time}$1\n')
+  .replace(/\n(( {2})+)stack: \|-?\n((\1  .*).*\n)+/gm,
+    '\n$1stack: |\n$1  {STACK}\n')
+  .replace(/\n(( {2})+)stack: \>-?\n((\1  .*).*\n(\1\n)?)+/gm,
+    '\n$1stack: |\n$1  {STACK}\n')
+  .replace(/(?:\n|^)([a-zA-Z]*Error): (.*)\n((    at .*\n)*)+/gm,
+    '\n$1: $2\n    {STACK}\n')
+  .replace(/:[0-9]+:[0-9]+(\)?\n)/g, '#:#$1')
+  .replace(/(line|column): [0-9]+/g, '$1: #')
+  .split(process.cwd()).join('{CWD}')
+
+const cases = {
+  ok: t => t.pass('fine'),
+  notOk: t => t.fail('expected'),
+  bail: t => t.bailout('cannot proceed'),
+  'plan 0': t => t.plan(0, 'skip it all'),
+  'plan unsatisied': t => t.plan(99),
+  'too much': t => {
+    t.plan(1)
+    t.pass('a little')
+    t.pass('a lot')
+  },
+  'stdout epipe': t => {
+    t.pass('this is fine')
+    const er = new Error('fake pipe')
+    er.code = 'EPIPE'
+    process.stdout.emit('some other event')
+    setTimeout(() => process.stdout.emit('error', er))
+  },
+  'close even if exiting hard': t => {
+    process.on('exit', (code) => process.exit(code))
+    t.pass('make sure, really')
+  },
+  'unhandled promise': t => {
+    t.pass('fine, i promise')
+    Promise.reject(new Error('broken'))
+  },
+  'teardown event loop': t => {
+    t.pass('fine')
+    const interval = setInterval(() => {}, 10000)
+    t.tearDown(() => clearInterval(interval))
+  },
+  'teardown throw': t => {
+    t.on('teardown', () => { throw new Error('poop') })
+    t.pass('x')
+  },
+  'process.exitCode polyfill': t => {
+    Object.defineProperty(process, 'version', { value: 'v0.10.420' })
+    t.fail(process.version)
+  },
+  'TAP_DEBUG=1': [
+    () => process.env.TAP_DEBUG = '1',
+    t => t.comment('this is fine')
+  ],
+  'NODE_DEBUG=tap': [
+    () => process.env.NODE_DEBUG = 'tap',
+    t => t.plan(0)
+  ],
+  TAP_GREP: [
+    () => process.env.TAP_GREP = 'x\n/^y$/i',
+    t => {
+      t.test('axo', t => {
+        t.test('yellow', t => Promise.reject(new Error('no')))
+        t.test('Y', t => t.end())
+        return t.test('y', t => t.test('this too', t => t.end()))
+      })
+      t.test('nope', t => Promise.reject(new Error('no')))
+    }
+  ],
+  TAP_GREP_INVERT: [
+    () => {
+      process.env.TAP_GREP = 'x\n/^y$/i'
+      process.env.TAP_GREP_INVERT = 1
+    },
+    t => {
+      t.test('yes this one', t => {
+        t.test('Y', t => Promise.reject(new Error('no')))
+        t.test('yellow', t => t.end())
+        return t.test('apple', t => t.test('this too', t => t.end()))
+      })
+      t.test('axo', t => Promise.reject(new Error('no')))
+    }
+  ],
+  TAP_ONLY: [
+    () => process.env.TAP_ONLY = '1',
+    t => {
+      t.only('only this one', t => t.end())
+      t.test('not this one', t => Promise.reject(new Error('no')))
+    }
+  ],
+  'timeout sigterm': t => {
+    t.pass('fine')
+    process.kill(process.pid, 'SIGTERM')
+  },
+  'timeout sigterm with handle': t => {
+    setTimeout(() => {}, 10000)
+    t.pass('fine')
+    process.kill(process.pid, 'SIGTERM')
+  },
+  'timeout sigterm many times': t => {
+    const fs = require('fs')
+    fs.readFile(__filename, (er, data) => {})
+    t.pass('fine')
+    process.kill(process.pid, 'SIGTERM')
+    process.kill(process.pid, 'SIGTERM')
+    process.kill(process.pid, 'SIGTERM')
+    process.kill(process.pid, 'SIGTERM')
+    process.kill(process.pid, 'SIGTERM')
+    process.kill(process.pid, 'SIGTERM')
+  },
+}
+
+const main = t => {
+  const spawn = require('child_process').spawn
+  const keys = Object.keys(cases)
+  t.plan(keys.length)
+  keys.forEach(k => t.test(k, t => {
+    t.plan(3)
+    const c = spawn(node, [__filename, k])
+    let out = ''
+    c.stdout.on('data', c => out += c)
+    let err = ''
+    c.stderr.on('data', c => err += c)
+    c.on('close', (code, signal) => {
+      t.matchSnapshot({
+        code: code,
+        signal: signal
+      }, 'exit status')
+      t.matchSnapshot(clean(out), 'stdout')
+      t.matchSnapshot(clean(err), 'stderr')
+    })
+  }))
+}
+
+const c = cases[process.argv[2]]
+if (Array.isArray(c)) {
+  c[0]()
+  c[1](require('../lib/tap.js'))
+} else if (typeof c === 'function')
+  c(require('../lib/tap.js'))
+else
+  main(require('../lib/tap.js'))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-tap.git



More information about the Pkg-javascript-commits mailing list