[Pkg-javascript-commits] [node-tap] 04/186: tap: rewrite to class and arrows

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:38 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 ee2cc44e6bdc97a6a1765e2bf34fb1f34359e9b6
Author: isaacs <i at izs.me>
Date:   Mon Jul 3 08:54:34 2017 -0700

    tap: rewrite to class and arrows
---
 lib/tap.js | 193 ++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 95 insertions(+), 98 deletions(-)

diff --git a/lib/tap.js b/lib/tap.js
index d405b72..cf300e1 100644
--- a/lib/tap.js
+++ b/lib/tap.js
@@ -5,57 +5,95 @@ const Spawn = require('./spawn.js')
 const util = require('util')
 const objToYaml = require('./obj-to-yaml.js')
 const yaml = require('js-yaml')
+const _didPipe = Symbol('_didPipe')
 
-util.inherits(TAP, Test)
-function TAP (options) {
-  Test.call(this, options)
-  this.runOnly = process.env.TAP_ONLY === '1'
-  this.start = Date.now()
+const monkeypatchEpipe = () => {
+  const emit = process.stdout.emit
+  process.stdout.emit = (ev, er) => {
+    if (ev !== 'error' || er.code !== 'EPIPE')
+      return emit.apply(process, arguments)
+  }
 }
 
-let didPipe = false
-TAP.prototype.pipe = function () {
-  didPipe = true
-  this.setTimeout(this.options.timeout)
-  this.pipe = Test.prototype.pipe
-  this.push = Test.prototype.push
-  const ret = this.pipe.apply(this, arguments)
-  this.process()
-  return ret
-}
+const monkeypatchExit = () => {
+  const exit = process.exit
+  const reallyExit = process.reallyExit
 
-function monkeypatchEpipe () {
-  process.stdout.emit = function (emit) {
-    return function (ev, er) {
-      if (ev === 'error' && er.code === 'EPIPE')
-        return this.emit = emit
-      return emit.apply(this, arguments)
-    }
-  }(process.stdout.emit)
-}
-
-function monkeypatchExit () {
   // ensure that we always get run, even if a user does
   // process.on('exit', process.exit)
-  process.reallyExit = function (original) {
-    return function reallyExit (code) {
-      code = onExitEvent(code)
-      return original.call(this, code)
-    }
-  }(process.reallyExit)
+  process.reallyExit = code =>
+    reallyExit.call(process, onExitEvent(code))
 
-  process.exit = function (original) {
-    return function exit (code) {
-      code = onExitEvent(code)
-      return original.call(this, code)
-    }
-  }(process.exit)
+  process.exit = code =>
+    exit.call(process, onExitEvent(code))
 
   process.on('exit', onExitEvent)
 }
 
+class TAP extends Test {
+  constructor (options) {
+    super(options)
+    this.runOnly = process.env.TAP_ONLY === '1'
+    this.start = Date.now()
+    this[_didPipe] = false
+  }
+
+  pipe () {
+    this[_didPipe] = true
+    this.setTimeout(this.options.timeout)
+    this.pipe = Test.prototype.pipe
+    this.push = Test.prototype.push
+    const ret = this.pipe.apply(this, arguments)
+    this.process()
+    return ret
+  }
+
+  push () {
+    // this resets push and pipe to standard values
+    this.pipe(process.stdout)
+    this.patchProcess()
+    return this.push.apply(this, arguments)
+  }
+
+  patchProcess () {
+    monkeypatchEpipe()
+    monkeypatchExit()
+    process.on('uncaughtException', this.threw)
+    process.on('unhandledRejection', er => this.threw(er))
+  }
+
+  onbail () {
+    Test.prototype.onbail.apply(this, arguments)
+    this.endAll()
+    process.exit(1)
+  }
+
+  onbeforeend () {
+    if (this[_didPipe] && this.time && !this.bailedOut)
+      this.emit('data', '# time=' + this.time + 'ms\n')
+  }
+
+  ondone () {
+    try {
+      this.emit('teardown')
+    } catch (er) {
+      this.threw(er)
+    }
+  }
+
+  // Root test runner doesn't have the 'teardown' event, because it
+  // isn't hooked into any parent Test as a harness.
+  teardown (fn) {
+    this.autoend()
+    return Test.prototype.teardown.apply(this, arguments)
+  }
+  tearDown (fn) {
+    return this.teardown(fn)
+  }
+}
+
 let didOnExitEvent = false
-function onExitEvent (code) {
+const onExitEvent = code => {
   if (didOnExitEvent)
     return process.exitCode || code
 
@@ -73,47 +111,6 @@ function onExitEvent (code) {
   return process.exitCode || code || 0
 }
 
-TAP.prototype.push = function push () {
-  // this resets push and pipe to standard values
-  this.pipe(process.stdout)
-  this.patchProcess()
-  return this.push.apply(this, arguments)
-}
-
-TAP.prototype.patchProcess = function () {
-  monkeypatchEpipe()
-  monkeypatchExit()
-  process.on('uncaughtException', this.threw)
-  process.on('unhandledRejection', function (er) {
-    this.threw(er)
-  }.bind(this))
-}
-
-TAP.prototype.onbail = function () {
-  Test.prototype.onbail.apply(this, arguments)
-  this.endAll()
-  process.exit(1)
-}
-
-TAP.prototype.onbeforeend = function () {
-  if (didPipe && this.time && !this.bailedOut)
-    this.emit('data', '# time=' + this.time + 'ms\n')
-}
-
-TAP.prototype.ondone = function () {
-  try {
-    this.emit('teardown')
-  } catch (er) {
-    this.threw(er)
-  }
-}
-
-// Root test runner doesn't have the 'teardown' event, because it
-// isn't hooked into any parent Test as a harness.
-TAP.prototype.teardown = TAP.prototype.tearDown = function (fn) {
-  this.autoend()
-  return Test.prototype.teardown.apply(this, arguments)
-}
 
 const opt = { name: 'TAP' }
 if (process.env.TAP_DEBUG === '1' ||
@@ -121,7 +118,7 @@ if (process.env.TAP_DEBUG === '1' ||
   opt.debug = true
 
 if (process.env.TAP_GREP) {
-  opt.grep = process.env.TAP_GREP.split('\n').map(function (g) {
+  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] : ''
@@ -150,15 +147,15 @@ tap.synonyms = require('./synonyms.js')
 // SIGTERM means being forcibly killed, almost always by timeout
 const onExit = require('signal-exit')
 let didTimeoutKill = false
-onExit(function (code, signal) {
-  if (signal !== 'SIGTERM' || !didPipe || didTimeoutKill)
+onExit((code, signal) => {
+  if (signal !== 'SIGTERM' || !tap[_didPipe] || didTimeoutKill)
     return
 
-  const handles = process._getActiveHandles().filter(function (h) {
-    return h !== process.stdout &&
+  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
@@ -169,29 +166,29 @@ onExit(function (code, signal) {
     signal: signal
   }
   if (requests.length) {
-    extra.requests = requests.map(function (r) {
+    extra.requests = requests.map(r => {
       const ret = { type: r.constructor.name }
-      if (r.context) {
+      if (r.context)
         ret.context = r.context
-      }
       return ret
     })
   }
   if (handles.length) {
-    extra.handles = handles.map(function (h) {
+    extra.handles = handles.map(h => {
       const ret = { type: h.constructor.name }
-      if (h.msecs) {
+
+      if (h.msecs)
         ret.msecs = h.msecs
-      }
-      if (h._events) {
+
+      if (h._events)
         ret.events = Object.keys(h._events)
-      }
-      if (h._sockname) {
+
+      if (h._sockname)
         ret.sockname = h._sockname
-      }
-      if (h._connectionKey) {
+
+      if (h._connectionKey)
         ret.connectionKey = h._connectionKey
-      }
+
       return ret
     })
   }

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