Skip to content

Commit

Permalink
assert: add partial support for evaluated code in simple assert
Browse files Browse the repository at this point in the history
This makes sure using `assert.ok()` in `new Function()` statements
visualizes the actual call site in the error message.

PR-URL: #27781
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR committed Jun 17, 2019
1 parent ef8f147 commit f03241f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
55 changes: 34 additions & 21 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,31 @@ function getErrMessage(message, fn) {
Error.prepareStackTrace = tmpPrepare;

const filename = call.getFileName();

if (!filename) {
return message;
}

const line = call.getLineNumber() - 1;
let column = call.getColumnNumber() - 1;
let identifier;
let code;

const identifier = `${filename}${line}${column}`;
if (filename) {
identifier = `${filename}${line}${column}`;

if (errorCache.has(identifier)) {
return errorCache.get(identifier);
// Skip Node.js modules!
if (filename.endsWith('.js') &&
NativeModule.exists(filename.slice(0, -3))) {
errorCache.set(identifier, undefined);
return;
}
} else {
const fn = call.getFunction();
if (!fn) {
return message;
}
code = String(fn);
identifier = `${code}${line}${column}`;
}

// Skip Node.js modules!
if (filename.endsWith('.js') && NativeModule.exists(filename.slice(0, -3))) {
errorCache.set(identifier, undefined);
return;
if (errorCache.has(identifier)) {
return errorCache.get(identifier);
}

let fd;
Expand All @@ -295,16 +302,22 @@ function getErrMessage(message, fn) {
// errors are handled faster.
Error.stackTraceLimit = 0;

if (decoder === undefined) {
const { StringDecoder } = require('string_decoder');
decoder = new StringDecoder('utf8');
if (filename) {
if (decoder === undefined) {
const { StringDecoder } = require('string_decoder');
decoder = new StringDecoder('utf8');
}
fd = openSync(filename, 'r', 0o666);
// Reset column and message.
[column, message] = getCode(fd, line, column);
// Flush unfinished multi byte characters.
decoder.end();
} else {
for (let i = 0; i < line; i++) {
code = code.slice(code.indexOf('\n') + 1);
}
[column, message] = parseCode(code, column);
}

fd = openSync(filename, 'r', 0o666);
// Reset column and message.
[column, message] = getCode(fd, line, column);
// Flush unfinished multi byte characters.
decoder.end();
// Always normalize indentation, otherwise the message could look weird.
if (message.includes('\n')) {
if (EOL === '\r\n') {
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,13 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'The expression evaluated to a falsy value:\n\n assert(1 === 2)\n'
}
);
assert.throws(
() => eval('console.log("FOO");\nassert.ok(1 === 2);'),
{
code: 'ERR_ASSERTION',
message: 'false == true'
}
);
Expand Down

0 comments on commit f03241f

Please sign in to comment.