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:
parent
4cea01a410
commit
fdf829eea4
@ -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(() => {});
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user