From 66f9ac1ea6d54c124f9df36a256eacf1ac669ae8 Mon Sep 17 00:00:00 2001 From: James Talmage Date: Sat, 9 Jan 2016 04:20:54 -0500 Subject: [PATCH 1/2] Standardize colors across reporters. PR #410 missed a color change on a single log line, See: https://github.com/sindresorhus/ava/pull/410#issuecomment-170210415 Using this convention will make it much easier to review PR's. No need to remember what color is used where, just ensure they don't use chalk directly, and alias the color with it's intended purpose in the colors module. --- cli.js | 6 +++--- lib/reporters/colors.js | 11 +++++++++++ lib/reporters/mini.js | 30 +++++++++++++++--------------- lib/reporters/verbose.js | 30 +++++++++++++++--------------- 4 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 lib/reporters/colors.js diff --git a/cli.js b/cli.js index 539931e5c..0ce814416 100755 --- a/cli.js +++ b/cli.js @@ -21,9 +21,9 @@ var updateNotifier = require('update-notifier'); var figures = require('figures'); var arrify = require('arrify'); var meow = require('meow'); -var chalk = require('chalk'); var Promise = require('bluebird'); var pkgConf = require('pkg-conf'); +var colors = require('./lib/reporters/colors'); var verboseReporter = require('./lib/reporters/verbose'); var miniReporter = require('./lib/reporters/mini'); var tapReporter = require('./lib/reporters/tap'); @@ -111,9 +111,9 @@ api.run() }) .catch(function (err) { if (err.name === 'AvaError') { - console.log(' ' + chalk.red(figures.cross) + ' ' + err.message); + console.log(' ' + colors.error(figures.cross) + ' ' + err.message); } else { - console.error(err.stack); + console.error(colors.stack(err.stack)); } logger.exit(1); diff --git a/lib/reporters/colors.js b/lib/reporters/colors.js new file mode 100644 index 000000000..e9cc10741 --- /dev/null +++ b/lib/reporters/colors.js @@ -0,0 +1,11 @@ +'use strict'; + +var chalk = require('chalk'); + +module.exports = { + error: chalk.red, + skip: chalk.yellow, + pass: chalk.green, + duration: chalk.gray.dim, + stack: chalk.red +}; diff --git a/lib/reporters/mini.js b/lib/reporters/mini.js index d6f39dad6..3c36077e5 100644 --- a/lib/reporters/mini.js +++ b/lib/reporters/mini.js @@ -1,6 +1,6 @@ 'use strict'; var logUpdate = require('log-update'); -var chalk = require('chalk'); +var colors = require('./colors'); var plur = require('plur'); var beautifyStack = require('../beautify-stack'); @@ -28,13 +28,13 @@ MiniReporter.prototype.test = function (test) { var title; if (test.skip) { - title = chalk.yellow('- ' + test.title); + title = colors.skip('- ' + test.title); this.skipCount++; } else if (test.error) { - title = chalk.red(test.title); + title = colors.error(test.title); this.failCount++; } else { - title = chalk.green(test.title); + title = colors.pass(test.title); this.passCount++; } @@ -42,11 +42,11 @@ MiniReporter.prototype.test = function (test) { status += '\n\n'; if (this.passCount > 0) { - status += ' ' + chalk.green(this.passCount, 'passed'); + status += ' ' + colors.pass(this.passCount, 'passed'); } if (this.failCount > 0) { - status += ' ' + chalk.red(this.failCount, 'failed'); + status += ' ' + colors.error(this.failCount, 'failed'); } return status; @@ -66,23 +66,23 @@ MiniReporter.prototype.finish = function () { var status = '\n'; if (this.passCount > 0) { - status += ' ' + chalk.green(this.passCount, 'passed'); + status += ' ' + colors.pass(this.passCount, 'passed'); } if (this.skipCount > 0) { - status += ' ' + chalk.yellow(this.skipCount, 'skipped'); + status += ' ' + colors.skip(this.skipCount, 'skipped'); } if (this.failCount > 0) { - status += ' ' + chalk.red(this.failCount, 'failed'); + status += ' ' + colors.error(this.failCount, 'failed'); } if (this.rejectionCount > 0) { - status += '\n ' + chalk.red(this.rejectionCount, plur('rejection', this.rejectionCount)); + status += '\n ' + colors.error(this.rejectionCount, plur('rejection', this.rejectionCount)); } if (this.exceptionCount > 0) { - status += '\n ' + chalk.red(this.exceptionCount, plur('exception', this.exceptionCount)); + status += '\n ' + colors.error(this.exceptionCount, plur('exception', this.exceptionCount)); } var i = 0; @@ -105,8 +105,8 @@ MiniReporter.prototype.finish = function () { description = JSON.stringify(test); } - status += '\n\n ' + chalk.red(i + '.', title) + '\n'; - status += chalk.red(description); + status += '\n\n ' + colors.error(i + '.', title) + '\n'; + status += colors.stack(description); }); } @@ -121,8 +121,8 @@ MiniReporter.prototype.finish = function () { var title = err.type === 'rejection' ? 'Unhandled Rejection' : 'Uncaught Exception'; var description = err.stack ? beautifyStack(err.stack) : JSON.stringify(err); - status += '\n\n ' + chalk.red(i + '.', title) + '\n'; - status += ' ' + chalk.red(description); + status += '\n\n ' + colors.error(i + '.', title) + '\n'; + status += ' ' + colors.stack(description); }); } diff --git a/lib/reporters/verbose.js b/lib/reporters/verbose.js index e322ac242..6b28435e0 100644 --- a/lib/reporters/verbose.js +++ b/lib/reporters/verbose.js @@ -1,7 +1,7 @@ 'use strict'; var prettyMs = require('pretty-ms'); var figures = require('figures'); -var chalk = require('chalk'); +var colors = require('./colors'); var plur = require('plur'); var beautifyStack = require('../beautify-stack'); @@ -19,11 +19,11 @@ VerboseReporter.prototype.start = function () { VerboseReporter.prototype.test = function (test) { if (test.error) { - return ' ' + chalk.red(figures.cross) + ' ' + test.title + ' ' + chalk.red(test.error.message); + return ' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message); } if (test.skip) { - return ' ' + chalk.cyan('- ' + test.title); + return ' ' + colors.skip('- ' + test.title); } if (this.api.fileCount === 1 && this.api.testCount === 1 && test.title === '[anonymous]') { @@ -32,9 +32,9 @@ VerboseReporter.prototype.test = function (test) { // display duration only over a threshold var threshold = 100; - var duration = test.duration > threshold ? chalk.gray.dim(' (' + prettyMs(test.duration) + ')') : ''; + var duration = test.duration > threshold ? colors.duration(' (' + prettyMs(test.duration) + ')') : ''; - return ' ' + chalk.green(figures.tick) + ' ' + test.title + duration; + return ' ' + colors.pass(figures.tick) + ' ' + test.title + duration; }; VerboseReporter.prototype.unhandledError = function (err) { @@ -43,12 +43,12 @@ VerboseReporter.prototype.unhandledError = function (err) { exception: 'Uncaught Exception' }; - var output = chalk.red(types[err.type] + ':', err.file) + '\n'; + var output = colors.error(types[err.type] + ':', err.file) + '\n'; if (err.stack) { - output += ' ' + chalk.red(beautifyStack(err.stack)) + '\n'; + output += ' ' + colors.stack(beautifyStack(err.stack)) + '\n'; } else { - output += ' ' + chalk.red(JSON.stringify(err)) + '\n'; + output += ' ' + colors.stack(JSON.stringify(err)) + '\n'; } output += '\n'; @@ -60,21 +60,21 @@ VerboseReporter.prototype.finish = function () { var output = '\n'; if (this.api.failCount > 0) { - output += ' ' + chalk.red(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n'; + output += ' ' + colors.error(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n'; } else { - output += ' ' + chalk.green(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n'; + output += ' ' + colors.pass(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n'; } if (this.api.skipCount > 0) { - output += ' ' + chalk.yellow(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n'; + output += ' ' + colors.skip(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n'; } if (this.api.rejectionCount > 0) { - output += ' ' + chalk.red(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n'; + output += ' ' + colors.error(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n'; } if (this.api.exceptionCount > 0) { - output += ' ' + chalk.red(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n'; + output += ' ' + colors.error(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n'; } if (this.api.failCount > 0) { @@ -89,8 +89,8 @@ VerboseReporter.prototype.finish = function () { i++; - output += ' ' + chalk.red(i + '.', test.title) + '\n'; - output += ' ' + chalk.red(beautifyStack(test.error.stack)) + '\n'; + output += ' ' + colors.error(i + '.', test.title) + '\n'; + output += ' ' + colors.stack(beautifyStack(test.error.stack)) + '\n'; }); } From 05b3b84a5fad1f257beecb777c5b71efcbf626ec Mon Sep 17 00:00:00 2001 From: James Talmage Date: Sat, 9 Jan 2016 04:49:41 -0500 Subject: [PATCH 2/2] move colors up a directory --- cli.js | 2 +- lib/{reporters => }/colors.js | 0 lib/reporters/mini.js | 2 +- lib/reporters/verbose.js | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename lib/{reporters => }/colors.js (100%) diff --git a/cli.js b/cli.js index 0ce814416..78a6521d8 100755 --- a/cli.js +++ b/cli.js @@ -23,7 +23,7 @@ var arrify = require('arrify'); var meow = require('meow'); var Promise = require('bluebird'); var pkgConf = require('pkg-conf'); -var colors = require('./lib/reporters/colors'); +var colors = require('./lib/colors'); var verboseReporter = require('./lib/reporters/verbose'); var miniReporter = require('./lib/reporters/mini'); var tapReporter = require('./lib/reporters/tap'); diff --git a/lib/reporters/colors.js b/lib/colors.js similarity index 100% rename from lib/reporters/colors.js rename to lib/colors.js diff --git a/lib/reporters/mini.js b/lib/reporters/mini.js index 3c36077e5..9cf79eca7 100644 --- a/lib/reporters/mini.js +++ b/lib/reporters/mini.js @@ -1,6 +1,6 @@ 'use strict'; var logUpdate = require('log-update'); -var colors = require('./colors'); +var colors = require('../colors'); var plur = require('plur'); var beautifyStack = require('../beautify-stack'); diff --git a/lib/reporters/verbose.js b/lib/reporters/verbose.js index 6b28435e0..82351b13e 100644 --- a/lib/reporters/verbose.js +++ b/lib/reporters/verbose.js @@ -1,7 +1,7 @@ 'use strict'; var prettyMs = require('pretty-ms'); var figures = require('figures'); -var colors = require('./colors'); +var colors = require('../colors'); var plur = require('plur'); var beautifyStack = require('../beautify-stack');