2018-03-15 13:09:17 +00:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const util = require('util');
|
|
|
|
const { Writable } = require('stream');
|
|
|
|
const { Console } = require('console');
|
|
|
|
|
2018-12-12 02:26:15 +00:00
|
|
|
function check(isTTY, colorMode, expectedColorMode, inspectOptions) {
|
2018-03-15 13:09:17 +00:00
|
|
|
const items = [
|
|
|
|
1,
|
|
|
|
{ a: 2 },
|
|
|
|
[ 'foo' ],
|
2021-03-26 15:51:08 +00:00
|
|
|
{ '\\a': '\\bar' },
|
2018-03-15 13:09:17 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
const stream = new Writable({
|
|
|
|
write: common.mustCall((chunk, enc, cb) => {
|
|
|
|
assert.strictEqual(chunk.trim(),
|
|
|
|
util.inspect(items[i++], {
|
2018-12-12 02:26:15 +00:00
|
|
|
colors: expectedColorMode,
|
|
|
|
...inspectOptions
|
2018-03-15 13:09:17 +00:00
|
|
|
}));
|
|
|
|
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,
|
2018-12-12 02:26:15 +00:00
|
|
|
colorMode,
|
|
|
|
inspectOptions
|
2018-03-15 13:09:17 +00:00
|
|
|
});
|
|
|
|
for (const item of items) {
|
|
|
|
testConsole.log(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check(true, 'auto', true);
|
|
|
|
check(false, 'auto', false);
|
2018-12-12 02:26:15 +00:00
|
|
|
check(false, undefined, true, { colors: true, compact: false });
|
|
|
|
check(true, 'auto', true, { compact: false });
|
|
|
|
check(true, undefined, false, { colors: false });
|
2018-03-15 13:09:17 +00:00
|
|
|
check(true, true, true);
|
|
|
|
check(false, true, true);
|
|
|
|
check(true, false, false);
|
|
|
|
check(false, false, false);
|
2018-06-10 21:47:12 +00:00
|
|
|
|
2018-12-12 02:26:15 +00:00
|
|
|
// Check invalid options.
|
2018-06-10 21:47:12 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{
|
2024-08-10 01:49:40 +00:00
|
|
|
message: `The argument 'colorMode' must be one of: 'auto', true, false. Received ${received}`,
|
2018-06-10 21:47:12 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2018-12-12 02:26:15 +00:00
|
|
|
|
|
|
|
[true, false, 'auto'].forEach((colorMode) => {
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
new Console({
|
|
|
|
stdout: stream,
|
|
|
|
ignoreErrors: false,
|
|
|
|
colorMode: colorMode,
|
|
|
|
inspectOptions: {
|
|
|
|
colors: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{
|
2020-03-24 22:17:02 +00:00
|
|
|
message: 'Option "options.inspectOptions.color" cannot be used in ' +
|
2018-12-12 02:26:15 +00:00
|
|
|
'combination with option "colorMode"',
|
|
|
|
code: 'ERR_INCOMPATIBLE_OPTION_PAIR'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2018-06-10 21:47:12 +00:00
|
|
|
}
|