Skip to content
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

tools, test: forbid string literal as third argument for assert.strictEqual() and friends #22849

Closed
wants to merge 12 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Sep 13, 2018

String literals provided as the third argument to assert.strictEqual() or assert.deepStrictEqual() will mask the values that are causing issues. Use a lint rule to prevent such usage.

@nodejs/testing @nodejs/linting

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

Remove string literal message in assert.strictEqual() call in napi test
testFinalizer.js.
Remove string literal message in assert.strictEqual() calls in
test-async-await.js.
Remove an unecessary string literal from assert.strictEqual() call in
test-inspector.js. The string literal is printed instead of the value
that causes an error. Removing the string literal allows the value that
caused the error to be printed. This improves the troubleshooting
experience when the test fails due to that assertion.
In test-http2-timeout-large-write.js and
test-http2-timeout-large-write-file.js:

Use assert.ok() on a boolean that the test itself creates and sets,
rather than assert.strictEqual(). This allows us to use a static message
without running afoul of the upcoming "do not use string literals with
assert.strictEqual()" lint rule.
Refactor test-vm-run-in-new-context so that check for `--expose-gc` flag
will not run afoul of an upcoming lint rule that checks that string
literals are not used for the `message` argument of
`assert.strictEqual()`.
Remove string literal from `assert.strictEqual()` call `message`
parameter and make it a comment above the assertion instead.
Remove string literal from assert.strictEqual message to improve output
of AssertionError.
Remove unnecessary string literal from assert.deepStrictEqual() call.
Remove string literal as assertion message in call to
assert.strictEqual() in test-dns-resolveany-bad-ancount.
Remove string literal as assertion message in call to
assert.strictEqual() in test-dns-lookup.
Make minor modifications to test-assert.js to prepare it for linting
rule that forbids the use of string literals for the third argument of
assert.strictEqual().
String literals provided as the third argument to assert.strictEqual()
or assert.deepStrictEqual() will mask the values that are causing
issues. Use a lint rule to prevent such usage.
@nodejs-github-bot nodejs-github-bot added the tools Issues and PRs related to the tools directory. label Sep 13, 2018
@Trott
Copy link
Member Author

Trott commented Sep 13, 2018

@Trott Trott added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 13, 2018
Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

I recently opened a PR to make use of the third argument. Prohibiting it seems the wrong way for me.

mscdex
mscdex previously requested changes Sep 14, 2018
Copy link
Contributor

@mscdex mscdex left a comment

Choose a reason for hiding this comment

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

I don't think we should forbid this. I could see some complicated situations where given enough detail in the custom message, a different error message could make things easier to understand what went wrong.

@Trott
Copy link
Member Author

Trott commented Sep 14, 2018

I don't think we should forbid this. I could see some complicated situations where given enough detail in the custom message, a different error message could make things easier to understand what went wrong.

@mcollina This doesn't forbid messages. It forbids string literals as messages as those are (almost) always anti-patterns. And in the unusual instances where a string literal makes sense, the rule can be disabled with a comment, just like we do for the occasional instance where it makes sense for a line to stretch out for more than 80 characters.

@mcollina
Copy link
Member

@Trott you tagged me in place of @mscdex

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@addaleax
Copy link
Member

I could see some complicated situations where given enough detail in the custom message, a different error message could make things easier to understand what went wrong.

@mscdex Just to be clear, this isn’t forbidding generating more complex messages – it forbids literals, i.e. purely static messages, because they nearly always obfuscate information.

@Trott
Copy link
Member Author

Trott commented Sep 14, 2018

@Trott you tagged me in place of @mscdex

Doh! Sorry about my error.

@Trott
Copy link
Member Author

Trott commented Sep 14, 2018

I recently opened a PR to make use of the third argument. Prohibiting it seems the wrong way for me.

@BridgeAR My preference would be:

  • Introduce this on master and backport to supported release lines.
  • Remove this from branches if and when your proposed change to message generation lands on each branch.

Does that work for you?

@BridgeAR
Copy link
Member

Yes, that works for me.

@Trott
Copy link
Member Author

Trott commented Sep 14, 2018

@mscdex Does this alleviate your concerns?

This doesn't forbid messages. It forbids string literals as messages as those are (almost) always anti-patterns. And in the unusual instances where a string literal makes sense, the rule can be disabled with a comment, just like we do for the occasional instance where it makes sense for a line to stretch out for more than 80 characters.

@refack
Copy link
Contributor

refack commented Sep 14, 2018

Since this PR "eats" human written institutional knowledge, I'm going to be petty, and ask to see the before and after message for those changes, so we can make sure we don't lose anything.

targos pushed a commit that referenced this pull request Sep 20, 2018
Remove string literal message in assert.strictEqual() call in napi test
testFinalizer.js.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove string literal message in assert.strictEqual() calls in
test-async-await.js.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove an unecessary string literal from assert.strictEqual() call in
test-inspector.js. The string literal is printed instead of the value
that causes an error. Removing the string literal allows the value that
caused the error to be printed. This improves the troubleshooting
experience when the test fails due to that assertion.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
In test-http2-timeout-large-write.js and
test-http2-timeout-large-write-file.js:

Use assert.ok() on a boolean that the test itself creates and sets,
rather than assert.strictEqual(). This allows us to use a static message
without running afoul of the upcoming "do not use string literals with
assert.strictEqual()" lint rule.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Refactor test-vm-run-in-new-context so that check for `--expose-gc` flag
will not run afoul of an upcoming lint rule that checks that string
literals are not used for the `message` argument of
`assert.strictEqual()`.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove string literal from `assert.strictEqual()` call `message`
parameter and make it a comment above the assertion instead.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove string literal from assert.strictEqual message to improve output
of AssertionError.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove unnecessary string literal from assert.deepStrictEqual() call.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove string literal as assertion message in call to
assert.strictEqual() in test-dns-resolveany-bad-ancount.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Remove string literal as assertion message in call to
assert.strictEqual() in test-dns-lookup.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
Make minor modifications to test-assert.js to prepare it for linting
rule that forbids the use of string literals for the third argument of
assert.strictEqual().

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this pull request Sep 20, 2018
String literals provided as the third argument to assert.strictEqual()
or assert.deepStrictEqual() will mask the values that are causing
issues. Use a lint rule to prevent such usage.

Backport-PR-URL: #22912
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
addaleax pushed a commit that referenced this pull request Sep 22, 2018
In short: Some unit tests are using string literals to simply tell you a
conclusion what's right/wrong BUT not tell you what actually values are.
So it's necessary to print them out in the console.

Refs: #22849
PR-URL: #22891
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this pull request Sep 23, 2018
In short: Some unit tests are using string literals to simply tell you a
conclusion what's right/wrong BUT not tell you what actually values are.
So it's necessary to print them out in the console.

Refs: #22849
PR-URL: #22891
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Sep 25, 2018
Remove an unecessary string literal from assert.strictEqual() call in
test-inspector.js. The string literal is printed instead of the value
that causes an error. Removing the string literal allows the value that
caused the error to be printed. This improves the troubleshooting
experience when the test fails due to that assertion.

Backport-PR-URL: #22888
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request Sep 25, 2018
In test-http2-timeout-large-write.js and
test-http2-timeout-large-write-file.js:

Use assert.ok() on a boolean that the test itself creates and sets,
rather than assert.strictEqual(). This allows us to use a static message
without running afoul of the upcoming "do not use string literals with
assert.strictEqual()" lint rule.

Backport-PR-URL: #22888
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request Sep 25, 2018
Refactor test-vm-run-in-new-context so that check for `--expose-gc` flag
will not run afoul of an upcoming lint rule that checks that string
literals are not used for the `message` argument of
`assert.strictEqual()`.

Backport-PR-URL: #22888
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request Sep 25, 2018
Remove string literal from `assert.strictEqual()` call `message`
parameter and make it a comment above the assertion instead.

Backport-PR-URL: #22888
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request Sep 25, 2018
Remove string literal as assertion message in call to
assert.strictEqual() in test-dns-resolveany-bad-ancount.

Backport-PR-URL: #22888
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request Sep 25, 2018
Make minor modifications to test-assert.js to prepare it for linting
rule that forbids the use of string literals for the third argument of
assert.strictEqual().

Backport-PR-URL: #22888
PR-URL: #22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
kjin pushed a commit to kjin/node that referenced this pull request Oct 3, 2018
In test-http2-timeout-large-write.js and
test-http2-timeout-large-write-file.js:

Use assert.ok() on a boolean that the test itself creates and sets,
rather than assert.strictEqual(). This allows us to use a static message
without running afoul of the upcoming "do not use string literals with
assert.strictEqual()" lint rule.

Backport-PR-URL: nodejs#22912
PR-URL: nodejs#22849
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@BethGriggs BethGriggs mentioned this pull request Oct 30, 2018
@Trott Trott deleted the strictequal-literal branch January 13, 2022 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.