From b0b9d4f93192b247bef7fde2af33e69cf4e18b8a Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 2 Apr 2017 14:55:31 +0100 Subject: [PATCH 1/3] Don't highlight formatted values with --no-color Fixes #843 (https://github.com/avajs/ava/issues/843#issuecomment-290935567). Values shouldn't be highlighted during formatting if colors are disabled. --- lib/format-assert-error.js | 3 ++- test/cli.js | 17 +++++++++++++++++ test/fixture/formatting-color.js | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/fixture/formatting-color.js diff --git a/lib/format-assert-error.js b/lib/format-assert-error.js index d60be2097..a899af463 100644 --- a/lib/format-assert-error.js +++ b/lib/format-assert-error.js @@ -5,12 +5,13 @@ const chalk = require('chalk'); const diff = require('diff'); const DiffMatchPatch = require('diff-match-patch'); const indentString = require('indent-string'); +const globals = require('./globals'); function formatValue(value, options) { return prettyFormat(value, Object.assign({ callToJSON: false, plugins: [reactTestPlugin], - highlight: true + highlight: globals.options.color !== false }, options)); } exports.formatValue = formatValue; diff --git a/test/cli.js b/test/cli.js index 6aedf396f..dcbe7dfee 100644 --- a/test/cli.js +++ b/test/cli.js @@ -11,6 +11,7 @@ const proxyquire = require('proxyquire'); const sinon = require('sinon'); const uniqueTempDir = require('unique-temp-dir'); const execa = require('execa'); +const stripAnsi = require('strip-ansi'); const cliPath = path.join(__dirname, '../cli.js'); @@ -525,3 +526,19 @@ test('snapshots work', t => { }); }); }); + +test('--no-color disables formatting colors', t => { + execCli(['--no-color', '--verbose', 'formatting-color.js'], {dirname: 'fixture'}, (err, stdout, stderr) => { + t.ok(err); + t.is(stripAnsi(stderr), stderr); + t.end(); + }); +}); + +test('--color enables formatting colors', t => { + execCli(['--color', '--verbose', 'formatting-color.js'], {dirname: 'fixture'}, (err, stdout, stderr) => { + t.ok(err); + t.isNot(stripAnsi(stderr), stderr); + t.end(); + }); +}); diff --git a/test/fixture/formatting-color.js b/test/fixture/formatting-color.js new file mode 100644 index 000000000..77f34f5d5 --- /dev/null +++ b/test/fixture/formatting-color.js @@ -0,0 +1,5 @@ +import test from '../..'; + +test(t => { + t.deepEqual({foo: 1}, {foo: 2}); +}); From ce1a072e88878988e6b77278f068a5310ff3d2cd Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Mon, 3 Apr 2017 13:27:46 +0100 Subject: [PATCH 2/3] Document --color CLI flag --- lib/cli.js | 1 + readme.md | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/cli.js b/lib/cli.js index 57408bf60..7dd6d1805 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -38,6 +38,7 @@ exports.run = () => { --verbose, -v Enable verbose output --no-cache Disable the transpiler cache --no-power-assert Disable Power Assert + --color Force color output --no-color Disable color output --match, -m Only run tests with matching title (Can be repeated) --watch, -w Re-run tests when tests and source files change diff --git a/readme.md b/readme.md index b45688265..36fc833f6 100644 --- a/readme.md +++ b/readme.md @@ -164,6 +164,7 @@ $ ava --help --verbose, -v Enable verbose output --no-cache Disable the transpiler cache --no-power-assert Disable Power Assert + --color Force color output --no-color Disable color output --match, -m Only run tests with matching title (Can be repeated) --watch, -w Re-run tests when tests and source files change From 276b47387ea129c348ab86cc0d0fdbd9fb588843 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Mon, 3 Apr 2017 13:33:45 +0100 Subject: [PATCH 3/3] Set default color config based on supports-color --- lib/cli.js | 2 +- package.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cli.js b/lib/cli.js index 7dd6d1805..f6213f107 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -75,7 +75,7 @@ exports.run = () => { ], default: { cache: conf.cache, - color: 'color' in conf ? conf.color : true, + color: 'color' in conf ? conf.color : require('supports-color') !== false, concurrency: conf.concurrency, failFast: conf.failFast, init: conf.init, diff --git a/package.json b/package.json index e21d83e65..83bb99132 100644 --- a/package.json +++ b/package.json @@ -161,6 +161,7 @@ "stack-utils": "^1.0.0", "strip-ansi": "^3.0.1", "strip-bom-buf": "^1.0.0", + "supports-color": "^3.2.3", "time-require": "^0.1.2", "unique-temp-dir": "^1.0.0", "update-notifier": "^2.1.0"