diff options
author | Deokjin Kim <deokjin81.kim@gmail.com> | 2023-01-01 15:33:50 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-01 06:33:50 +0000 |
commit | 87f541e77d0baa5dbdcbe87652b83f8ced61c433 (patch) | |
tree | 7b9ef657f9f816a4198534450deb0aa877c6422d | |
parent | 979ec876656c4d4c69cbf6962b87feae04d466bf (diff) | |
download | node-new-87f541e77d0baa5dbdcbe87652b83f8ced61c433.tar.gz |
fs: refactor to use `validateInteger`
PR-URL: https://github.com/nodejs/node/pull/46008
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
-rw-r--r-- | lib/internal/fs/utils.js | 11 | ||||
-rw-r--r-- | test/parallel/test-fs-access.js | 4 | ||||
-rw-r--r-- | test/parallel/test-fs-copyfile.js | 2 | ||||
-rw-r--r-- | test/parallel/test-fs-error-messages.js | 3 | ||||
-rw-r--r-- | test/parallel/test-fs-promises-file-handle-sync.js | 1 | ||||
-rw-r--r-- | 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 a3ef20b708..5b57de3f7d 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, @@ -862,14 +861,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 744d23b4e9..66089049e0 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 1bfa410a12..cbe79adf02 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 e47dee023c..43185725e5 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 53eb242454..07cfe98e99 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 c0cfb468a2..98f3334219 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$/ } ); } |