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

test: check parameter type of fs.mkdir() #22616

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
});
}

// mkdirSync and mkdir require options.recursive to be a boolean.
// Anything else generates an error.
{
const pathname = path.join(tmpdir.path, nextdir());
['', 1, {}, [], null, Symbol('test'), () => {}].forEach((i) => {
common.expectsError(
() => fs.mkdir(pathname, { recursive: i }, common.mustNotCall()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename i to recursive, and this can just be { recursive }.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review. Fixed this.

{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "recursive" argument must be of type boolean. Received ' +
`type ${typeof i}`
}
);
common.expectsError(
() => fs.mkdirSync(pathname, { recursive: i }),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "recursive" argument must be of type boolean. Received ' +
`type ${typeof i}`
}
);
});
}

// Keep the event loop alive so the async mkdir() requests
// have a chance to run (since they don't ref the event loop).
process.nextTick(() => {});
34 changes: 34 additions & 0 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ function verifyStatObject(stat) {
assert.deepStrictEqual(list, ['baz2.js', 'dir']);
await rmdir(newdir);

// mkdir when options is number.
{
const dir = path.join(tmpDir, nextdir());
await mkdir(dir, 777);
stats = await stat(dir);
assert(stats.isDirectory());
}

// mkdir when options is string.
{
const dir = path.join(tmpDir, nextdir());
await mkdir(dir, '777');
stats = await stat(dir);
assert(stats.isDirectory());
}

// mkdirp when folder does not yet exist.
{
const dir = path.join(tmpDir, nextdir(), nextdir());
Expand Down Expand Up @@ -250,6 +266,24 @@ function verifyStatObject(stat) {
assert(stats.isDirectory());
}

// mkdirp require recursive option to be a boolean.
// Anything else generates an error.
{
const dir = path.join(tmpDir, nextdir(), nextdir());
['', 1, {}, [], null, Symbol('test'), () => {}].forEach((i) => {
assert.rejects(
// mkdtemp() expects to get a string prefix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mkdtemp()?

Copy link
Contributor Author

@shisama shisama Aug 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sorry. this is wrong copy and paste. Fixed this.

async () => mkdir(dir, { recursive: i }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small oversight: it should be recursive without : i

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review. I fixed this.

{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "recursive" argument must be of type boolean. ' +
`Received type ${typeof i}`
}
);
});
}

await mkdtemp(path.resolve(tmpDir, 'FOO'));
assert.rejects(
// mkdtemp() expects to get a string prefix.
Expand Down