Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fs/unstable): add fs.lstat #6276

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}

Check warning on line 33 in fs/unstable_lstat.ts

View check run for this annotation

Codecov / codecov/patch

fs/unstable_lstat.ts#L26-L33

Added lines #L26 - L33 were not covered by tests
}
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);
});
Loading