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:
parent
3424f71cc9
commit
2c42999f7e
@ -120,12 +120,12 @@ assert.fail = fail;
|
|||||||
assert.AssertionError = AssertionError;
|
assert.AssertionError = AssertionError;
|
||||||
|
|
||||||
function getBuffer(fd, assertLine) {
|
function getBuffer(fd, assertLine) {
|
||||||
var lines = 0;
|
let lines = 0;
|
||||||
// Prevent blocking the event loop by limiting the maximum amount of
|
// Prevent blocking the event loop by limiting the maximum amount of
|
||||||
// data that may be read.
|
// data that may be read.
|
||||||
var maxReads = 64; // bytesPerRead * maxReads = 512 kb
|
let maxReads = 64; // bytesPerRead * maxReads = 512 kb
|
||||||
var bytesRead = 0;
|
let bytesRead = 0;
|
||||||
var startBuffer = 0; // Start reading from that char on
|
let startBuffer = 0; // Start reading from that char on
|
||||||
const bytesPerRead = 8192;
|
const bytesPerRead = 8192;
|
||||||
const buffers = [];
|
const buffers = [];
|
||||||
do {
|
do {
|
||||||
@ -168,7 +168,7 @@ function getErrMessage(call) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fd;
|
let fd, message;
|
||||||
try {
|
try {
|
||||||
fd = openSync(filename, 'r', 0o666);
|
fd = openSync(filename, 'r', 0o666);
|
||||||
const buffers = getBuffer(fd, line);
|
const buffers = getBuffer(fd, line);
|
||||||
@ -187,11 +187,14 @@ function getErrMessage(call) {
|
|||||||
// not user defined function names.
|
// not user defined function names.
|
||||||
const ok = name === 'ok' ? '.ok' : '';
|
const ok = name === 'ok' ? '.ok' : '';
|
||||||
const args = node.arguments;
|
const args = node.arguments;
|
||||||
var message = code
|
message = code
|
||||||
.slice(args[0].start, args[args.length - 1].end)
|
.slice(args[0].start, args[args.length - 1].end)
|
||||||
.replace(escapeSequencesRegExp, escapeFn);
|
.replace(escapeSequencesRegExp, escapeFn);
|
||||||
|
if (EOL === '\r\n') {
|
||||||
|
message = message.replace(/\r\n/g, '\n');
|
||||||
|
}
|
||||||
message = 'The expression evaluated to a falsy value:' +
|
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
|
// Make sure to always set the cache! No matter if the message is
|
||||||
// undefined or not
|
// undefined or not
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
|
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const { EOL } = require('os');
|
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { errorCache } = require('internal/assert');
|
const { errorCache } = require('internal/assert');
|
||||||
const { writeFileSync, unlinkSync } = require('fs');
|
const { writeFileSync, unlinkSync } = require('fs');
|
||||||
@ -462,8 +461,8 @@ assert.throws(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert.ok(typeof 123 === 'string')${EOL}`
|
"assert.ok(typeof 123 === 'string')\n"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
Error.stackTraceLimit = tmpLimit;
|
Error.stackTraceLimit = tmpLimit;
|
||||||
@ -625,8 +624,8 @@ common.expectsError(
|
|||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
generatedMessage: true,
|
generatedMessage: true,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert.ok(null)${EOL}`
|
'assert.ok(null)\n'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
@ -635,8 +634,8 @@ common.expectsError(
|
|||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
generatedMessage: true,
|
generatedMessage: true,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert(typeof 123 === 'string')${EOL}`
|
"assert(typeof 123 === 'string')\n"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -666,8 +665,8 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert(Buffer.from('test') instanceof Error)${EOL}`
|
"assert(Buffer.from('test') instanceof Error)\n"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
@ -675,8 +674,8 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert(Buffer.from('test') instanceof Error)${EOL}`
|
"assert(Buffer.from('test') instanceof Error)\n"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
fs.close = tmp;
|
fs.close = tmp;
|
||||||
@ -695,12 +694,12 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert((() => 'string')()${EOL}` +
|
"assert((() => 'string')()\n" +
|
||||||
` // eslint-disable-next-line${EOL}` +
|
' // eslint-disable-next-line\n' +
|
||||||
` ===${EOL}` +
|
' ===\n' +
|
||||||
` 123 instanceof${EOL}` +
|
' 123 instanceof\n' +
|
||||||
` Buffer)${EOL}`
|
' Buffer)\n'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -709,8 +708,8 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
|
message: 'The expression evaluated to a falsy value:\n\n ' +
|
||||||
`assert(null, undefined)${EOL}`
|
'assert(null, undefined)\n'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user