Skip to content

Commit 5fe0953

Browse files
committed
test: improve tty.getColorDepth coverage
1 parent f24d0ec commit 5fe0953

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

test/pseudo-tty/test-tty-get-color-depth.js

+50-17
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,65 @@ const assert = require('assert').strict;
55
/* eslint-disable no-restricted-properties */
66
const { WriteStream } = require('tty');
77

8+
const NODE_DISABLE_COLORS = process.env.NODE_DISABLE_COLORS;
9+
delete process.env.NODE_DISABLE_COLORS;
10+
811
const fd = common.getTTYfd();
912
const writeStream = new WriteStream(fd);
1013

1114
{
1215
const depth = writeStream.getColorDepth();
13-
1416
assert.equal(typeof depth, 'number');
1517
assert(depth >= 1 && depth <= 24);
16-
17-
if (depth === 1) {
18-
// Terminal does not support colors, compare to a value that would.
19-
assert.notEqual(writeStream.getColorDepth({ COLORTERM: '1' }), depth);
20-
} else {
21-
// Terminal supports colors, compare to a value that would not.
22-
assert.notEqual(writeStream.getColorDepth({ TERM: 'dumb' }), depth);
23-
}
2418
}
2519

26-
// Deactivate colors
27-
{
28-
const tmp = process.env.NODE_DISABLE_COLORS;
29-
process.env.NODE_DISABLE_COLORS = 1;
30-
31-
const depth = writeStream.getColorDepth();
20+
// Check different environment variables.
21+
[
22+
[{ COLORTERM: '1' }, 4],
23+
[{ TMUX: '1' }, 8],
24+
[{ CI: '1' }, 1],
25+
[{ CI: '1', TRAVIS: '1' }, 8],
26+
[{ CI: '1', CIRCLECI: '1' }, 8],
27+
[{ CI: '1', APPVEYOR: '1' }, 8],
28+
[{ CI: '1', GITLAB_CI: '1' }, 8],
29+
[{ CI: '1', CI_NAME: 'codeship' }, 8],
30+
[{ TEAMCITY_VERSION: '1.0.0' }, 1],
31+
[{ TEAMCITY_VERSION: '9.11.0' }, 4],
32+
[{ TERM_PROGRAM: 'iTerm.app' }, 8],
33+
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '3.0' }, 24],
34+
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '2.0' }, 8],
35+
[{ TERM_PROGRAM: 'HyperTerm' }, 24],
36+
[{ TERM_PROGRAM: 'Hyper' }, 24],
37+
[{ TERM_PROGRAM: 'MacTerm' }, 24],
38+
[{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
39+
[{ TERM: 'xterm-256' }, 8],
40+
[{ TERM: 'ansi' }, 4],
41+
[{ TERM: 'ANSI' }, 4],
42+
[{ TERM: 'color' }, 4],
43+
[{ TERM: 'linux' }, 4],
44+
[{ TERM: 'fail' }, 1],
45+
[{ NODE_DISABLE_COLORS: '1' }, 1],
46+
[{ TERM: 'dumb' }, 1],
47+
[{ TERM: 'dumb', COLORTERM: '1' }, 4],
48+
].forEach(([env, depth], i) => {
49+
const actual = writeStream.getColorDepth(env);
50+
assert.equal(
51+
actual,
52+
depth,
53+
`i: ${i}, expected: ${depth}, actual: ${actual}, env: ${env}`
54+
);
55+
});
3256

33-
assert.equal(depth, 1);
57+
// OS settings
58+
{
59+
const platform = Object.getOwnPropertyDescriptor(process, 'platform');
60+
const [ value, depth1, depth2 ] = process.platform !== 'win32' ?
61+
['win32', 1, 4] : ['linux', 4, 1];
3462

35-
process.env.NODE_DISABLE_COLORS = tmp;
63+
assert.equal(writeStream.getColorDepth({}), depth1);
64+
Object.defineProperty(process, 'platform', { value });
65+
assert.equal(writeStream.getColorDepth({}), depth2);
66+
Object.defineProperty(process, 'platform', platform);
3667
}
68+
69+
process.env.NODE_DISABLE_COLORS = NODE_DISABLE_COLORS;

0 commit comments

Comments
 (0)