summaryrefslogtreecommitdiff
path: root/test/pseudo-tty/test-tty-color-support.js
blob: 57ed640d4d813f0629c8851f09829c14e1482a35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';

const common = require('../common');
const assert = require('assert').strict;
const { WriteStream } = require('tty');
const { inspect } = require('util');

const fd = common.getTTYfd();
const writeStream = new WriteStream(fd);

{
  const depth = writeStream.getColorDepth();
  assert.strictEqual(typeof depth, 'number');
  assert(depth >= 1 && depth <= 24);

  const support = writeStream.hasColors();
  assert.strictEqual(support, depth !== 1);
}

// Validate invalid input.
[true, null, () => {}, Symbol(), 5n].forEach((input) => {
  assert.throws(
    () => writeStream.hasColors(input),
    { code: 'ERR_INVALID_ARG_TYPE' }
  );
});

[-1, 1].forEach((input) => {
  assert.throws(
    () => writeStream.hasColors(input),
    { code: 'ERR_OUT_OF_RANGE' }
  );
});

// Check different environment variables.
[
  [{ COLORTERM: '1' }, 4],
  [{ COLORTERM: 'truecolor' }, 24],
  [{ COLORTERM: '24bit' }, 24],
  [{ TMUX: '1' }, 8],
  [{ CI: '1' }, 1],
  [{ CI: '1', TRAVIS: '1' }, 8],
  [{ CI: '1', CIRCLECI: '1' }, 8],
  [{ CI: '1', APPVEYOR: '1' }, 8],
  [{ CI: '1', GITLAB_CI: '1' }, 8],
  [{ CI: '1', CI_NAME: 'codeship' }, 8],
  [{ TEAMCITY_VERSION: '1.0.0' }, 1],
  [{ TEAMCITY_VERSION: '9.11.0' }, 4],
  [{ TERM_PROGRAM: 'iTerm.app' }, 8],
  [{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '3.0' }, 24],
  [{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '2.0' }, 8],
  [{ TERM_PROGRAM: 'HyperTerm' }, 24],
  [{ TERM_PROGRAM: 'Hyper' }, 1],
  [{ TERM_PROGRAM: 'MacTerm' }, 24],
  [{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
  [{ TERM: 'xterm-256' }, 8],
  [{ TERM: 'ansi' }, 4],
  [{ TERM: 'ANSI' }, 4],
  [{ TERM: 'color' }, 4],
  [{ TERM: 'linux' }, 4],
  [{ TERM: 'fail' }, 1],
  [{ TERM: 'color', NODE_DISABLE_COLORS: '1' }, 1],
  [{ TERM: 'dumb' }, 1],
  [{ TERM: 'dumb', COLORTERM: '1' }, 1],
  [{ TERM: 'terminator' }, 24],
  [{ TERM: 'console' }, 4],
  [{ COLORTERM: '24bit', FORCE_COLOR: '' }, 4],
  [{ NO_COLOR: '1', FORCE_COLOR: '2' }, 8],
  [{ NODE_DISABLE_COLORS: '1', FORCE_COLOR: '3' }, 24],
  [{ NO_COLOR: '1', COLORTERM: '24bit' }, 1],
  [{ NO_COLOR: '', COLORTERM: '24bit' }, 1],
  [{ TMUX: '1', FORCE_COLOR: 0 }, 1],
  [{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
  [{ TERM: 'xterm-256color', COLORTERM: 'truecolor' }, 24],
].forEach(([env, depth], i) => {
  const actual = writeStream.getColorDepth(env);
  assert.strictEqual(
    actual,
    depth,
    `i: ${i}, expected: ${depth}, ` +
      `actual: ${actual}, env: ${inspect(env)}`
  );
  const colors = 2 ** actual;
  assert(writeStream.hasColors(colors, env));
  assert(!writeStream.hasColors(colors + 1, env));
  assert(depth >= 4 ? writeStream.hasColors(env) : !writeStream.hasColors(env));
});

// OS settings
{
  const platform = Object.getOwnPropertyDescriptor(process, 'platform');
  const [ value, depth1, depth2 ] = process.platform !== 'win32' ?
    ['win32', 1, 4] : ['linux', 4, 1];

  assert.strictEqual(writeStream.getColorDepth({}), depth1);
  Object.defineProperty(process, 'platform', { value });
  assert.strictEqual(writeStream.getColorDepth({}), depth2);
  Object.defineProperty(process, 'platform', platform);
}