From 338e663860b8b53281418b22fbdfc87c18b3a3c6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 15 May 2018 18:44:19 +0200 Subject: [PATCH] assert: fix EOL issue in messages on Windows PR-URL: https://github.com/nodejs/node/pull/20754 Reviewed-By: Joyee Cheung Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat --- lib/assert.js | 17 ++++++++++------- test/parallel/test-assert.js | 37 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 98900c5151ed41..17b0059cba50e7 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -119,12 +119,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 { @@ -167,7 +167,7 @@ function getErrMessage(call) { return; } - var fd; + let fd, message; try { fd = openSync(filename, 'r', 0o666); const buffers = getBuffer(fd, line); @@ -186,11 +186,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 diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index e35ef326c34b86..d81ef32cd82210 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -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' } );