From 6cb82ab01823bbdf1b3669265890fa790c9cfbe9 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Fri, 31 Aug 2018 08:15:59 +0900 Subject: [PATCH 1/4] test: check parameter type of fs.mkdir() Added tests to check parameter type of fs.mkdir(), fs.mkdirSync() and fsPromises.mkdir() to increase coverage. --- test/parallel/test-fs-mkdir.js | 26 +++++++++++++++++++++++ test/parallel/test-fs-promises.js | 34 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/test/parallel/test-fs-mkdir.js b/test/parallel/test-fs-mkdir.js index f5fecbe1572aaf..abc94aa4ec75d0 100644 --- a/test/parallel/test-fs-mkdir.js +++ b/test/parallel/test-fs-mkdir.js @@ -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()), + { + 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(() => {}); diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index bcfaaf3890d377..e9adb438dbcb74 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -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((i) => { + assert.rejects( + // mkdtemp() expects to get a string prefix. + async () => mkdir(dir, { recursive: i }), + { + 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. From df5ee0fca40593e782a9e41a31542468fbe709a9 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Fri, 31 Aug 2018 23:33:17 +0900 Subject: [PATCH 2/4] test: recursive argument name to check type in fs.mkdir --- test/parallel/test-fs-mkdir.js | 10 +++++----- test/parallel/test-fs-promises.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-fs-mkdir.js b/test/parallel/test-fs-mkdir.js index abc94aa4ec75d0..d07aab53769299 100644 --- a/test/parallel/test-fs-mkdir.js +++ b/test/parallel/test-fs-mkdir.js @@ -181,23 +181,23 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) { // Anything else generates an error. { const pathname = path.join(tmpdir.path, nextdir()); - ['', 1, {}, [], null, Symbol('test'), () => {}].forEach((i) => { + ['', 1, {}, [], null, Symbol('test'), () => {}].forEach((recursive) => { common.expectsError( - () => fs.mkdir(pathname, { recursive: i }, common.mustNotCall()), + () => 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 i}` + `type ${typeof recursive}` } ); common.expectsError( - () => fs.mkdirSync(pathname, { recursive: i }), + () => fs.mkdirSync(pathname, { recursive }), { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "recursive" argument must be of type boolean. Received ' + - `type ${typeof i}` + `type ${typeof recursive}` } ); }); diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index e9adb438dbcb74..6076899bbeb0d9 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -270,7 +270,7 @@ function verifyStatObject(stat) { // Anything else generates an error. { const dir = path.join(tmpDir, nextdir(), nextdir()); - ['', 1, {}, [], null, Symbol('test'), () => {}].forEach((i) => { + ['', 1, {}, [], null, Symbol('test'), () => {}].forEach((recursive) => { assert.rejects( // mkdtemp() expects to get a string prefix. async () => mkdir(dir, { recursive: i }), @@ -278,7 +278,7 @@ function verifyStatObject(stat) { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]', message: 'The "recursive" argument must be of type boolean. ' + - `Received type ${typeof i}` + `Received type ${typeof recursive}` } ); }); From 99708c6acffcad32a5e064e15387ae64a4503737 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Fri, 31 Aug 2018 23:34:42 +0900 Subject: [PATCH 3/4] test: comment to check type for fsPromises.mkdir() --- test/parallel/test-fs-promises.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index 6076899bbeb0d9..8180c11699de23 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -272,7 +272,7 @@ function verifyStatObject(stat) { const dir = path.join(tmpDir, nextdir(), nextdir()); ['', 1, {}, [], null, Symbol('test'), () => {}].forEach((recursive) => { assert.rejects( - // mkdtemp() expects to get a string prefix. + // mkdir() expects to get a boolean value for options.recursive. async () => mkdir(dir, { recursive: i }), { code: 'ERR_INVALID_ARG_TYPE', From 2299b070fc2a0fbb750f421d87507999642a34cf Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Fri, 31 Aug 2018 23:43:55 +0900 Subject: [PATCH 4/4] test: fix fs.mkdir recursive option in test-fs-promises --- test/parallel/test-fs-promises.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index 8180c11699de23..714f1db05d5494 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -273,7 +273,7 @@ function verifyStatObject(stat) { ['', 1, {}, [], null, Symbol('test'), () => {}].forEach((recursive) => { assert.rejects( // mkdir() expects to get a boolean value for options.recursive. - async () => mkdir(dir, { recursive: i }), + async () => mkdir(dir, { recursive }), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]',