Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: refactor to use validateInteger #46008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/internal/fs/utils.js
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
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
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
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
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
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