From 5a3623af746b90b7c2b43d447a3ad345374bd498 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 13 Mar 2019 19:23:00 +0100 Subject: [PATCH] assert: validate required arguments This validates most `assert` functions to verify that the required arguments are indeed passed to the function. PR-URL: https://github.com/nodejs/node/pull/26641 Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott Reviewed-By: Sam Roberts Reviewed-By: James M Snell Reviewed-By: Matteo Collina --- .eslintrc.js | 16 ----------- lib/assert.js | 27 ++++++++++++++++++- test/parallel/test-assert.js | 52 +++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index f8e6e78769c..4863bbd9986 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -210,22 +210,6 @@ module.exports = { selector: 'ThrowStatement > CallExpression[callee.name=/Error$/]', message: 'Use `new` keyword when throwing an `Error`.', }, - { - selector: "CallExpression[callee.property.name='notDeepStrictEqual'][arguments.length<2]", - message: 'assert.notDeepStrictEqual() must be invoked with at least two arguments.', - }, - { - selector: "CallExpression[callee.property.name='deepStrictEqual'][arguments.length<2]", - message: 'assert.deepStrictEqual() must be invoked with at least two arguments.', - }, - { - selector: "CallExpression[callee.property.name='notStrictEqual'][arguments.length<2]", - message: 'assert.notStrictEqual() must be invoked with at least two arguments.', - }, - { - selector: "CallExpression[callee.property.name='strictEqual'][arguments.length<2]", - message: 'assert.strictEqual() must be invoked with at least two arguments.', - }, { selector: "CallExpression[callee.property.name='notDeepStrictEqual'][arguments.0.type='Literal']:not([arguments.1.type='Literal']):not([arguments.1.type='ObjectExpression']):not([arguments.1.type='ArrayExpression']):not([arguments.1.type='UnaryExpression'])", message: 'The first argument should be the `actual`, not the `expected` value.', diff --git a/lib/assert.js b/lib/assert.js index e32cae3cffb..4cdbdc7da9e 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -25,7 +25,8 @@ const { codes: { ERR_AMBIGUOUS_ARGUMENT, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, - ERR_INVALID_RETURN_VALUE + ERR_INVALID_RETURN_VALUE, + ERR_MISSING_ARGS } } = require('internal/errors'); const AssertionError = require('internal/assert/assertion_error'); const { openSync, closeSync, readSync } = require('fs'); @@ -351,6 +352,9 @@ assert.ok = ok; // The equality assertion tests shallow, coercive equality with ==. /* eslint-disable no-restricted-properties */ assert.equal = function equal(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } // eslint-disable-next-line eqeqeq if (actual != expected) { innerFail({ @@ -366,6 +370,9 @@ assert.equal = function equal(actual, expected, message) { // The non-equality assertion tests for whether two objects are not // equal with !=. assert.notEqual = function notEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } // eslint-disable-next-line eqeqeq if (actual == expected) { innerFail({ @@ -380,6 +387,9 @@ assert.notEqual = function notEqual(actual, expected, message) { // The equivalence assertion tests a deep equality relation. assert.deepEqual = function deepEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } if (isDeepEqual === undefined) lazyLoadComparison(); if (!isDeepEqual(actual, expected)) { innerFail({ @@ -394,6 +404,9 @@ assert.deepEqual = function deepEqual(actual, expected, message) { // The non-equivalence assertion tests for any deep inequality. assert.notDeepEqual = function notDeepEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } if (isDeepEqual === undefined) lazyLoadComparison(); if (isDeepEqual(actual, expected)) { innerFail({ @@ -408,6 +421,9 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) { /* eslint-enable */ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } if (isDeepEqual === undefined) lazyLoadComparison(); if (!isDeepStrictEqual(actual, expected)) { innerFail({ @@ -422,6 +438,9 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { assert.notDeepStrictEqual = notDeepStrictEqual; function notDeepStrictEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } if (isDeepEqual === undefined) lazyLoadComparison(); if (isDeepStrictEqual(actual, expected)) { innerFail({ @@ -435,6 +454,9 @@ function notDeepStrictEqual(actual, expected, message) { } assert.strictEqual = function strictEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } if (!Object.is(actual, expected)) { innerFail({ actual, @@ -447,6 +469,9 @@ assert.strictEqual = function strictEqual(actual, expected, message) { }; assert.notStrictEqual = function notStrictEqual(actual, expected, message) { + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('actual', 'expected'); + } if (Object.is(actual, expected)) { innerFail({ actual, diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 83b97cfec3e..5bca70a01db 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -560,7 +560,7 @@ assert.throws( '- undefined', ].join('\n'); assert.throws( - () => assert.deepEqual([1, 2, 1]), + () => assert.deepEqual([1, 2, 1], undefined), { message }); message = [ @@ -1147,3 +1147,53 @@ assert.throws( } assert(threw); } + +assert.throws( + () => a.equal(1), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.deepEqual(/a/), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.notEqual(null), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.notDeepEqual('test'), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.strictEqual({}), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.deepStrictEqual(Symbol()), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.notStrictEqual(5n), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.notDeepStrictEqual(undefined), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.strictEqual(), + { code: 'ERR_MISSING_ARGS' } +); + +assert.throws( + () => a.deepStrictEqual(), + { code: 'ERR_MISSING_ARGS' } +);