Skip to content

assert: multiple improvements - #21628

Closed
BridgeAR wants to merge 6 commits into
nodejs:masterfrom
BridgeAR:improve-assert-things
Closed

assert: multiple improvements#21628
BridgeAR wants to merge 6 commits into
nodejs:masterfrom
BridgeAR:improve-assert-things

Conversation

@BridgeAR

@BridgeARBridgeAR commented Jul 3, 2018

Copy link
Copy Markdown
Member

I am going to split this into smaller commits. Therefore it is WIP.

The following changes are in here:

  • Switched + / - | red / green (by request). It does seem to feel a bit more natural this way. Let's hope that most people agree with this.
  • Short primitives do not use the diff anymore (by request). Especially short numbers can be read well like 1 !== 2.
  • Improved error descriptions. It was not really clear how things worked. It should now be clear what everything is about.
  • Added a position indicator for single lines that fit into the tty.
  • Simple assert will now rarely bail out when checking the call site and the output got improved.
  • Some performance optimizations
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@BridgeARBridgeAR added wip Issues and PRs that are still a work in progress. semver-major PRs that contain breaking changes and should be released in the next major version. labels Jul 3, 2018
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@BridgeAR
BridgeAR requested review from a team and devsnekJuly 3, 2018 00:25
@nodejs-github-botnodejs-github-bot added the assert Issues and PRs related to the assert subsystem. label Jul 3, 2018
Comment threadlib/assert.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Comment threadlib/assert.js Outdated

Copy link
Copy Markdown
Member

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?

@devsnek

Copy link
Copy Markdown
Member

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.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

@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.

@BridgeAR
BridgeARforce-pushed the improve-assert-things branch from 5a7225c to dfb54bfCompareJuly 13, 2018 19:20
@BridgeARBridgeAR removed the wip Issues and PRs that are still a work in progress. label Jul 13, 2018
@BridgeAR

Copy link
Copy Markdown
MemberAuthor

I moved the simple assert changes out of this PR. I guess the rest is fine to be in one commit.

So PTAL.

@BridgeAR
BridgeAR requested a review from a teamJuly 14, 2018 01:54
@Trott

Copy link
Copy Markdown
Member

@nodejs/tsc @nodejs/testing

@targostargos left a comment

Copy link
Copy Markdown
Member

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

Comment threadlib/internal/assert.js Outdated

Copy link
Copy Markdown
Member

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"

Copy link
Copy Markdown
MemberAuthor

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?

Copy link
Copy Markdown
Contributor

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)

Copy link
Copy Markdown
Member

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).

Comment threadlib/internal/assert.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use "actual" here?

Copy link
Copy Markdown
MemberAuthor

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?

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

@BridgeAR
BridgeAR requested a review from a teamJuly 16, 2018 09:30

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a nit.

Comment threadlib/internal/assert.js Outdated

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

@BridgeAR
BridgeAR requested a review from a teamJuly 16, 2018 15:46
@BridgeARBridgeAR mentioned this pull request Jul 18, 2018
4 tasks
@BridgeAR
BridgeAR requested a review from jasnellJuly 27, 2018 01:45
Comment threadlib/internal/assert.js Outdated

Copy link
Copy Markdown
Member

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?

@BridgeARBridgeARJul 27, 2018

Copy link
Copy Markdown
MemberAuthor

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.
@BridgeAR
BridgeARforce-pushed the improve-assert-things branch from e0dec36 to e789901CompareJuly 27, 2018 13:19
@BridgeAR

BridgeAR commented Jul 27, 2018

Copy link
Copy Markdown
MemberAuthor

@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.

CI https://ci.nodejs.org/job/node-test-pull-request/16029/

@TrottTrott left a comment

Copy link
Copy Markdown
Member

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.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

I just changed two more small things:

  1. Instead of checking stdout, it will now check stderr as it is more likely that the error ends up there.
  2. The indicator is ignored for the first three characters.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

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.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

PTAL. It would be nice to get LG for the last changes.

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

@nodejs/documentation PTAL

@vsemozhetbytvsemozhetbyt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc changes LGTM)

@vsemozhetbyt

Copy link
Copy Markdown
Contributor

Just a nit question. Sometimes in the error logs we have AssertionError [ERR_ASSERTION]: ... and sometimes just AssertionError: .... Is there some system in this?

@BridgeAR

Copy link
Copy Markdown
MemberAuthor

The line length was to long when adding the ERR_ASSERTION every time. Therefore it was some times removed.

BridgeAR added a commit to BridgeAR/node that referenced this pull request Aug 4, 2018
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>
@BridgeAR

Copy link
Copy Markdown
MemberAuthor

Thanks a lot.

Landed in 0518b9e 🎉

@BridgeARBridgeAR closed this Aug 4, 2018
oyyd pushed a commit to oyyd/node that referenced this pull request Sep 25, 2018
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>
@oyydoyyd mentioned this pull request Sep 26, 2018
2 tasks
@BridgeAR
BridgeAR deleted the improve-assert-things branch January 20, 2020 11:34
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assertIssues and PRs related to the assert subsystem.semver-majorPRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@BridgeAR@nodejs-github-bot@devsnek@Trott@vsemozhetbyt@mcollina@jasnell@addaleax@targos