diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index a77414834d1e32..691f057ad281e3 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -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 = ''; @@ -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. @@ -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. diff --git a/lib/internal/tty.js b/lib/internal/tty.js index 98975fa68a436f..44d51737008eb0 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -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; } } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 23e1734aff235c..9f45fbb43abdb9 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -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; + }); +} diff --git a/test/pseudo-tty/test-assert-colors.js b/test/pseudo-tty/test-assert-colors.js index a241542c34cebf..9d5a923aa0f8e7 100644 --- a/test/pseudo-tty/test-assert-colors.js +++ b/test/pseudo-tty/test-assert-colors.js @@ -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' + @@ -23,4 +20,5 @@ try { ' 2\n' + ' ]'; assert.strictEqual(err.message, expected); -} + return true; +}); diff --git a/test/pseudo-tty/test-tty-color-support-warning-2.js b/test/pseudo-tty/test-tty-color-support-warning-2.js new file mode 100644 index 00000000000000..34ba49c0dfd1b5 --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning-2.js @@ -0,0 +1,8 @@ +'use strict'; + +require('../common'); + +process.env.NODE_DISABLE_COLORS = '1'; +process.env.FORCE_COLOR = '3'; + +console.log(); diff --git a/test/pseudo-tty/test-tty-color-support-warning-2.out b/test/pseudo-tty/test-tty-color-support-warning-2.out new file mode 100644 index 00000000000000..cd57b3f9f69672 --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning-2.out @@ -0,0 +1,2 @@ + +(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. diff --git a/test/pseudo-tty/test-tty-color-support-warning.js b/test/pseudo-tty/test-tty-color-support-warning.js new file mode 100644 index 00000000000000..ae164958d2d6b9 --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning.js @@ -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(); diff --git a/test/pseudo-tty/test-tty-color-support-warning.out b/test/pseudo-tty/test-tty-color-support-warning.out new file mode 100644 index 00000000000000..9360ae1328b2cd --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning.out @@ -0,0 +1,2 @@ + +(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. diff --git a/test/pseudo-tty/test-tty-color-support.out b/test/pseudo-tty/test-tty-color-support.out index f73fbdcd9651f6..e6904d10b8a7b8 100644 --- a/test/pseudo-tty/test-tty-color-support.out +++ b/test/pseudo-tty/test-tty-color-support.out @@ -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. -(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.