Skip to content

Commit

Permalink
fs: refactor to use validateInteger
Browse files Browse the repository at this point in the history
PR-URL: #46008
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
deokjinkim authored and RafaelGSS committed Jan 4, 2023
1 parent 57274a8 commit 928576f
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 20 deletions.
11 changes: 2 additions & 9 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
FunctionPrototypeCall,
Number,
NumberIsFinite,
NumberIsInteger,
MathMin,
MathRound,
ObjectIs,
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 0 additions & 4 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/
}
);
});
Expand All @@ -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/
}
);
});
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-fs-copyfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-fs-promises-file-handle-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ assert.strictEqual(
access(__filename, 8),
{
code: 'ERR_OUT_OF_RANGE',
message: /"mode".*must be an integer >= 0 && <= 7\. Received 8$/
}
);

assert.rejects(
access(__filename, { [Symbol.toPrimitive]() { return 5; } }),
{
code: 'ERR_INVALID_ARG_TYPE',
message: /"mode" argument.+integer\. Received an instance of Object$/
}
);
}
Expand Down

0 comments on commit 928576f

Please sign in to comment.