• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const util = require('util');
5const { Writable } = require('stream');
6const { Console } = require('console');
7
8function check(isTTY, colorMode, expectedColorMode, inspectOptions) {
9  const items = [
10    1,
11    { a: 2 },
12    [ 'foo' ],
13    { '\\a': '\\bar' },
14  ];
15
16  let i = 0;
17  const stream = new Writable({
18    write: common.mustCall((chunk, enc, cb) => {
19      assert.strictEqual(chunk.trim(),
20                         util.inspect(items[i++], {
21                           colors: expectedColorMode,
22                           ...inspectOptions
23                         }));
24      cb();
25    }, items.length),
26    decodeStrings: false
27  });
28  stream.isTTY = isTTY;
29
30  // Set ignoreErrors to `false` here so that we see assertion failures
31  // from the `write()` call happen.
32  const testConsole = new Console({
33    stdout: stream,
34    ignoreErrors: false,
35    colorMode,
36    inspectOptions
37  });
38  for (const item of items) {
39    testConsole.log(item);
40  }
41}
42
43check(true, 'auto', true);
44check(false, 'auto', false);
45check(false, undefined, true, { colors: true, compact: false });
46check(true, 'auto', true, { compact: false });
47check(true, undefined, false, { colors: false });
48check(true, true, true);
49check(false, true, true);
50check(true, false, false);
51check(false, false, false);
52
53// Check invalid options.
54{
55  const stream = new Writable({
56    write: common.mustNotCall()
57  });
58
59  [0, 'true', null, {}, [], () => {}].forEach((colorMode) => {
60    const received = util.inspect(colorMode);
61    assert.throws(
62      () => {
63        new Console({
64          stdout: stream,
65          ignoreErrors: false,
66          colorMode: colorMode
67        });
68      },
69      {
70        message: `The argument 'colorMode' is invalid. Received ${received}`,
71        code: 'ERR_INVALID_ARG_VALUE'
72      }
73    );
74  });
75
76  [true, false, 'auto'].forEach((colorMode) => {
77    assert.throws(
78      () => {
79        new Console({
80          stdout: stream,
81          ignoreErrors: false,
82          colorMode: colorMode,
83          inspectOptions: {
84            colors: false
85          }
86        });
87      },
88      {
89        message: 'Option "options.inspectOptions.color" cannot be used in ' +
90                 'combination with option "colorMode"',
91        code: 'ERR_INCOMPATIBLE_OPTION_PAIR'
92      }
93    );
94  });
95}
96