Skip to content

Commit

Permalink
assert: fix EOL issue in messages on Windows
Browse files Browse the repository at this point in the history
PR-URL: nodejs#20754
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
BridgeAR committed May 18, 2018
1 parent 3424f71 commit 2c42999
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
17 changes: 10 additions & 7 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,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 @@ -168,7 +168,7 @@ function getErrMessage(call) {
return;
}

var fd;
let fd, message;
try {
fd = openSync(filename, 'r', 0o666);
const buffers = getBuffer(fd, line);
Expand All @@ -187,11 +187,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');
}
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

0 comments on commit 2c42999

Please sign in to comment.