-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: multiple improvements #21628
Conversation
lib/assert.js
Outdated
@@ -128,8 +128,10 @@ function getBuffer(fd, assertLine) { | |||
let startBuffer = 0; // Start reading from that char on | |||
const bytesPerRead = 8192; | |||
const buffers = []; | |||
// Use a single buffer up front that is reused until the call site is found. | |||
let buffer = Buffer.allocUnsafe(bytesPerRead); |
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.
💯
lib/assert.js
Outdated
nodes = parseExpressionAt(code, start); | ||
if (nodes.type !== 'CallExpression') { | ||
// Walk the tree to find the call expression. | ||
const walk = require('internal/deps/acorn/dist/walk'); |
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.
Perhaps make this a more "traditional" lazy load by having a top level let walk;
then here walk = walk || require('internal/deps/acorn/dist/walk')
or something similar?
it might help to just remove the +/- and use colors. i generally try to just focus on the colors because the +/- add another dimension that confuses the heck outta me. |
@devsnek using only colors is not an option in case people do not have a TTY with color support. In the end the color is just the same as + / - and only a different notation for the same thing. |
5a7225c
to
dfb54bf
Compare
I moved the simple assert changes out of this PR. I guess the rest is fine to be in one commit. So PTAL. |
@nodejs/tsc @nodejs/testing |
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.
Diff changes tested and approved :)
Just a few questions on the messages
lib/internal/assert.js
Outdated
strictEqual: 'Input A expected to strictly equal input B', | ||
notStrictEqual: 'Input A expected to strictly not equal input B' | ||
const kReadableOperator = { | ||
deepStrictEqual: 'Expected input to be strictly deep-equal', |
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.
suggestion: "inputs" instead of "input"
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 am good either way as it seems that both would work. @Trott @vsemozhetbyt do you have a preference?
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.
For me, "inputs" seems more clear (no confusing "input is deep-equal — to what?"), but I am not a native speaker)
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 agree with @targos and @vsemozhetbyt for the reason offered by @vsemozhetbyt (using "inputs" removes potential ambiguity).
lib/internal/assert.js
Outdated
notStrictEqual: 'Input A expected to strictly not equal input B' | ||
const kReadableOperator = { | ||
deepStrictEqual: 'Expected input to be strictly deep-equal', | ||
notDeepStrictEqual: 'Expected "actual" not to be strictly deep-equal', |
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.
why use "actual" here?
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.
It is the value of the input argument "actual". I had hoped that this is actually clearer than the version before. Do you have a idea or a suggestion for to make it even better?
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'm wondering why it's not input
or inputs
like with the sibling method
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.
It is a combination of this message including the whole other output. Without the further output I agree with you.
If we do a equal comparison the output would look like:
assert.deepStrictEqual([1, 2, 3], [1, 2, 2]);
// Expected inputs to be strictly deep-equal:
// + actual - expected
//
//
// [
// 1,
// 2,
// + 3
// - 2
// ]
While a not equal diff looks like:
assert.notDeepStrictEqual([1, 2, 3], [1, 2, 3]);
// Expected "actual" not be strictly deep-equal
//
// [
// 1,
// 2,
// 3
// ]
So the first part compares two different inputs (actual & expected) while the latter says that "actual" should not be equal to the afterwards printed value.
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 see. Thanks for the explanation, this seems fine.
Comment addressed. |
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.
LGTM with a nit.
lib/internal/assert.js
Outdated
(actual !== 0 || expected !== 0)) { // -0 === +0 | ||
return `${kReadableOperator[operator]}:\n\n` + | ||
`${actualLines[0]} !== ${expectedLines[0]}\n`; | ||
} |
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.
Can you please add a comment explaining this block of logic?
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.
Done.
lib/internal/assert.js
Outdated
// If the stdout is a tty and the input length is lower than the current | ||
// columns per line, add a mismatch indicator below the output. | ||
} else if (process.stdout.isTTY && | ||
inputLength < process.stdout.columns) { |
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’d prefer not to add a dependency on process.stdout
here … there’s no guarantee that this is where the error output ends up, and it makes assert
depend on something it shouldn’t depend on (or makes it more so, at least).
Can we pick a value, e.g. 80 or 100 or so?
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 am aware that there is no guarantee that this is where the error output ends up. It is also the reason why I only added the indicator if it is a tty (and yes, even then the error could end up somewhere else but that is unlikely).
If I understand you correct you would actually like to have the indicator all the time? In that case I could add a default of 80 in case it is not a tty and check stdout otherwise?
That all aside: I would actually like to add a proper diffing algorithm at some point with which it would be possible to use colors to highlight the differences instead. But that is a bit more work and I got quite some pushback when it comes to improvements to assert, so I am not really sure if that is something worth trying.
1) Switched + / - and red / green in diffs. It seems like that style is more natural to most people. 2) Short primitives do not use the diff anymore. Especially short numbers can be read well like 1 !== 2. Cases that can not be displayed like that (e.g., -0 and +0) use the regular diff output. 3) Improved error descriptions. It was not always clear what the messages stood for. That should now be resolved. 4) Added a position indicator for single lines in case a tty is used and the line is shorter than the visual columns.
e0dec36
to
e789901
Compare
@addaleax I added a default of 80 for all cases where stdout is not a tty. @targos I also added a "to" to make things even clearer (it addresses #21628 (comment)). I also moved some code up to remove a intermediate variable and to shorten the code path in that case. |
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.
Seems good to me at a high level.
I just changed two more small things:
|
I just pushed the doc changes that were missing. The CI was green after resuming as it can be seen by the green check mark next to the commit before the docs. Since the doc changes do not need a full CI, I did not start anything else anymore. PTAL. I think this is ready otherwise and could land. |
PTAL. It would be nice to get LG for the last changes. |
@nodejs/documentation PTAL |
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.
Doc changes LGTM)
Just a nit question. Sometimes in the error logs we have |
The line length was to long when adding the |
1) Switched + / - and red / green in diffs. It seems like that style is more natural to most people. 2) Short primitives do not use the diff anymore. Especially short numbers can be read well like 1 !== 2. Cases that can not be displayed like that (e.g., -0 and +0) use the regular diff output. 3) Improved error descriptions. It was not always clear what the messages stood for. That should now be resolved. 4) Added a position indicator for single lines in case a tty is used and the line is shorter than the visual columns. 5) Color detection is now done by checking stderr instead of stdout. PR-URL: nodejs#21628 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Thanks a lot. Landed in 0518b9e 🎉 |
1) Switched + / - and red / green in diffs. It seems like that style is more natural to most people. 2) Short primitives do not use the diff anymore. Especially short numbers can be read well like 1 !== 2. Cases that can not be displayed like that (e.g., -0 and +0) use the regular diff output. 3) Improved error descriptions. It was not always clear what the messages stood for. That should now be resolved. 4) Added a position indicator for single lines in case a tty is used and the line is shorter than the visual columns. 5) Color detection is now done by checking stderr instead of stdout. PR-URL: nodejs#21628 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
I am going to split this into smaller commits. Therefore it is WIP.The following changes are in here:
+ / -
|red / green
(by request). It does seem to feel a bit more natural this way. Let's hope that most people agree with this.1 !== 2
.Simple assert will now rarely bail out when checking the call site and the output got improved.Some performance optimizationsChecklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes