Skip to content

Commit adaf530

Browse files
Masashi Hiranotargos
Masashi Hirano
authored andcommitted
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: #22616 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: George Adams <[email protected]>
1 parent f1b1b73 commit adaf530

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

test/parallel/test-fs-mkdir.js

+26
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
177177
});
178178
}
179179

180+
// mkdirSync and mkdir require options.recursive to be a boolean.
181+
// Anything else generates an error.
182+
{
183+
const pathname = path.join(tmpdir.path, nextdir());
184+
['', 1, {}, [], null, Symbol('test'), () => {}].forEach((recursive) => {
185+
common.expectsError(
186+
() => fs.mkdir(pathname, { recursive }, common.mustNotCall()),
187+
{
188+
code: 'ERR_INVALID_ARG_TYPE',
189+
type: TypeError,
190+
message: 'The "recursive" argument must be of type boolean. Received ' +
191+
`type ${typeof recursive}`
192+
}
193+
);
194+
common.expectsError(
195+
() => fs.mkdirSync(pathname, { recursive }),
196+
{
197+
code: 'ERR_INVALID_ARG_TYPE',
198+
type: TypeError,
199+
message: 'The "recursive" argument must be of type boolean. Received ' +
200+
`type ${typeof recursive}`
201+
}
202+
);
203+
});
204+
}
205+
180206
// Keep the event loop alive so the async mkdir() requests
181207
// have a chance to run (since they don't ref the event loop).
182208
process.nextTick(() => {});

test/parallel/test-fs-promises.js

+34
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ function verifyStatObject(stat) {
211211
assert.deepStrictEqual(list, ['baz2.js', 'dir']);
212212
await rmdir(newdir);
213213

214+
// mkdir when options is number.
215+
{
216+
const dir = path.join(tmpDir, nextdir());
217+
await mkdir(dir, 777);
218+
stats = await stat(dir);
219+
assert(stats.isDirectory());
220+
}
221+
222+
// mkdir when options is string.
223+
{
224+
const dir = path.join(tmpDir, nextdir());
225+
await mkdir(dir, '777');
226+
stats = await stat(dir);
227+
assert(stats.isDirectory());
228+
}
229+
214230
// mkdirp when folder does not yet exist.
215231
{
216232
const dir = path.join(tmpDir, nextdir(), nextdir());
@@ -250,6 +266,24 @@ function verifyStatObject(stat) {
250266
assert(stats.isDirectory());
251267
}
252268

269+
// mkdirp require recursive option to be a boolean.
270+
// Anything else generates an error.
271+
{
272+
const dir = path.join(tmpDir, nextdir(), nextdir());
273+
['', 1, {}, [], null, Symbol('test'), () => {}].forEach((recursive) => {
274+
assert.rejects(
275+
// mkdir() expects to get a boolean value for options.recursive.
276+
async () => mkdir(dir, { recursive }),
277+
{
278+
code: 'ERR_INVALID_ARG_TYPE',
279+
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
280+
message: 'The "recursive" argument must be of type boolean. ' +
281+
`Received type ${typeof recursive}`
282+
}
283+
);
284+
});
285+
}
286+
253287
await mkdtemp(path.resolve(tmpDir, 'FOO'));
254288
assert.rejects(
255289
// mkdtemp() expects to get a string prefix.

0 commit comments

Comments
 (0)