summaryrefslogtreecommitdiff
path: root/test/parallel/test-console-tty-colors.js
blob: 0eb51c72898f7c28321c5f395b42ca17444cae08 (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
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');
const { Writable } = require('stream');
const { Console } = require('console');

function check(isTTY, colorMode, expectedColorMode, inspectOptions) {
  const items = [
    1,
    { a: 2 },
    [ 'foo' ],
    { '\\a': '\\bar' },
  ];

  let i = 0;
  const stream = new Writable({
    write: common.mustCall((chunk, enc, cb) => {
      assert.strictEqual(chunk.trim(),
                         util.inspect(items[i++], {
                           colors: expectedColorMode,
                           ...inspectOptions
                         }));
      cb();
    }, items.length),
    decodeStrings: false
  });
  stream.isTTY = isTTY;

  // Set ignoreErrors to `false` here so that we see assertion failures
  // from the `write()` call happen.
  const testConsole = new Console({
    stdout: stream,
    ignoreErrors: false,
    colorMode,
    inspectOptions
  });
  for (const item of items) {
    testConsole.log(item);
  }
}

check(true, 'auto', true);
check(false, 'auto', false);
check(false, undefined, true, { colors: true, compact: false });
check(true, 'auto', true, { compact: false });
check(true, undefined, false, { colors: false });
check(true, true, true);
check(false, true, true);
check(true, false, false);
check(false, false, false);

// Check invalid options.
{
  const stream = new Writable({
    write: common.mustNotCall()
  });

  [0, 'true', null, {}, [], () => {}].forEach((colorMode) => {
    const received = util.inspect(colorMode);
    assert.throws(
      () => {
        new Console({
          stdout: stream,
          ignoreErrors: false,
          colorMode: colorMode
        });
      },
      {
        message: `The argument 'colorMode' is invalid. Received ${received}`,
        code: 'ERR_INVALID_ARG_VALUE'
      }
    );
  });

  [true, false, 'auto'].forEach((colorMode) => {
    assert.throws(
      () => {
        new Console({
          stdout: stream,
          ignoreErrors: false,
          colorMode: colorMode,
          inspectOptions: {
            colors: false
          }
        });
      },
      {
        message: 'Option "options.inspectOptions.color" cannot be used in ' +
                 'combination with option "colorMode"',
        code: 'ERR_INCOMPATIBLE_OPTION_PAIR'
      }
    );
  });
}