Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert,tty: fix indicator and warning #31429

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const { inspect } = require('internal/util/inspect');
const { codes: {
ERR_INVALID_ARG_TYPE
} } = require('internal/errors');
const {
removeColors,
} = require('internal/util');

let blue = '';
let green = '';
Expand Down Expand Up @@ -93,7 +96,12 @@ function createErrDiff(actual, expected, operator) {
// equal, check further special handling.
if (actualLines.length === 1 && expectedLines.length === 1 &&
actualLines[0] !== expectedLines[0]) {
const inputLength = actualLines[0].length + expectedLines[0].length;
// Check for the visible length using the `removeColors()` function, if
// appropriate.
const c = inspect.defaultOptions.colors;
const actualRaw = c ? removeColors(actualLines[0]) : actualLines[0];
const expectedRaw = c ? removeColors(expectedLines[0]) : expectedLines[0];
const inputLength = actualRaw.length + expectedRaw.length;
// If the character length of "actual" and "expected" together is less than
// kMaxShortLength and if neither is an object and at least one of them is
// not `zero`, use the strict equal comparison to visualize the output.
Expand All @@ -110,7 +118,7 @@ function createErrDiff(actual, expected, operator) {
// not a tty, use a default value of 80 characters.
const maxLength = process.stderr.isTTY ? process.stderr.columns : 80;
if (inputLength < maxLength) {
while (actualLines[0][i] === expectedLines[0][i]) {
while (actualRaw[i] === expectedRaw[i]) {
i++;
}
// Ignore the first characters.
Expand Down
16 changes: 12 additions & 4 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,26 @@ const TERM_ENVS_REG_EXP = [
/^vt100/
];

let warned = false;
function warnOnDeactivatedColors(env) {
let name;
if (warned)
return;
let name = '';
if (env.NODE_DISABLE_COLORS !== undefined)
name = 'NODE_DISABLE_COLORS';
if (env.NO_COLOR !== undefined)
name = 'NO_COLOR';
if (env.NO_COLOR !== undefined) {
if (name !== '') {
name += "' and '";
}
name += 'NO_COLOR';
}

if (name !== undefined) {
if (name !== '') {
process.emitWarning(
`The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`,
'Warning'
);
warned = true;
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1467,3 +1467,16 @@ assert.throws(
);
assert.doesNotMatch('I will pass', /different$/);
}

{
const tempColor = inspect.defaultOptions.colors;
assert.throws(() => {
inspect.defaultOptions.colors = true;
// Guarantee the position indicator is placed correctly.
assert.strictEqual(111554n, 11111115);
}, (err) => {
assert.strictEqual(inspect(err).split('\n')[5], ' ^');
inspect.defaultOptions.colors = tempColor;
return true;
});
}
14 changes: 6 additions & 8 deletions test/pseudo-tty/test-assert-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
require('../common');
const assert = require('assert').strict;

try {
// Activate colors even if the tty does not support colors.
process.env.COLORTERM = '1';
// Make sure TERM is not set to e.g., 'dumb' and NODE_DISABLE_COLORS is not
// active.
process.env.TERM = 'FOOBAR';
assert.throws(() => {
process.env.FORCE_COLOR = '1';
delete process.env.NODE_DISABLE_COLORS;
delete process.env.NO_COLOR;
assert.deepStrictEqual([1, 2, 2, 2, 2], [2, 2, 2, 2, 2]);
} catch (err) {
}, (err) => {
const expected = 'Expected values to be strictly deep-equal:\n' +
'\u001b[32m+ actual\u001b[39m \u001b[31m- expected\u001b[39m' +
' \u001b[34m...\u001b[39m Lines skipped\n\n' +
Expand All @@ -23,4 +20,5 @@ try {
' 2\n' +
' ]';
assert.strictEqual(err.message, expected);
}
return true;
});
8 changes: 8 additions & 0 deletions test/pseudo-tty/test-tty-color-support-warning-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

require('../common');

process.env.NODE_DISABLE_COLORS = '1';
process.env.FORCE_COLOR = '3';

console.log();
2 changes: 2 additions & 0 deletions test/pseudo-tty/test-tty-color-support-warning-2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
9 changes: 9 additions & 0 deletions test/pseudo-tty/test-tty-color-support-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('../common');

process.env.NO_COLOR = '1';
process.env.NODE_DISABLE_COLORS = '1';
process.env.FORCE_COLOR = '3';

console.log();
2 changes: 2 additions & 0 deletions test/pseudo-tty/test-tty-color-support-warning.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
7 changes: 0 additions & 7 deletions test/pseudo-tty/test-tty-color-support.out
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved