Skip to content

Commit

Permalink
Stabilize colors (#1524)
Browse files Browse the repository at this point in the history
Prior to this commit, the library and its tests relied on the same code
to add color to terminal output. This prevented the tests from
recognizing changes to that code.

Such a change was introduced in a recent release of the `chalk` module
[1]: it was updated to disable text coloring when run without a TTY.
This is the case in the test environment environment provided by the
`tap` module (which AVA uses for its tests).

This fix disabled color output in the tests, but it caused no failures
because both the expected values and the actual values were effected.
However, this situation amounted to a decrease in test coverage because
the expected color of the terminal output could no longer be verified.

Update the tests environment to simulate a TTY so that AVA continues to
produce colored text despite being run in a sub-process provided by
`tap`. Update the test logic to reduce dependency on Ava internals in
order to avoid similar silent regressions in the future.

[1] chalk/chalk@0827d3b
  • Loading branch information
jugglinmike authored and novemberborn committed Sep 24, 2017
1 parent 61101d9 commit b888419
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 159 deletions.
40 changes: 40 additions & 0 deletions test/helper/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* This module is maintained to promote separation between the tests and the
* implementation.
*/
'use strict';

const ansiStyles = require('ansi-styles');

function make(name) {
const style = ansiStyles[name];
return function (string) {
return style.open + string + style.close;
};
}
const bold = make('bold');
const white = make('white');
const gray = make('gray');

// The following color definitions are contextual so that they produce expected
// values which mimic the behavior of the Chalk library.
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
const openDim = isSimpleWindowsTerm ? '' : ansiStyles.dim.open;
const openBlue = isSimpleWindowsTerm ? '\u001B[94m' : ansiStyles.blue.open;
// "Use `bold` by default on Windows"
// https://github.com/chalk/chalk/issues/36
const blue = string => openBlue + string + ansiStyles.blue.close;
// "(Windows) chalk.gray.dim not visible"
// https://github.com/chalk/chalk/issues/58
const dimGray = string => gray(openDim + string + ansiStyles.dim.close);

module.exports = {
blue,
boldWhite: string => bold(white(string)),
dimGray,
gray,
green: make('green'),
magenta: make('magenta'),
red: make('red'),
yellow: make('yellow')
};
Loading

0 comments on commit b888419

Please sign in to comment.