From 9613c194373adfa9070d9306e82e334084aa9ab0 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 26 Oct 2024 20:22:44 +0800 Subject: [PATCH] fs: fix `getDirent().parentPath` when type is `UV_DIRENT_UNKNOWN` --- lib/internal/fs/utils.js | 2 +- test/parallel/test-fs-utils-get-dirents.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 739dd0bd6ffba7..53644740144570 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -301,7 +301,7 @@ function getDirent(path, name, type, callback) { callback(err); return; } - callback(null, new DirentFromStats(name, stats, filepath)); + callback(null, new DirentFromStats(name, stats, path)); }); } else { callback(null, new Dirent(name, type, path)); diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index 50393927066dcc..55082ee698f31d 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -75,6 +75,7 @@ const filename = 'foo'; common.mustCall((err, dirent) => { assert.strictEqual(err, null); assert.strictEqual(dirent.name, filename); + assert.strictEqual(dirent.parentPath, tmpdir.path); }, )); } @@ -100,20 +101,22 @@ const filename = 'foo'; common.mustCall((err, dirent) => { assert.strictEqual(err, null); assert.strictEqual(dirent.name, filenameBuffer); - assert.deepStrictEqual(dirent.parentPath, Buffer.from(tmpdir.resolve(`${filename}/`))); + assert.strictEqual(dirent.parentPath, tmpdir.path); }, )); } { // Buffer + Buffer const filenameBuffer = Buffer.from(filename); + const dirnameBuffer = Buffer.from(tmpdir.path); getDirent( - Buffer.from(tmpdir.path), + dirnameBuffer, filenameBuffer, UV_DIRENT_UNKNOWN, common.mustCall((err, dirent) => { assert.strictEqual(err, null); assert.strictEqual(dirent.name, filenameBuffer); + assert.deepStrictEqual(dirent.parentPath, dirnameBuffer); }, )); }