File tree Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ import "../../collections/unzip_test.ts";
50
50
import "../../collections/without_all_test.ts" ;
51
51
import "../../collections/zip_test.ts" ;
52
52
import "../../fs/unstable_stat_test.ts" ;
53
+ import "../../fs/unstable_lstat_test.ts" ;
53
54
54
55
for ( const testDef of testDefinitions ) {
55
56
test ( testDef . name , testDef . fn ) ;
Original file line number Diff line number Diff line change 14
14
"./expand-glob" : " ./expand_glob.ts" ,
15
15
"./move" : " ./move.ts" ,
16
16
"./unstable-stat" : " ./unstable_stat.ts" ,
17
+ "./unstable-lstat" : " ./unstable_lstat.ts" ,
17
18
"./walk" : " ./walk.ts"
18
19
}
19
20
}
Original file line number Diff line number Diff line change
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+
3
+ import { getNodeFsPromises , isDeno } from "./_utils.ts" ;
4
+ import { mapError } from "./_map_error.ts" ;
5
+ import { toFileInfo } from "./_to_file_info.ts" ;
6
+ import type { FileInfo } from "./unstable_types.ts" ;
7
+
8
+ /** 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.
9
+ *
10
+ * ```ts
11
+ * import { assert } from "@std/assert";
12
+ * import { lstat } from "@std/fs/unstable-lstat";
13
+ * const fileInfo = await lstat("README.md");
14
+ * assert(fileInfo.isFile);
15
+ * ```
16
+ *
17
+ * Requires `allow-read` permission.
18
+ *
19
+ * @tags allow-read
20
+ * @category File System
21
+ */
22
+ export async function lstat ( path : string | URL ) : Promise < FileInfo > {
23
+ if ( isDeno ) {
24
+ return Deno . lstat ( path ) ;
25
+ } else {
26
+ const fsPromises = getNodeFsPromises ( ) ;
27
+ try {
28
+ const stat = await fsPromises . lstat ( path ) ;
29
+ return toFileInfo ( stat ) ;
30
+ } catch ( error ) {
31
+ throw mapError ( error ) ;
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
+
3
+ import { assert , assertRejects } from "@std/assert" ;
4
+ import { lstat } from "./unstable_lstat.ts" ;
5
+ import { NotFound } from "./unstable_errors.js" ;
6
+
7
+ Deno . test ( "lstat() returns FileInfo for a file" , async ( ) => {
8
+ const fileInfo = await lstat ( "README.md" ) ;
9
+
10
+ assert ( fileInfo . isFile ) ;
11
+ } ) ;
12
+
13
+ Deno . test ( "lstat() does not follow symlinks" , async ( ) => {
14
+ const linkFile = `${ import . meta. dirname } /testdata/0-link` ;
15
+ const fileInfo = await lstat ( linkFile ) ;
16
+
17
+ assert ( fileInfo . isSymlink ) ;
18
+ } ) ;
19
+
20
+ Deno . test ( "lstat() returns FileInfo for a directory" , async ( ) => {
21
+ const fileInfo = await lstat ( "fs" ) ;
22
+
23
+ assert ( fileInfo . isDirectory ) ;
24
+ } ) ;
25
+
26
+ Deno . test ( "lstat() rejects with NotFound for a non-existent file" , async ( ) => {
27
+ await assertRejects ( async ( ) => {
28
+ await lstat ( "non_existent_file" ) ;
29
+ } , NotFound ) ;
30
+ } ) ;
You can’t perform that action at this time.
0 commit comments