0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-console-tty-colors.js
HEESEUNG 90d91abbfe
console: use validateOneOf for colorMode validation
refactor the Console constructor to use validateOneOf for validating
the colorMode parameter.

PR-URL: https://github.com/nodejs/node/pull/54245
Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2024-08-10 01:49:40 +00:00

96 lines
2.3 KiB
JavaScript

'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' must be one of: 'auto', true, false. 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'
}
);
});
}