[Pkg-javascript-commits] [node-tap-mocha-reporter] 35/137: set duration from comment for root suite

Bastien Roucariès rouca at moszumanska.debian.org
Thu Sep 7 09:49:24 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-mocha-reporter.

commit cbeb2c5e9fa89226b34878d9f4d66624bd820ce6
Author: isaacs <i at izs.me>
Date:   Sun Apr 26 20:24:22 2015 -0400

    set duration from comment for root suite
---
 lib/runner.js | 54 ++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 18 deletions(-)

diff --git a/lib/runner.js b/lib/runner.js
index 30e1c98..f6f3a9c 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -46,6 +46,7 @@ var Test = require('./test.js')
 var Suite = require('./suite.js')
 var Writable = require('stream').Writable
 var Parser = require('tap-parser')
+var timere = /^#\s*time=((?:0|[1-9][0-9]*)(:?\.[0-9]+)?)(ms|s)?\n$/
 
 util.inherits(Runner, Writable)
 
@@ -81,11 +82,6 @@ Parser.prototype.fullTitle = function () {
 }
 
 function attachEvents (runner, parser, level) {
-  var events = [
-    'version', 'plan', 'assert', 'comment',
-    'complete', 'extra', 'bailout'
-  ]
-
   parser.runner = runner
 
   if (level === 0) {
@@ -93,13 +89,12 @@ function attachEvents (runner, parser, level) {
       runner.emit('version', v)
     })
     parser.on('complete', function (res) {
-      runner.emit('end')
+      runner.emit('end', runner.stats)
     })
   }
 
   parser.emittedSuite = false
   parser.didAssert = false
-  parser.printed = false
   parser.name = ''
   parser.doingChild = null
 
@@ -128,7 +123,6 @@ function attachEvents (runner, parser, level) {
   })
 
   parser.on('child', function (child) {
-    //console.log('>>> child')
     child.parent = parser
     attachEvents(runner, child, level + 1)
 
@@ -143,9 +137,22 @@ function attachEvents (runner, parser, level) {
   })
 
   parser.on('comment', function (c) {
-    if (!this.printed && c.match(/^# Subtest: /)) {
+    if (c.match(/^# Subtest: /)) {
       c = c.trim().replace(/^# Subtest: /, '')
-      this.name = c
+      parser.name = c
+    }
+
+    // get the top-level duration from the trailing comment,
+    // if one occurs.
+    if (!this.suite || this.suite.root) {
+      var m = c.match(timere)
+      if (m) {
+        var time = m[1]
+        if (m[2] === 's')
+          time *= 1000
+
+        runner.stats.duration = Math.round(time, 0)
+      }
     }
   })
 
@@ -155,12 +162,17 @@ function attachEvents (runner, parser, level) {
   })
 
   parser.on('assert', function (result) {
-    emitSuite(this)
+    emitSuite(this, result)
 
     // no need to print the trailing assert for subtests
-    // we've already emitted a 'suite end' event for this.
+    // just do a 'suite end' event for this.
     if (this.doingChild && this.doingChild.didAssert &&
         this.doingChild.name === result.name) {
+      var suite = this.doingChild.suite
+      if (suite) {
+        suite.duration = Math.round(result.time, 0)
+        runner.emit('suite end', this.doingChild.suite)
+      }
       this.doingChild = null
       return
     }
@@ -173,8 +185,6 @@ function attachEvents (runner, parser, level) {
 
   parser.on('complete', function (results) {
     this.results = results
-    if (this.suite)
-      runner.emit('suite end', this.suite)
   })
 
   parser.on('bailout', function (reason) {
@@ -198,13 +208,14 @@ function attachEvents (runner, parser, level) {
   })
 }
 
-function emitSuite (parser) {
-  //console.log('emitSuite', parser.emittedSuite, parser.level, parser.name)
-  if (!parser.emittedSuite && parser.name) {
+function emitSuite (parser, result) {
+  if (!parser.emittedSuite && (parser.name || parser.level === 0)) {
     parser.emittedSuite = true
     var suite = parser.suite = new Suite(parser)
-    if (parser.parent && parser.parent.suite)
+    if (parser.parent && parser.parent.suite) {
       parser.parent.suite.suites.push(suite)
+    }
+
     parser.runner.emit('suite', suite)
   }
 }
@@ -213,10 +224,17 @@ function emitTest (parser, result) {
   var runner = parser.runner
   var test = new Test(result, parser)
 
+  if (!parser.suite) {
+    emitSuite(parser, result)
+  }
+
+
   if (parser.suite) {
     //if (test.parent === parser)
     //  test.parent = parser.suite
     parser.suite.tests.push(test)
+  } else {
+    console.error('test without suite?')
   }
 
   runner.emit('test', test)

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



More information about the Pkg-javascript-commits mailing list