Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: fix EOL issue in messages on Windows #20754

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ assert.fail = fail;
assert.AssertionError = AssertionError;

function getBuffer(fd, assertLine) {
var lines = 0;
let lines = 0;
// Prevent blocking the event loop by limiting the maximum amount of
// data that may be read.
var maxReads = 64; // bytesPerRead * maxReads = 512 kb
var bytesRead = 0;
var startBuffer = 0; // Start reading from that char on
let maxReads = 64; // bytesPerRead * maxReads = 512 kb
let bytesRead = 0;
let startBuffer = 0; // Start reading from that char on
const bytesPerRead = 8192;
const buffers = [];
do {
Expand Down Expand Up @@ -163,7 +163,7 @@ function getErrMessage(call) {
return;
}

var fd;
let fd, message;
try {
fd = openSync(filename, 'r', 0o666);
const buffers = getBuffer(fd, line);
Expand All @@ -182,11 +182,14 @@ function getErrMessage(call) {
// not user defined function names.
const ok = name === 'ok' ? '.ok' : '';
const args = node.arguments;
var message = code
message = code
.slice(args[0].start, args[args.length - 1].end)
.replace(escapeSequencesRegExp, escapeFn);
if (EOL === '\r\n') {
message = message.replace(/\r\n/g, '\n');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am just bikeshedding, but what if the user deliberately put \r\n in their custom messages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is not a user message. It is the code that is read and parsed from the file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BridgeAR Oh, didn't look at this close enough, sorry. In that case it should probably be fine.

}
message = 'The expression evaluated to a falsy value:' +
`${EOL}${EOL} assert${ok}(${message})${EOL}`;
`\n\n assert${ok}(${message})\n`;
}
// Make sure to always set the cache! No matter if the message is
// undefined or not
Expand Down
37 changes: 18 additions & 19 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

const common = require('../common');
const assert = require('assert');
const { EOL } = require('os');
const EventEmitter = require('events');
const { errorCache } = require('internal/assert');
const { writeFileSync, unlinkSync } = require('fs');
Expand Down Expand Up @@ -462,8 +461,8 @@ assert.throws(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert.ok(typeof 123 === 'string')${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
"assert.ok(typeof 123 === 'string')\n"
}
);
Error.stackTraceLimit = tmpLimit;
Expand Down Expand Up @@ -625,8 +624,8 @@ common.expectsError(
code: 'ERR_ASSERTION',
type: assert.AssertionError,
generatedMessage: true,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert.ok(null)${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
'assert.ok(null)\n'
}
);
common.expectsError(
Expand All @@ -635,8 +634,8 @@ common.expectsError(
code: 'ERR_ASSERTION',
type: assert.AssertionError,
generatedMessage: true,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert(typeof 123 === 'string')${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
"assert(typeof 123 === 'string')\n"
}
);

Expand Down Expand Up @@ -666,17 +665,17 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert(Buffer.from('test') instanceof Error)${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
"assert(Buffer.from('test') instanceof Error)\n"
}
);
common.expectsError(
() => throwErr(),
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert(Buffer.from('test') instanceof Error)${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
"assert(Buffer.from('test') instanceof Error)\n"
}
);
fs.close = tmp;
Expand All @@ -695,12 +694,12 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert((() => 'string')()${EOL}` +
` // eslint-disable-next-line${EOL}` +
` ===${EOL}` +
` 123 instanceof${EOL}` +
` Buffer)${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
"assert((() => 'string')()\n" +
' // eslint-disable-next-line\n' +
' ===\n' +
' 123 instanceof\n' +
' Buffer)\n'
}
);

Expand All @@ -709,8 +708,8 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert(null, undefined)${EOL}`
message: 'The expression evaluated to a falsy value:\n\n ' +
'assert(null, undefined)\n'
}
);

Expand Down