Skip to content

Commit f756c80

Browse files
BridgeARcodebytere
authored andcommittedMar 30, 2020
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: #31429 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent c18cec7 commit f756c80

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎lib/internal/assert/assertion_error.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const { inspect } = require('internal/util/inspect');
1313
const { codes: {
1414
ERR_INVALID_ARG_TYPE
1515
} } = require('internal/errors');
16+
const {
17+
removeColors,
18+
} = require('internal/util');
1619

1720
let blue = '';
1821
let green = '';
@@ -93,7 +96,12 @@ function createErrDiff(actual, expected, operator) {
9396
// equal, check further special handling.
9497
if (actualLines.length === 1 && expectedLines.length === 1 &&
9598
actualLines[0] !== expectedLines[0]) {
96-
const inputLength = actualLines[0].length + expectedLines[0].length;
99+
// Check for the visible length using the `removeColors()` function, if
100+
// appropriate.
101+
const c = inspect.defaultOptions.colors;
102+
const actualRaw = c ? removeColors(actualLines[0]) : actualLines[0];
103+
const expectedRaw = c ? removeColors(expectedLines[0]) : expectedLines[0];
104+
const inputLength = actualRaw.length + expectedRaw.length;
97105
// If the character length of "actual" and "expected" together is less than
98106
// kMaxShortLength and if neither is an object and at least one of them is
99107
// not `zero`, use the strict equal comparison to visualize the output.
@@ -110,7 +118,7 @@ function createErrDiff(actual, expected, operator) {
110118
// not a tty, use a default value of 80 characters.
111119
const maxLength = process.stderr.isTTY ? process.stderr.columns : 80;
112120
if (inputLength < maxLength) {
113-
while (actualLines[0][i] === expectedLines[0][i]) {
121+
while (actualRaw[i] === expectedRaw[i]) {
114122
i++;
115123
}
116124
// Ignore the first characters.

‎test/parallel/test-assert.js

+13
Original file line numberDiff line numberDiff line change
@@ -1401,3 +1401,16 @@ assert.throws(
14011401
);
14021402
assert.doesNotMatch('I will pass', /different$/);
14031403
}
1404+
1405+
{
1406+
const tempColor = inspect.defaultOptions.colors;
1407+
assert.throws(() => {
1408+
inspect.defaultOptions.colors = true;
1409+
// Guarantee the position indicator is placed correctly.
1410+
assert.strictEqual(111554n, 11111115);
1411+
}, (err) => {
1412+
assert.strictEqual(inspect(err).split('\n')[5], ' ^');
1413+
inspect.defaultOptions.colors = tempColor;
1414+
return true;
1415+
});
1416+
}

0 commit comments

Comments
 (0)
Please sign in to comment.