From a5f25ecf07c3eb2b8b01eef04e94d49469fdf8e0 Mon Sep 17 00:00:00 2001 From: Artem Maksimov Date: Wed, 6 Nov 2019 17:24:29 +0300 Subject: [PATCH] test: cover 'close' method in Dir class cover 'close' method (in Dir class) with tests Add 2 tests for full covering of method 'close' in class Dir 1. If pass smth that not string as a callback - throw an exception 2. If do .close() on already closed directory - throw an exception PR-URL: https://github.com/nodejs/node/pull/30310 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Jeremiah Senkpiel Reviewed-By: Luigi Pinca Reviewed-By: Gireesh Punathil Reviewed-By: Ruben Bridgewater Reviewed-By: Trivikram Kamat --- test/parallel/test-fs-opendir.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/parallel/test-fs-opendir.js b/test/parallel/test-fs-opendir.js index 05fded527fe7f1..7ae6186b28518a 100644 --- a/test/parallel/test-fs-opendir.js +++ b/test/parallel/test-fs-opendir.js @@ -33,6 +33,11 @@ const dirclosedError = { code: 'ERR_DIR_CLOSED' }; +const invalidCallbackObj = { + code: 'ERR_INVALID_CALLBACK', + name: 'TypeError' +}; + // Check the opendir Sync version { const dir = fs.opendirSync(testDir); @@ -205,3 +210,19 @@ for (const bufferSize of ['', '1', null]) { assertDirent(dir.readSync()); dir.close(); } + +// Check that when passing a string instead of function - throw an exception +async function doAsyncIterInvalidCallbackTest() { + const dir = await fs.promises.opendir(testDir); + assert.throws(() => dir.close('not function'), invalidCallbackObj); +} +doAsyncIterInvalidCallbackTest().then(common.mustCall()); + +// Check if directory already closed - throw an exception +async function doAsyncIterDirClosedTest() { + const dir = await fs.promises.opendir(testDir); + await dir.close(); + + assert.throws(() => dir.close(), dirclosedError); +} +doAsyncIterDirClosedTest().then(common.mustCall());