deno.land / std@0.201.0 / fs / walk_test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { walk, WalkEntry, WalkError, WalkOptions, walkSync } from "./walk.ts";import { assertArrayIncludes, assertEquals, assertRejects, assertThrows,} from "../assert/mod.ts";import { fromFileUrl, resolve } from "../path/mod.ts";
const testdataDir = resolve(fromFileUrl(import.meta.url), "../testdata/walk");
async function toArray(iterator: AsyncIterableIterator<WalkEntry>) { const entries = []; for await (const entry of iterator) { entries.push(entry); } return entries;}
async function assertWalkPaths( rootPath: string, expectedPaths: string[], options?: WalkOptions,) { const root = resolve(testdataDir, rootPath); const entries = await toArray(walk(root, options)); const entriesSync = Array.from(walkSync(root, options));
const expected = expectedPaths.map((path) => resolve(root, path)); assertEquals(entries, entriesSync); assertEquals(entries.length, expected.length); assertArrayIncludes(entries.map(({ path }) => path), expected);}
Deno.test("[fs/walk] empty dir", async () => { const emptyDir = resolve(testdataDir, "empty_dir"); await Deno.mkdir(emptyDir); await assertWalkPaths("empty_dir", ["."]); await Deno.remove(emptyDir);});
Deno.test("[fs/walk] single file", async () => await assertWalkPaths("single_file", [".", "x"]));
Deno.test("[fs/walk] nested single file", async () => await assertWalkPaths("nested_single_file", [".", "a", "a/x"]));
Deno.test("[fs/walk] max depth", async () => await assertWalkPaths("depth", [".", "a", "a/b", "a/b/c"], { maxDepth: 3 }));
Deno.test("[fs/walk] exclude dirs", async () => await assertWalkPaths("depth", ["a/b/c/d/x"], { includeDirs: false }));
Deno.test("[fs/walk] exclude files", async () => await assertWalkPaths("depth", [".", "a", "a/b", "a/b/c", "a/b/c/d"], { includeFiles: false, }));
Deno.test("[fs/walk] ext", async () => await assertWalkPaths("ext", ["y.rs", "x.ts"], { exts: [".rs", ".ts"], }));
Deno.test("[fs/walk] ext", async () => await assertWalkPaths("match", ["x", "y"], { match: [/x/, /y/], }));
Deno.test("[fs/walk] skip", async () => await assertWalkPaths("match", [".", "z"], { skip: [/x/, /y/], }));
// https://github.com/denoland/deno_std/issues/1358Deno.test("[fs/walk] symlink", async () => await assertWalkPaths("symlink", [".", "x", "x"], { followSymlinks: true, }));
Deno.test("[fs/walk] symlink without followSymlink", async () => { await assertWalkPaths("symlink", [".", "x", "y"], { followSymlinks: false, });});
Deno.test("[fs/walk] non-existent root", async () => { const root = resolve(testdataDir, "non_existent"); await assertRejects( async () => await toArray(walk(root)), Deno.errors.NotFound, ); assertThrows(() => Array.from(walkSync(root)), Deno.errors.NotFound);});
// https://github.com/denoland/deno_std/issues/1789Deno.test({ name: "[fs/walk] unix socket", ignore: Deno.build.os === "windows", async fn() { const path = resolve(testdataDir, "socket", "a.sock"); try { const listener = Deno.listen({ path, transport: "unix" }); await assertWalkPaths("socket", [".", "a.sock", ".gitignore"], { followSymlinks: true, }); listener.close(); } finally { await Deno.remove(path); } },});
Deno.test({ name: "[fs/walk] fifo", ignore: Deno.build.os === "windows", async fn() { const command = new Deno.Command("mkfifo", { args: [resolve(testdataDir, "fifo", "fifo")], }); await command.output(); await assertWalkPaths("fifo", [".", "fifo", ".gitignore"], { followSymlinks: true, }); },});
Deno.test("[fs/walk] error", async () => { const root = resolve(testdataDir, "error"); await Deno.mkdir(root); await assertRejects(async () => { for await (const _entry of walk(root)) { await Deno.remove(root, { recursive: true }); } }, WalkError);});
std

Version Info

Tagged at
a year ago