From aec340b3128557631f382966c7b966280d7121fe Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sun, 1 Jan 2023 15:33:50 +0900 Subject: [PATCH] fs: refactor to use `validateInteger` PR-URL: https://github.com/nodejs/node/pull/46008 Reviewed-By: Antoine du Hamel Reviewed-By: Yagiz Nizipli --- lib/internal/fs/utils.js | 11 ++--------- test/parallel/test-fs-access.js | 4 ---- test/parallel/test-fs-copyfile.js | 2 -- test/parallel/test-fs-error-messages.js | 3 +-- test/parallel/test-fs-promises-file-handle-sync.js | 1 - test/parallel/test-fs-promises.js | 2 -- 6 files changed, 3 insertions(+), 20 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 25fd1cc28a8ce9..3927b5a6953720 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -10,7 +10,6 @@ const { FunctionPrototypeCall, Number, NumberIsFinite, - NumberIsInteger, MathMin, MathRound, ObjectIs, @@ -863,14 +862,8 @@ const getValidMode = hideStackFrames((mode, type) => { if (mode == null) { return def; } - if (NumberIsInteger(mode) && mode >= min && mode <= max) { - return mode; - } - if (typeof mode !== 'number') { - throw new ERR_INVALID_ARG_TYPE('mode', 'integer', mode); - } - throw new ERR_OUT_OF_RANGE( - 'mode', `an integer >= ${min} && <= ${max}`, mode); + validateInteger(mode, 'mode', min, max); + return mode; }); const validateStringAfterArrayBufferView = hideStackFrames((buffer, name) => { diff --git a/test/parallel/test-fs-access.js b/test/parallel/test-fs-access.js index 744d23b4e95080..66089049e0cf07 100644 --- a/test/parallel/test-fs-access.js +++ b/test/parallel/test-fs-access.js @@ -170,14 +170,12 @@ fs.accessSync(readWriteFile, mode); () => fs.access(readWriteFile, mode, common.mustNotCall()), { code: 'ERR_INVALID_ARG_TYPE', - message: /"mode" argument.+integer/ } ); assert.throws( () => fs.accessSync(readWriteFile, mode), { code: 'ERR_INVALID_ARG_TYPE', - message: /"mode" argument.+integer/ } ); }); @@ -194,14 +192,12 @@ fs.accessSync(readWriteFile, mode); () => fs.access(readWriteFile, mode, common.mustNotCall()), { code: 'ERR_OUT_OF_RANGE', - message: /"mode".+It must be an integer >= 0 && <= 7/ } ); assert.throws( () => fs.accessSync(readWriteFile, mode), { code: 'ERR_OUT_OF_RANGE', - message: /"mode".+It must be an integer >= 0 && <= 7/ } ); }); diff --git a/test/parallel/test-fs-copyfile.js b/test/parallel/test-fs-copyfile.js index 1bfa410a124d2d..cbe79adf02c478 100644 --- a/test/parallel/test-fs-copyfile.js +++ b/test/parallel/test-fs-copyfile.js @@ -156,8 +156,6 @@ assert.throws(() => { }, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', - message: 'The value of "mode" is out of range. It must be an integer ' + - '>= 0 && <= 7. Received 8' }); assert.throws(() => { diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index e47dee023c9623..43185725e5fbcd 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -662,8 +662,7 @@ if (!common.isAIX) { // Check copyFile with invalid modes. { const validateError = { - message: /"mode".+must be an integer >= 0 && <= 7\. Received -1/, - code: 'ERR_OUT_OF_RANGE' + code: 'ERR_OUT_OF_RANGE', }; assert.throws( diff --git a/test/parallel/test-fs-promises-file-handle-sync.js b/test/parallel/test-fs-promises-file-handle-sync.js index 53eb2424549f93..07cfe98e99a5fd 100644 --- a/test/parallel/test-fs-promises-file-handle-sync.js +++ b/test/parallel/test-fs-promises-file-handle-sync.js @@ -14,7 +14,6 @@ async function validate() { copyFile(fixtures.path('baz.js'), dest, 'r'), { code: 'ERR_INVALID_ARG_TYPE', - message: /mode.*integer.*string/ } ); await copyFile(fixtures.path('baz.js'), dest); diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index 03f71069897345..3102543c5a0838 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -64,7 +64,6 @@ assert.strictEqual( access(__filename, 8), { code: 'ERR_OUT_OF_RANGE', - message: /"mode".*must be an integer >= 0 && <= 7\. Received 8$/ } ); @@ -72,7 +71,6 @@ assert.strictEqual( access(__filename, { [Symbol.toPrimitive]() { return 5; } }), { code: 'ERR_INVALID_ARG_TYPE', - message: /"mode" argument.+integer\. Received an instance of Object$/ } ); }