Skip to content

Commit

Permalink
validate arguments to mustCall(), default value
Browse files Browse the repository at this point in the history
Updates expected to use modern default syntax and also validate the value
and throw an error rather than silently overwriting invalid arguments.

Would prevent issues such as the one fixed in the first commit on
nodejs#9031
  • Loading branch information
nfriedly committed Oct 11, 2016
1 parent 6de9d13 commit 620cc12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,10 @@ function runCallChecks(exitCode) {
}


exports.mustCall = function(fn, expected) {
if (typeof expected !== 'number') expected = 1;
exports.mustCall = function(fn, expected = 1) {
if (typeof expected !== 'number' || expected < 0) {
throw new RangeError(`Invalid expected value: ${expected}`);
}

var context = {
expected: expected,
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ var assert = require('assert');
common.globalCheck = false;
global.gc = 42; // Not a valid global unless --expose_gc is set.
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);


assert.throws(function() {
common.mustCall(function() {}, 'foo')();
}, /invalid expected value: foo/i);

assert.throws(function() {
common.mustCall(function() {}, /foo/)();
}, /invalid expected value: \/foo\//i);

0 comments on commit 620cc12

Please sign in to comment.