test: check parameter type of fs.mkdir()

Added tests to check parameter type of fs.mkdir(), fs.mkdirSync()
and fsPromises.mkdir() to increase coverage.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Masashi Hirano 2018-08-31 08:15:59 +09:00 committed by George Adams
parent 4cea01a410
commit fdf829eea4
No known key found for this signature in database
GPG Key ID: 7B8D7E4421A0916D
2 changed files with 60 additions and 0 deletions

View File

@ -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(() => {});

View File

@ -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());
@ -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.