Skip to content

Commit

Permalink
test: check parameter type of fs.mkdir()
Browse files Browse the repository at this point in the history
Added tests to check parameter type of fs.mkdir(), fs.mkdirSync()
and fsPromises.mkdir() to increase coverage.

PR-URL: #22616
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
  • Loading branch information
Masashi Hirano authored and George Adams committed Sep 4, 2018
1 parent 4cea01a commit fdf829e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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((recursive) => {
common.expectsError(
() => fs.mkdir(pathname, { recursive }, common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "recursive" argument must be of type boolean. Received ' +
`type ${typeof recursive}`
}
);
common.expectsError(
() => fs.mkdirSync(pathname, { recursive }),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "recursive" argument must be of type boolean. Received ' +
`type ${typeof recursive}`
}
);
});
}

// 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((recursive) => {
assert.rejects(
// mkdir() expects to get a boolean value for options.recursive.
async () => mkdir(dir, { recursive }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "recursive" argument must be of type boolean. ' +
`Received type ${typeof recursive}`
}
);
});
}

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

0 comments on commit fdf829e

Please sign in to comment.