-
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
test: validate arguments to mustCall(), fix incorrect tests #10692
test: validate arguments to mustCall(), fix incorrect tests #10692
Conversation
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message.
@@ -423,8 +423,10 @@ function runCallChecks(exitCode) { | |||
} | |||
|
|||
|
|||
exports.mustCall = function(fn, expected) { | |||
if (typeof expected !== 'number') expected = 1; | |||
exports.mustCall = function(fn, expected = 1) { |
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 don't think this syntax is supported on v4. This will need to be changed now or during backport.
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.
Oh, I didn't realize this would be backported. Which is the preferred method - older syntax everywhere, or change during the backport?
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 think the simplest thing to do would be to update this PR. If expected === undefined
, set it to 1. Otherwise, throw a TypeError
if typeof expected !== 'number'
. I don't think we need to worry about negative numbers slipping through, because they should blow up during development.
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. I tried running the test under v4 just to see it, but there are other bits of modern elsewhere in common.js :/
@@ -424,7 +424,12 @@ function runCallChecks(exitCode) { | |||
|
|||
|
|||
exports.mustCall = function(fn, expected) { | |||
if (typeof expected !== 'number') expected = 1; | |||
if (expected === undefined) { |
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 drop the curly braces on both if
statements.
if (expected === undefined) { | ||
expected = 1; | ||
} | ||
if (typeof expected !== 'number' || expected < 0) { |
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.
Make this an else if
, and drop the expected < 0
check.
expected = 1; | ||
} | ||
if (typeof expected !== 'number' || expected < 0) { | ||
throw new RangeError(`Invalid expected value: ${expected}`); |
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.
Change this to a TypeError
.
|
||
|
||
assert.throws(function() { | ||
common.mustCall(function() {}, 'foo')(); |
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.
The trailing ()
shouldn't be necessary here.
}, /invalid expected value: foo/i); | ||
|
||
assert.throws(function() { | ||
common.mustCall(function() {}, /foo/)(); |
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.
Same here.
|
||
assert.throws(function() { | ||
common.mustCall(function() {}, 'foo')(); | ||
}, /invalid expected value: foo/i); |
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 do /^TypeError: Invalid expected value: foo$/
. Also, drop the insensitive check.
|
||
assert.throws(function() { | ||
common.mustCall(function() {}, /foo/)(); | ||
}, /invalid expected value: \/foo\//i); |
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.
Same thing here.
3dac8a1
to
c5ba0c3
Compare
@cjihrig I made several improvements that I believe address everything you pointed out, and then rebased it back to two commits because the log was getting a bit hairy. |
@@ -424,7 +424,9 @@ function runCallChecks(exitCode) { | |||
|
|||
|
|||
exports.mustCall = function(fn, expected) { | |||
if (typeof expected !== 'number') expected = 1; | |||
if (expected === undefined) expected = 1; |
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.
Move expected = 1;
to its own line please.
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. Thanks!
c5ba0c3
to
f3c1855
Compare
@@ -5,3 +5,12 @@ const assert = require('assert'); | |||
common.globalCheck = false; | |||
global.gc = 42; // Not a valid global unless --expose_gc is set. | |||
assert.deepStrictEqual(common.leakedGlobals(), ['gc']); | |||
|
|||
|
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.
Nit: can you please remove one of these 2 blank lines?
instead of silently overwriting invalid values with the default
f3c1855
to
cd7ce11
Compare
LGTM and thanks! 🎉 |
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: #10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: #10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Landed in 762a303 |
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: nodejs#10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Missed a commit in the backport... nvm |
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: #10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: #10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This fixes a misplaced parenthesis in each of the tests in test/parallel/test-http-response-statuscodes.js, causing the tests to pass as long as any error was thrown, without validating the error message. PR-URL: #10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
instead of silently overwriting invalid values with the default PR-URL: #10692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
tests
Notes: this is a subset of the changes in #9031 as requested by @mscdex and @targos.