[Pkg-javascript-commits] [node-tap] 76/186: move spawn unit test to snapshots, fix misinsg proc/exit handling

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:45 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 b2bf6021ce0fa8c6dfb11a917049c101a128cf62
Author: isaacs <i at izs.me>
Date:   Fri Nov 10 09:25:31 2017 -0800

    move spawn unit test to snapshots, fix misinsg proc/exit handling
---
 lib/spawn.js                            |  7 +++-
 tap-snapshots/unit-spawn.js-TAP.test.js | 62 +++++++++++++++++++++++++++++
 unit/spawn.js                           | 70 +++++++++++++++++++++++----------
 3 files changed, 116 insertions(+), 23 deletions(-)

diff --git a/lib/spawn.js b/lib/spawn.js
index 49c5d6d..f959879 100644
--- a/lib/spawn.js
+++ b/lib/spawn.js
@@ -72,8 +72,11 @@ class Spawn extends Base {
     try {
       const proc = this.proc = cp.spawn(this.command, this.args, options)
       proc.stdout.pipe(this.parser)
-      proc.on('close', this.onprocclose.bind(this))
-      proc.on('error', this.threw.bind(this))
+      proc.on('close', (code, signal) => this.onprocclose(code, signal))
+      proc.on('error', er => this.threw(er))
+      this.emit('process', proc)
+      if (this.parent)
+        this.parent.emit('spawn', this)
     } catch (er) {
       this.threw(er)
     }
diff --git a/tap-snapshots/unit-spawn.js-TAP.test.js b/tap-snapshots/unit-spawn.js-TAP.test.js
new file mode 100644
index 0000000..1c5e129
--- /dev/null
+++ b/tap-snapshots/unit-spawn.js-TAP.test.js
@@ -0,0 +1,62 @@
+'use strict'
+exports[`unit/spawn.js TAP timeout KILL > undefined 1`] = `
+SIGTERM
+
+not ok 1 - timeout!
+  ---
+  expired: killa
+  ...
+1..1
+# failed 1 test
+
+`
+
+exports[`unit/spawn.js TAP skip stuff > undefined 1`] = `
+TAP version 13
+ok 1 - skipper # SKIP {
+    1..0
+    # {time}
+}
+
+# Subtest: node ./unit/spawn.js skip-reason
+    1..0 # for no raisins
+    # {time}
+ok 2 - node ./unit/spawn.js skip-reason # SKIP for no raisins
+
+
+`
+
+exports[`unit/spawn.js TAP using proc event > undefined 1`] = `
+TAP version 13
+ok
+1..1
+
+`
+
+exports[`unit/spawn.js TAP failure to spawn > undefined 1`] = `
+
+not ok 1 - spawn something that does not exist ENOENT
+  ---
+  command: something that does not exist
+  args: []
+  stdio:
+    - 0
+    - pipe
+    - 2
+  cwd: {CWD}
+  at:
+    line: #
+    column: #
+    file: util.js
+    function: _errnoException
+  code: ENOENT
+  errno: ENOENT
+  syscall: spawn something that does not exist
+  path: something that does not exist
+  spawnargs: []
+  test: 'something that does not exist '
+  ...
+1..1
+# failed 1 test
+
+`
diff --git a/unit/spawn.js b/unit/spawn.js
index 8c673f1..ea77f6d 100644
--- a/unit/spawn.js
+++ b/unit/spawn.js
@@ -5,6 +5,18 @@ const Parser = require('tap-parser')
 const node = process.execPath
 const file = __filename
 
+const clean = out => out
+  .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 main = () => {
   t.test('basic child process', t =>
     t.spawn(node, [ file, 'ok' ]))
@@ -34,31 +46,24 @@ const main = () => {
       buffered: true,
       name: 'killa'
     })
-    s.main(_ => {
-      t.equal(s.output, `
-not ok 1 - timeout!
-  ---
-  expired: killa
-  ...
-1..1
-# failed 1 test
-`)
+    s.main(() => {
+      t.matchSnapshot(clean(s.output))
       t.end()
     })
   })
 
   t.test('skip stuff', t => {
-    const tt = new Test({ buffered: true })
+    const tt = new Test({ buffered: true, bail: false })
     tt.spawn(node, [ file, 'skip' ], {
-      bail: true
+      bail: true,
+      buffered: true
     }, 'skipper')
     tt.spawn(node, [ file, 'skip-reason' ])
     tt.test('check it', ttt => {
-      const output = tt.output
-      t.match(output, 'skipper # SKIP')
-      t.match(output,
-        'spawn.js skip-reason # SKIP for no raisins')
+      t.matchSnapshot(clean(tt.output))
       t.end()
+      tt.end()
+      ttt.end()
     })
   })
 
@@ -71,18 +76,38 @@ not ok 1 - timeout!
     t.end()
   })
 
+  t.test('using spawn event', t => {
+    t.on('spawn', t =>
+      t.proc.stdin.end('TAP version 13\nok\n1..1\n'))
+    t.spawn('cat', [], { stdio: [ 'pipe', 'pipe', 'ignore' ] })
+    t.end()
+  })
+
+  t.test('using proc event', t => {
+    const s = new Spawn({
+      command: 'cat',
+      args: [],
+      buffered: true,
+      bail: false,
+      stdio: [ 'pipe', 'ignore', 'ignore' ]
+    })
+    s.on('process', p =>
+      p.stdin.end('TAP version 13\nok\n1..1\n'))
+
+    t.plan(1)
+    s.main(() => t.matchSnapshot(clean(s.output)))
+  })
+
   t.test('failure to spawn', t => {
     const s = new Spawn({
       command: 'something that does not exist',
       args: [],
       buffered: true,
+      bail: false,
       stdio: [0, 1, 2]
     })
-    s.main(_ => {
-      t.match(s.output,
-        'not ok 1 - spawn something that does not exist ENOENT\n')
-      t.end()
-    })
+    t.plan(1)
+    s.main(() => t.matchSnapshot(clean(s.output)))
   })
 
   t.test('failure to spawn even harder', t => {
@@ -95,6 +120,7 @@ not ok 1 - timeout!
       command: 'poop',
       args: [],
       buffered: true,
+      bail: false,
       stdio: 'inherit'
     })
     s.main(_ => {
@@ -112,6 +138,7 @@ not ok 1 - timeout!
       const s = new Spawn({
         command: node,
         args: [ file, 'timeout' ],
+        bail: false,
         buffered: true
       })
       s.main(_ => {
@@ -125,6 +152,7 @@ not ok 1 - timeout!
       const s = new Spawn({
         command: node,
         args: [ file, 'timeout' ],
+        bail: false,
         buffered: true
       })
       s.endAll()
@@ -167,7 +195,7 @@ switch (process.argv[2]) {
     break
 
   case 'catch-term':
-    process.on('SIGTERM', _ => _)
+    process.on('SIGTERM', _ => console.log('SIGTERM'))
   case 'timeout':
     setTimeout(_ => _, 5000)
     break

-- 
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