From 25a18afa82ce48d91a63e7dc7cc319c23e28f1fa Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 8 Mar 2018 03:21:48 +0800 Subject: [PATCH] assert: do not use EOL in ERR_ASSERTION messages On Windows if an error is thrown from a script that uses `\n` to break lines - which is very common in the JavaScript ecosystem, and is the case in our own code base - then the error messages would contain mixed line feeds: the part coming from the source code breaks with `\n` while the message itself break with `\r\n`. Since we do not use `\r\n` in util.inspect(), we should use `\n` in error messages as well. --- lib/assert.js | 3 +-- test/parallel/test-assert.js | 37 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index f8eeba7222ec69..b35638ec376382 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -35,7 +35,6 @@ const { const { openSync, closeSync, readSync } = require('fs'); const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn'); const { inspect } = require('util'); -const { EOL } = require('os'); const { NativeModule } = require('internal/bootstrap_loaders'); // Escape control characters but not \n and \t to keep the line breaks and @@ -189,7 +188,7 @@ function getErrMessage(call) { .slice(args[0].start, args[args.length - 1].end) .replace(escapeSequencesRegExp, escapeFn); 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 2ea041e1e28dbd..068dd8e3d0bc4d 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/errors'); const { writeFileSync, unlinkSync } = require('fs'); @@ -422,8 +421,8 @@ common.expectsError( { 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; @@ -592,8 +591,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( @@ -602,8 +601,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' } ); @@ -623,8 +622,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( @@ -632,8 +631,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; @@ -652,12 +651,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' } ); @@ -666,8 +665,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' } );