assert: align character indicators properly

This makes sure color codes are not taken into account in case
util.inspect's default value was changed.

PR-URL: https://github.com/nodejs/node/pull/31429
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Ruben Bridgewater committed 2020-02-09 13:39:54 +01:00
1 parent 1450ea7bf6
commit 36f328e21f
2 files changed
+23 -2

No files matched your search

+10 -2
View File
@@ -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.