From 31119e192cb70e62f37d4da3258be7ebe1b8a253 Mon Sep 17 00:00:00 2001 From: sendoru Date: Wed, 25 Sep 2024 09:36:42 +0900 Subject: [PATCH 1/4] test: add test cases for getDirents and getDirent function Add test case when `type` is not `UV_DIRENT_UNKNOWN` for test case of `getDirent` Add test case when `path` is a nonexistent path for test case of `getDirents` and `getDirent` --- test/parallel/test-fs-utils-get-dirents.js | 75 +++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index dfc851090b26..3a5d8ecd7bd2 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -5,7 +5,7 @@ const common = require('../common'); const { getDirents, getDirent } = require('internal/fs/utils'); const assert = require('assert'); const { internalBinding } = require('internal/test/binding'); -const { UV_DIRENT_UNKNOWN } = internalBinding('constants').fs; +const { UV_DIRENT_UNKNOWN, UV_DIRENT_FILE } = internalBinding('constants').fs; const fs = require('fs'); const tmpdir = require('../common/tmpdir'); @@ -65,6 +65,36 @@ const filename = 'foo'; }, )); } +{ + // Intentional error in lstat + let path = ''; + if (common.isWindows) { + path = 'c:\\dev\\null\\does\\not\\exist'; + } else { + path = '/dev/null/does/not/exist'; + } + let full_path = path; + if (common.isWindows) { + full_path = path + '\\' + filename; + } else { + full_path = path + '/' + filename; + } + + const errmsg_enotdir_start = + 'ENOTDIR: not a directory, '; + const errmsg_enoent_start = + 'ENOENT: no such file or directory, '; + + getDirents( + Buffer.from(path), + [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], + common.mustCall((err) => { + assert.match( + err.message, new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) + ); + }, + )); +} // getDirent { // string + string @@ -125,3 +155,46 @@ const filename = 'foo'; }, )); } +{ + // When type != UV_DIRENT_UNKNOWN + getDirent( + tmpdir.path, + filename, + UV_DIRENT_FILE, + common.mustCall((err, dirent) => { + assert.strictEqual(err, null); + assert.strictEqual(dirent.name, filename); + }, + )); +} +{ + // Intentional error in lstat + let path = ''; + if (common.isWindows) { + path = 'c:\\dev\\null\\does\\not\\exist'; + } else { + path = '/dev/null/does/not/exist'; + } + let full_path = path; + if (common.isWindows) { + full_path = path + '\\' + filename; + } else { + full_path = path + '/' + filename; + } + + const errmsg_enotdir_start = + 'ENOTDIR: not a directory, '; + const errmsg_enoent_start = + 'ENOENT: no such file or directory, '; + + getDirent( + path, + filename, + UV_DIRENT_UNKNOWN, + common.mustCall((err) => { + assert.match( + err.message, new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) + ); + }, + )); +} From 6baae356abdab22056d76bbfd577a90343afec8c Mon Sep 17 00:00:00 2001 From: sendoru Date: Wed, 25 Sep 2024 09:42:25 +0900 Subject: [PATCH 2/4] test: linted code --- test/parallel/test-fs-utils-get-dirents.js | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index 3a5d8ecd7bd2..e7c20b58538a 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -73,12 +73,6 @@ const filename = 'foo'; } else { path = '/dev/null/does/not/exist'; } - let full_path = path; - if (common.isWindows) { - full_path = path + '\\' + filename; - } else { - full_path = path + '/' + filename; - } const errmsg_enotdir_start = 'ENOTDIR: not a directory, '; @@ -90,7 +84,8 @@ const filename = 'foo'; [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], common.mustCall((err) => { assert.match( - err.message, new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) + err.message, + new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) ); }, )); @@ -175,12 +170,6 @@ const filename = 'foo'; } else { path = '/dev/null/does/not/exist'; } - let full_path = path; - if (common.isWindows) { - full_path = path + '\\' + filename; - } else { - full_path = path + '/' + filename; - } const errmsg_enotdir_start = 'ENOTDIR: not a directory, '; @@ -193,7 +182,8 @@ const filename = 'foo'; UV_DIRENT_UNKNOWN, common.mustCall((err) => { assert.match( - err.message, new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) + err.message, + new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) ); }, )); From cf7849fa4066771ae7006bb0bbac88fcba73c5ff Mon Sep 17 00:00:00 2001 From: sendoru Date: Wed, 25 Sep 2024 10:44:50 +0900 Subject: [PATCH 3/4] test: apply suggestions of RedYetiDev --- test/parallel/test-fs-utils-get-dirents.js | 38 +++++----------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index e7c20b58538a..3ed350c84ff9 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -7,9 +7,11 @@ const assert = require('assert'); const { internalBinding } = require('internal/test/binding'); const { UV_DIRENT_UNKNOWN, UV_DIRENT_FILE } = internalBinding('constants').fs; const fs = require('fs'); +const { resolve } = require('path'); const tmpdir = require('../common/tmpdir'); const filename = 'foo'; +const nonExistentPath = resolve(tmpdir.path, 'does-not-exist'); { // setup @@ -67,25 +69,13 @@ const filename = 'foo'; } { // Intentional error in lstat - let path = ''; - if (common.isWindows) { - path = 'c:\\dev\\null\\does\\not\\exist'; - } else { - path = '/dev/null/does/not/exist'; - } - - const errmsg_enotdir_start = - 'ENOTDIR: not a directory, '; - const errmsg_enoent_start = - 'ENOENT: no such file or directory, '; - getDirents( - Buffer.from(path), + Buffer.from(nonExistentPath), [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], common.mustCall((err) => { assert.match( - err.message, - new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) + err.code, + new RegExp(`^${'ENOTDIR'}|${'ENOENT'}$`) ); }, )); @@ -164,26 +154,14 @@ const filename = 'foo'; } { // Intentional error in lstat - let path = ''; - if (common.isWindows) { - path = 'c:\\dev\\null\\does\\not\\exist'; - } else { - path = '/dev/null/does/not/exist'; - } - - const errmsg_enotdir_start = - 'ENOTDIR: not a directory, '; - const errmsg_enoent_start = - 'ENOENT: no such file or directory, '; - getDirent( - path, + nonExistentPath, filename, UV_DIRENT_UNKNOWN, common.mustCall((err) => { assert.match( - err.message, - new RegExp(`^${errmsg_enotdir_start}|${errmsg_enoent_start}$`) + err.code, + new RegExp(`^${'ENOTDIR'}|${'ENOENT'}$`) ); }, )); From 885bc552d67636fe7e65e67b497ae7bc080fa633 Mon Sep 17 00:00:00 2001 From: sendoru Date: Wed, 25 Sep 2024 11:31:01 +0900 Subject: [PATCH 4/4] test: simplify RegEx for error code matching --- test/parallel/test-fs-utils-get-dirents.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index 3ed350c84ff9..f8f7ee682808 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -74,8 +74,7 @@ const nonExistentPath = resolve(tmpdir.path, 'does-not-exist'); [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], common.mustCall((err) => { assert.match( - err.code, - new RegExp(`^${'ENOTDIR'}|${'ENOENT'}$`) + err.code, /ENOTDIR|ENOENT/ ); }, )); @@ -160,8 +159,7 @@ const nonExistentPath = resolve(tmpdir.path, 'does-not-exist'); UV_DIRENT_UNKNOWN, common.mustCall((err) => { assert.match( - err.code, - new RegExp(`^${'ENOTDIR'}|${'ENOENT'}$`) + err.code, /ENOTDIR|ENOENT/ ); }, ));