Skip to content

Commit

Permalink
fix(ext/node): Add Dirent.path and Dirent.parentPath (denoland#24257)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Jun 19, 2024
1 parent 1d6b775 commit 209b286
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/standalone/virtual_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)),
Expand Down
1 change: 1 addition & 0 deletions ext/fs/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 785,7 @@ fn read_dir(path: &Path) -> FsResult<Vec<FsDirEntry>> {
};
}
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),
Expand Down
9 changes: 9 additions & 0 deletions ext/node/polyfills/_fs/_fs_dirent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 13 additions & 0 deletions tests/unit_node/_fs/_fs_dirent_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
},
});

0 comments on commit 209b286

Please sign in to comment.