Skip to content

Commit

Permalink
Fake hasColors() in worker processes
Browse files Browse the repository at this point in the history
Fixes #2170.
  • Loading branch information
oantoro authored and novemberborn committed Jul 7, 2019
1 parent 90acbb9 commit d399797
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ if (process.env.NODE_PATH) {

const describeTTY = tty => ({
colorDepth: tty.getColorDepth ? tty.getColorDepth() : undefined,
hasColors: typeof tty.hasColors === 'function',
columns: tty.columns || 80,
rows: tty.rows
});
Expand Down
19 changes: 19 additions & 0 deletions lib/worker/fake-tty-has-colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
const tty = require('tty');

// Call original method to ensure the correct errors are thrown.
const assertHasColorsArguments = count => {
tty.WriteStream.prototype.hasColors(count);
};

const makeHasColors = colorDepth => (count = 16, env) => {
// `count` is optional too, so make sure it's not an env object.
if (env === undefined && typeof count === 'object' && count !== null) {
count = 16;
}

assertHasColorsArguments(count);
return count <= 2 ** colorDepth;
};

module.exports = makeHasColors;
7 changes: 6 additions & 1 deletion lib/worker/fake-tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
const tty = require('tty');
const ansiEscapes = require('ansi-escapes');
const options = require('./options').get();
const makeHasColors = require('./fake-tty-has-colors');

const fakeTTYs = new Set();

const {isatty} = tty;
tty.isatty = fd => fakeTTYs.has(fd) || isatty(fd);

const simulateTTY = (stream, {colorDepth, columns, rows}) => {
const simulateTTY = (stream, {colorDepth, hasColors, columns, rows}) => {
Object.assign(stream, {isTTY: true, columns, rows});

stream.clearLine = dir => {
Expand All @@ -35,6 +36,10 @@ const simulateTTY = (stream, {colorDepth, columns, rows}) => {
if (colorDepth !== undefined) {
stream.getColorDepth = () => colorDepth;
}

if (hasColors) {
stream.hasColors = makeHasColors(colorDepth);
}
};

if (options.tty.stderr) {
Expand Down
12 changes: 9 additions & 3 deletions test/helper/simulate-tty.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';
const makeHasColors = require('../../lib/worker/fake-tty-has-colors');

const simulateTTY = (stream, colorDepth) => {
const simulateTTY = (stream, colorDepth, hasColors) => {
stream.isTTY = true;
stream.columns = 80;
stream.rows = 24;

if (colorDepth) {
stream.getColorDepth = () => colorDepth;
}

if (hasColors) {
stream.hasColors = makeHasColors(colorDepth);
}
};

// The execCli helper spawns tests in a child process. This means that stdout is
Expand All @@ -17,7 +22,8 @@ if (process.env.AVA_SIMULATE_TTY) {
const colorDepth = process.env.AVA_TTY_COLOR_DEPTH ?
parseInt(process.env.AVA_TTY_COLOR_DEPTH, 10) :
undefined;
const hasColors = process.env.AVA_TTY_HAS_COLORS !== undefined;

simulateTTY(process.stderr, colorDepth);
simulateTTY(process.stdout, colorDepth);
simulateTTY(process.stderr, colorDepth, hasColors);
simulateTTY(process.stdout, colorDepth, hasColors);
}
3 changes: 2 additions & 1 deletion test/integration/node-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test('node assertion failures are reported to the console when running in a term
dirname: 'fixture/node-assertions',
env: {
AVA_SIMULATE_TTY: true,
AVA_TTY_COLOR_DEPTH: 8
AVA_TTY_COLOR_DEPTH: 8,
AVA_TTY_HAS_COLORS: typeof process.stderr.hasColors === 'function'
}
};

Expand Down

0 comments on commit d399797

Please sign in to comment.