Skip to content

Commit 014a2d1

Browse files
BridgeARMylesBorins
authored andcommitted
assert: fix wrong message indentation
If code is read from a file and that code is indented, it would be misaligned. This makes sure it has a natural indentation that is relative to the starting point of the assertion. PR-URL: #20791 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 3e7741c commit 014a2d1

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

lib/assert.js

+13
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ function getErrMessage(call) {
192192
if (EOL === '\r\n') {
193193
message = message.replace(/\r\n/g, '\n');
194194
}
195+
// Always normalize indentation, otherwise the message could look weird.
196+
if (message.indexOf('\n') !== -1) {
197+
const tmp = message.split('\n');
198+
message = tmp[0];
199+
for (var i = 1; i < tmp.length; i++) {
200+
let pos = 0;
201+
while (pos < column &&
202+
(tmp[i][pos] === ' ' || tmp[i][pos] === '\t')) {
203+
pos++;
204+
}
205+
message += `\n ${tmp[i].slice(pos)}`;
206+
}
207+
}
195208
message = 'The expression evaluated to a falsy value:' +
196209
`\n\n assert${ok}(${message})\n`;
197210
}

test/parallel/test-assert.js

+47-6
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,55 @@ common.expectsError(
694694
{
695695
code: 'ERR_ASSERTION',
696696
type: assert.AssertionError,
697-
message: 'The expression evaluated to a falsy value:\n\n ' +
698-
"assert((() => 'string')()\n" +
699-
' // eslint-disable-next-line\n' +
700-
' ===\n' +
701-
' 123 instanceof\n' +
702-
' Buffer)\n'
697+
message: 'The expression evaluated to a falsy value:\n\n' +
698+
' assert((() => \'string\')()\n' +
699+
' // eslint-disable-next-line\n' +
700+
' ===\n' +
701+
' 123 instanceof\n' +
702+
' Buffer)\n'
703+
}
704+
);
705+
706+
common.expectsError(
707+
() => {
708+
a(
709+
(() => 'string')()
710+
// eslint-disable-next-line
711+
===
712+
123 instanceof
713+
Buffer
714+
);
715+
},
716+
{
717+
code: 'ERR_ASSERTION',
718+
type: assert.AssertionError,
719+
message: 'The expression evaluated to a falsy value:\n\n' +
720+
' assert((() => \'string\')()\n' +
721+
' // eslint-disable-next-line\n' +
722+
' ===\n' +
723+
' 123 instanceof\n' +
724+
' Buffer)\n'
725+
}
726+
);
727+
728+
/* eslint-disable indent */
729+
common.expectsError(() => {
730+
a((
731+
() => 'string')() ===
732+
123 instanceof
733+
Buffer
734+
);
735+
}, {
736+
code: 'ERR_ASSERTION',
737+
type: assert.AssertionError,
738+
message: 'The expression evaluated to a falsy value:\n\n' +
739+
' assert((\n' +
740+
' () => \'string\')() ===\n' +
741+
' 123 instanceof\n' +
742+
' Buffer)\n'
703743
}
704744
);
745+
/* eslint-enable indent */
705746

706747
common.expectsError(
707748
() => assert(null, undefined),

0 commit comments

Comments
 (0)