Skip to content

Commit d399797

Browse files
oantoronovemberborn
authored andcommitted
Fake hasColors() in worker processes
Fixes #2170.
1 parent 90acbb9 commit d399797

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

lib/fork.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if (process.env.NODE_PATH) {
2525

2626
const describeTTY = tty => ({
2727
colorDepth: tty.getColorDepth ? tty.getColorDepth() : undefined,
28+
hasColors: typeof tty.hasColors === 'function',
2829
columns: tty.columns || 80,
2930
rows: tty.rows
3031
});

lib/worker/fake-tty-has-colors.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const tty = require('tty');
3+
4+
// Call original method to ensure the correct errors are thrown.
5+
const assertHasColorsArguments = count => {
6+
tty.WriteStream.prototype.hasColors(count);
7+
};
8+
9+
const makeHasColors = colorDepth => (count = 16, env) => {
10+
// `count` is optional too, so make sure it's not an env object.
11+
if (env === undefined && typeof count === 'object' && count !== null) {
12+
count = 16;
13+
}
14+
15+
assertHasColorsArguments(count);
16+
return count <= 2 ** colorDepth;
17+
};
18+
19+
module.exports = makeHasColors;

lib/worker/fake-tty.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
const tty = require('tty');
33
const ansiEscapes = require('ansi-escapes');
44
const options = require('./options').get();
5+
const makeHasColors = require('./fake-tty-has-colors');
56

67
const fakeTTYs = new Set();
78

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

11-
const simulateTTY = (stream, {colorDepth, columns, rows}) => {
12+
const simulateTTY = (stream, {colorDepth, hasColors, columns, rows}) => {
1213
Object.assign(stream, {isTTY: true, columns, rows});
1314

1415
stream.clearLine = dir => {
@@ -35,6 +36,10 @@ const simulateTTY = (stream, {colorDepth, columns, rows}) => {
3536
if (colorDepth !== undefined) {
3637
stream.getColorDepth = () => colorDepth;
3738
}
39+
40+
if (hasColors) {
41+
stream.hasColors = makeHasColors(colorDepth);
42+
}
3843
};
3944

4045
if (options.tty.stderr) {

test/helper/simulate-tty.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
'use strict';
2+
const makeHasColors = require('../../lib/worker/fake-tty-has-colors');
23

3-
const simulateTTY = (stream, colorDepth) => {
4+
const simulateTTY = (stream, colorDepth, hasColors) => {
45
stream.isTTY = true;
56
stream.columns = 80;
67
stream.rows = 24;
78

89
if (colorDepth) {
910
stream.getColorDepth = () => colorDepth;
1011
}
12+
13+
if (hasColors) {
14+
stream.hasColors = makeHasColors(colorDepth);
15+
}
1116
};
1217

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

21-
simulateTTY(process.stderr, colorDepth);
22-
simulateTTY(process.stdout, colorDepth);
27+
simulateTTY(process.stderr, colorDepth, hasColors);
28+
simulateTTY(process.stdout, colorDepth, hasColors);
2329
}

test/integration/node-assertions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ test('node assertion failures are reported to the console when running in a term
88
dirname: 'fixture/node-assertions',
99
env: {
1010
AVA_SIMULATE_TTY: true,
11-
AVA_TTY_COLOR_DEPTH: 8
11+
AVA_TTY_COLOR_DEPTH: 8,
12+
AVA_TTY_HAS_COLORS: typeof process.stderr.hasColors === 'function'
1213
}
1314
};
1415

0 commit comments

Comments
 (0)