[Pkg-javascript-commits] [node-tap-mocha-reporter] 47/137: diffs in classic reporter

Bastien Roucariès rouca at moszumanska.debian.org
Thu Sep 7 09:49:25 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 ea77719ad3b61b7e92ae5b02ddd7ec78f8d0f343
Author: isaacs <i at izs.me>
Date:   Sat May 2 10:03:11 2015 -0700

    diffs in classic reporter
---
 lib/reporters/classic.js | 194 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 192 insertions(+), 2 deletions(-)

diff --git a/lib/reporters/classic.js b/lib/reporters/classic.js
index 7fcebb0..e31a0db 100644
--- a/lib/reporters/classic.js
+++ b/lib/reporters/classic.js
@@ -7,6 +7,176 @@ var Base = require('./base')
   , util = require('util')
   , fancy = Base.useColors
   , ms = require('../ms.js')
+  , diff = require('diff')
+  , utils = require('../utils.js')
+
+function hasOwnProperty (obj, key) {
+  return Object.prototype.hasOwnProperty.call(obj, key)
+}
+
+function doDiff (found, wanted, palette) {
+  // TODO: Make this a configurable thing or something?
+  //
+  // Choosing a palette for diffs in a test-runner context
+  // is really tricky.  The temptation is to make it look
+  // exactly like `git diff`, but the default red and green
+  // are very confusing with the colors used to indicate
+  // pass/fail.
+  //
+  // So, I decided to experiment with setting a background to
+  // distinguish the diff section from the rest of the test
+  // output.  The obvious choice, then, was to mimick GitHub's
+  // diff styling.
+  //
+  // The problem there is that, while those of us with an
+  // abundance of cones tend to find that palette most pleasing,
+  // it's virtually impossible for people with various sorts of
+  // red/green colorblindness to distinguish those colors.
+  //
+  // The resulting option, with a somewhat pink-ish red and a
+  // somewhat yellow-ish green, seems to be a pretty good
+  // compromise between esthetics and accessibility.  In a poll
+  // on twitter, it was the only one that no color-sighted people
+  // strongly objected to, and no color-blind people had
+  // significant trouble interpreting.  The twitter poll agrees
+  // with the results of Sim Daltonism, which showed that this
+  // palette was easily distinguishable across all forms of color
+  // deficiency.
+
+  palette = 6
+
+  var plain = ''
+  var reset = '\u001b[m'
+
+  switch (palette) {
+    case 1:
+      // most git-like.  r/g, no background, no bold
+      var bg = ''
+      var removed = '\u001b[31m'
+      var added = '\u001b[32m'
+      break
+
+    case 2:
+      // dark option, maybe confusing with pass/fail colors?
+      var bg = '\u001b[48;5;234m'
+      var removed = '\u001b[31;1m'
+      var added = '\u001b[32;1m'
+      break
+
+    case 3:
+      // pastel option, most githubby
+      var removed = '\u001b[48;5;224m\u001b[38;5;52m'
+      var added = '\u001b[48;5;194m\u001b[38;5;22m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+    case 4:
+      // orange/cyan pastel option, most r/g-colorblind friendly
+      var removed = '\u001b[48;5;223m\u001b[38;5;52m'
+      var added = '\u001b[48;5;158m\u001b[38;5;22m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+    case 5:
+      // pastel option, green is bluish, red is just red
+      var removed = '\u001b[48;5;224m\u001b[38;5;52m'
+      var added = '\u001b[48;5;158m\u001b[38;5;22m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+    case 6:
+      // pastel option, red is purplish, green is yellowish
+      var removed = '\u001b[48;5;218m\u001b[38;5;52m'
+      var added = '\u001b[48;5;193m\u001b[38;5;22m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+    case 7:
+      // pastel option, red is purplish, green is just green
+      var removed = '\u001b[48;5;218m\u001b[38;5;52m'
+      var added = '\u001b[48;5;194m\u001b[38;5;22m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+    case 8:
+      // pastel, red and blue
+      var removed = '\u001b[48;5;224m\u001b[38;5;52m'
+      var added = '\u001b[48;5;189m\u001b[38;5;17m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+    case 9:
+      // pastel option, red is purplish, green is yellowish
+      var removed = '\u001b[48;5;224m\u001b[38;5;52m'
+      var added = '\u001b[48;5;193m\u001b[38;5;22m'
+      var plain = '\u001b[38;5;233m'
+      var bg = '\u001b[48;5;8m'
+      break
+
+  }
+
+
+  var maxLen = process.stdout.columns || 0
+  if (maxLen >= 5)
+    maxLen -= 5
+
+  if (!Base.useColors) {
+    bg = removed = added = reset = plain = ''
+    maxLen = 0
+  }
+
+  if (typeof found !== 'string' ||
+      typeof wanted !== 'string') {
+    found = utils.stringify(found)
+    wanted = utils.stringify(wanted)
+  }
+
+  var patch = diff.createPatch('', wanted, found)
+  //console.error(patch)
+  var width = 0
+  patch = patch.split('\n').map(function (line, index) {
+    if (line.length > width)
+      width = Math.min(maxLen, line.length)
+    if (line.match(/^\=+$/) ||
+        line === '\\ No newline at end of file')
+      return null
+    else
+      return line
+  }).filter(function (line, i) {
+    return line && i > 4
+  }).map(function (line) {
+    if (line.length < width)
+      line += new Array(width - line.length + 1).join(' ')
+    return line
+  }).map(function (line) {
+    if (line.charAt(0) === '+')
+      return bg + added + line + reset
+    else if (line.charAt(0) === '-')
+      return bg + removed + line + reset
+    else
+      return bg + plain + line + reset
+  }).join('\n')
+
+  var pref =
+    bg + added + '+++ found' +
+      (Base.useColors
+        ? new Array(width - '+++ found'.length + 1).join(' ')
+        : '') +
+      reset + '\n' +
+    bg + removed + '--- wanted' +
+      (Base.useColors
+        ? new Array(width - '--- wanted'.length + 1).join(' ')
+        : '') +
+      reset + '\n'
+
+  return pref + patch
+}
 
 util.inherits(Classic, Base)
 
@@ -92,8 +262,28 @@ function Classic (runner) {
       var failMsg = ''
       fails.forEach(function (t) {
         failMsg += Base.color('fail', 'not ok ' + t.name) + '\n'
-        if (t.diag)
-          failMsg += indent(yaml.safeDump(t.diag), 2) + '\n'
+        if (t.diag) {
+          var printDiff = false
+          if (hasOwnProperty(t.diag, 'found') &&
+              hasOwnProperty(t.diag, 'wanted')) {
+            printDiff = true
+            var found = t.diag.found
+            var wanted = t.diag.wanted
+            failMsg += indent(doDiff(found, wanted), 2) + '\n'
+          }
+
+          var o = {}
+          var print = false
+          for (var i in t.diag) {
+            // Don't re-print what we already showed in the diff
+            if (printDiff && ( i === 'found' || i === 'wanted'))
+              continue
+            o[i] = t.diag[i]
+            print = true
+          }
+          if (print)
+            failMsg += indent(yaml.safeDump(o), 2) + '\n'
+        }
       })
       console.log(indent(failMsg, 2))
     }

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