-
Notifications
You must be signed in to change notification settings - Fork 29.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assert: do not read Node.js modules #18322
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,18 @@ | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
// Flags: --expose-internals | ||
|
||
'use strict'; | ||
|
||
/* eslint-disable prefer-common-expectserror */ | ||
|
||
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'); | ||
const a = assert; | ||
|
||
function makeBlock(f) { | ||
|
@@ -914,6 +919,7 @@ common.expectsError( | |
{ | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
generatedMessage: true, | ||
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` + | ||
`assert.ok(null)${EOL}` | ||
} | ||
|
@@ -923,6 +929,7 @@ 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}` | ||
} | ||
|
@@ -1006,7 +1013,8 @@ common.expectsError( | |
{ | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
message: '0 == true' | ||
message: '0 == true', | ||
generatedMessage: true | ||
} | ||
); | ||
|
||
|
@@ -1015,10 +1023,43 @@ common.expectsError( | |
{ | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
message: 'test' | ||
message: 'test', | ||
generatedMessage: false | ||
} | ||
); | ||
|
||
// Do not try to check Node.js modules. | ||
{ | ||
const e = new EventEmitter(); | ||
|
||
e.on('hello', assert); | ||
|
||
let threw = false; | ||
try { | ||
e.emit('hello', false); | ||
} catch (err) { | ||
const frames = err.stack.split('\n'); | ||
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be affected by internal changes of events and assert...can you look for the frame that contains There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I am not sure I can follow. I explicitly tried to write the test in a way that it will not be affected by internal changes. It should keep on working no matter if the lines are moved or if the specific code is moved to another file. It depends on the thrown error with the information about the filename etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh, so this frame is guaranteed to belong to where |
||
// Reset the cache to check again | ||
errorCache.delete(`${filename}${line - 1}${column - 1}`); | ||
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` + | ||
'ok(failed(badly));'; | ||
try { | ||
writeFileSync(filename, data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to be so late to this, but can you explain why this is writing a file in project root? Is the idea to make sure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I just saw this right now. What this test makes sure is that if |
||
assert.throws( | ||
() => e.emit('hello', false), | ||
{ | ||
message: 'false == true' | ||
} | ||
); | ||
threw = true; | ||
} finally { | ||
unlinkSync(filename); | ||
} | ||
} | ||
assert(threw); | ||
} | ||
|
||
common.expectsError( | ||
// eslint-disable-next-line no-restricted-syntax | ||
() => assert.throws(() => {}, 'Error message', 'message'), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might deserve a separate test file since
test-assert.js
is already 1000+ lines long..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would like to keep this test together with the other simple assert tests. I am fine to move all of those to another file though and I agree that this file is a bit long.