From 209b2868131f43e1707fc706805a270a2b73d45a Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 19 Jun 2024 12:51:01 +0530 Subject: [PATCH] fix(ext/node): Add Dirent.path and Dirent.parentPath (#24257) --- cli/standalone/virtual_fs.rs | 1 + ext/fs/interface.rs | 1 + ext/fs/std_fs.rs | 1 + ext/node/polyfills/_fs/_fs_dirent.ts | 9 +++++++++ tests/unit_node/_fs/_fs_dirent_test.ts | 13 +++++++++++++ 5 files changed, 25 insertions(+) diff --git a/cli/standalone/virtual_fs.rs b/cli/standalone/virtual_fs.rs index 4c14f0e9a2..3e6823d506 100644 --- a/cli/standalone/virtual_fs.rs +++ b/cli/standalone/virtual_fs.rs @@ -764,6 +764,7 @@ impl FileBackedVfs { .entries .iter() .map(|entry| FsDirEntry { + parent_path: path.to_string_lossy().into_owned(), name: entry.name().to_string(), is_file: matches!(entry, VfsEntry::File(_)), is_directory: matches!(entry, VfsEntry::Dir(_)), diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs index 5031dc1349..833f362bf9 100644 --- a/ext/fs/interface.rs +++ b/ext/fs/interface.rs @@ -72,6 +72,7 @@ pub enum FsFileType { #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct FsDirEntry { + pub parent_path: String, pub name: String, pub is_file: bool, pub is_directory: bool, diff --git a/ext/fs/std_fs.rs b/ext/fs/std_fs.rs index 9e2acedcca..054f5c9c4b 100644 --- a/ext/fs/std_fs.rs +++ b/ext/fs/std_fs.rs @@ -785,6 +785,7 @@ fn read_dir(path: &Path) -> FsResult> { }; } Some(FsDirEntry { + parent_path: path.to_string_lossy().to_string(), name, is_file: method_or_false!(is_file), is_directory: method_or_false!(is_dir), diff --git a/ext/node/polyfills/_fs/_fs_dirent.ts b/ext/node/polyfills/_fs/_fs_dirent.ts index 155d5cb033..0f80fc135c 100644 --- a/ext/node/polyfills/_fs/_fs_dirent.ts +++ b/ext/node/polyfills/_fs/_fs_dirent.ts @@ -43,4 +43,13 @@ export default class Dirent { get name(): string | null { return this.entry.name; } + + get parentPath(): string { + return this.entry.parentPath; + } + + /** @deprecated */ + get path(): string { + return this.parentPath; + } } diff --git a/tests/unit_node/_fs/_fs_dirent_test.ts b/tests/unit_node/_fs/_fs_dirent_test.ts index 7c88a6d3f2..8538889256 100644 --- a/tests/unit_node/_fs/_fs_dirent_test.ts +++ b/tests/unit_node/_fs/_fs_dirent_test.ts @@ -6,6 +6,7 @@ import { Dirent as Dirent_ } from "node:fs"; const Dirent = Dirent_ as any; class DirEntryMock implements Deno.DirEntry { + parentPath = ""; name = ""; isFile = false; isDirectory = false; @@ -80,3 +81,15 @@ Deno.test({ ); }, }); + +Deno.test({ + name: "Path and parent path is correct", + fn() { + const entry: DirEntryMock = new DirEntryMock(); + entry.name = "my_file"; + entry.parentPath = "/home/user"; + assertEquals(new Dirent(entry).name, "my_file"); + assertEquals(new Dirent(entry).path, "/home/user"); + assertEquals(new Dirent(entry).parentPath, "/home/user"); + }, +});