assert: fix EOL issue in messages on Windows

PR-URL: https://github.com/nodejs/node/pull/20754
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-05-15 18:44:19 +02:00
parent 3424f71cc9
commit 2c42999f7e
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 28 additions and 26 deletions

View File

@ -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 {
@ -168,7 +168,7 @@ function getErrMessage(call) {
return;
}
var fd;
let fd, message;
try {
fd = openSync(filename, 'r', 0o666);
const buffers = getBuffer(fd, line);
@ -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

View File

@ -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');
@ -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;
@ -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(
@ -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"
}
);
@ -666,8 +665,8 @@ 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(
@ -675,8 +674,8 @@ 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"
}
);
fs.close = tmp;
@ -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'
}
);
@ -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'
}
);