Skip to content

Commit

Permalink
feat(fs/unstable): add fs.lstat (#6276)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorM867 authored Dec 19, 2024
1 parent 093fceb commit 450bdfb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions _tools/node_test_runner/run_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import "../../collections/unzip_test.ts";
import "../../collections/without_all_test.ts";
import "../../collections/zip_test.ts";
import "../../fs/unstable_stat_test.ts";
import "../../fs/unstable_lstat_test.ts";

for (const testDef of testDefinitions) {
test(testDef.name, testDef.fn);
Expand Down
1 change: 1 addition & 0 deletions fs/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"./expand-glob": "./expand_glob.ts",
"./move": "./move.ts",
"./unstable-stat": "./unstable_stat.ts",
"./unstable-lstat": "./unstable_lstat.ts",
"./walk": "./walk.ts"
}
}
34 changes: 34 additions & 0 deletions fs/unstable_lstat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { getNodeFsPromises, isDeno } from "./_utils.ts";
import { mapError } from "./_map_error.ts";
import { toFileInfo } from "./_to_file_info.ts";
import type { FileInfo } from "./unstable_types.ts";

/** Resolves to a {@linkcode FileInfo} for the specified `path`. If `path` is a symlink, information for the symlink will be returned instead of what it points to.
*
* ```ts
* import { assert } from "@std/assert";
* import { lstat } from "@std/fs/unstable-lstat";
* const fileInfo = await lstat("README.md");
* assert(fileInfo.isFile);
* ```
*
* Requires `allow-read` permission.
*
* @tags allow-read
* @category File System
*/
export async function lstat(path: string | URL): Promise<FileInfo> {
if (isDeno) {
return Deno.lstat(path);
} else {
const fsPromises = getNodeFsPromises();
try {
const stat = await fsPromises.lstat(path);
return toFileInfo(stat);
} catch (error) {
throw mapError(error);
}
}
}
30 changes: 30 additions & 0 deletions fs/unstable_lstat_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assert, assertRejects } from "@std/assert";
import { lstat } from "./unstable_lstat.ts";
import { NotFound } from "./unstable_errors.js";

Deno.test("lstat() returns FileInfo for a file", async () => {
const fileInfo = await lstat("README.md");

assert(fileInfo.isFile);
});

Deno.test("lstat() does not follow symlinks", async () => {
const linkFile = `${import.meta.dirname}/testdata/0-link`;
const fileInfo = await lstat(linkFile);

assert(fileInfo.isSymlink);
});

Deno.test("lstat() returns FileInfo for a directory", async () => {
const fileInfo = await lstat("fs");

assert(fileInfo.isDirectory);
});

Deno.test("lstat() rejects with NotFound for a non-existent file", async () => {
await assertRejects(async () => {
await lstat("non_existent_file");
}, NotFound);
});

0 comments on commit 450bdfb

Please sign in to comment.