deno.land / std@0.224.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { walk, WalkError, type 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 assertWalkPaths( rootPath: string, expectedPaths: string[], options?: WalkOptions,) { const root = resolve(testdataDir, rootPath); const entries = await Array.fromAsync(walk(root, options));
const expected = expectedPaths.map((path) => resolve(root, path)); assertEquals(entries.length, expected.length); assertArrayIncludes(entries.map(({ path }) => path), expected);}
function assertWalkSyncPaths( rootPath: string, expectedPaths: string[], options?: WalkOptions,) { const root = resolve(testdataDir, rootPath); const entriesSync = Array.from(walkSync(root, options));
const expected = expectedPaths.map((path) => resolve(root, path)); assertEquals(entriesSync.length, expected.length); assertArrayIncludes(entriesSync.map(({ path }) => path), expected);}
Deno.test("walk() returns current dir for empty dir", async () => { const emptyDir = resolve(testdataDir, "empty_dir"); await Deno.mkdir(emptyDir); await assertWalkPaths("empty_dir", ["."]); await Deno.remove(emptyDir);});
Deno.test("walkSync() returns current dir for empty dir", async () => { const emptyDir = resolve(testdataDir, "empty_dir"); await Deno.mkdir(emptyDir); assertWalkSyncPaths("empty_dir", ["."]); await Deno.remove(emptyDir);});
Deno.test("walk() returns current dir and single file", async () => await assertWalkPaths("single_file", [".", "x"]));
Deno.test("walkSync() returns current dir and single file", () => assertWalkSyncPaths("single_file", [".", "x"]));
Deno.test("walk() returns current dir, subdir, and nested file", async () => await assertWalkPaths("nested_single_file", [".", "a", "a/x"]));
Deno.test("walkSync() returns current dir, subdir, and nested file", () => assertWalkSyncPaths("nested_single_file", [".", "a", "a/x"]));
Deno.test("walk() accepts maxDepth option", async () => await assertWalkPaths("depth", [".", "a", "a/b", "a/b/c"], { maxDepth: 3 }));
Deno.test("walkSync() accepts maxDepth option", () => assertWalkSyncPaths("depth", [".", "a", "a/b", "a/b/c"], { maxDepth: 3 }));
Deno.test("walk() accepts includeDirs option set to false", async () => await assertWalkPaths("depth", ["a/b/c/d/x"], { includeDirs: false }));
Deno.test("walkSync() accepts includeDirs option set to false", () => assertWalkSyncPaths("depth", ["a/b/c/d/x"], { includeDirs: false }));
Deno.test("walk() accepts includeFiles option set to false", async () => await assertWalkPaths("depth", [".", "a", "a/b", "a/b/c", "a/b/c/d"], { includeFiles: false, }));
Deno.test("walkSync() accepts includeFiles option set to false", () => assertWalkSyncPaths("depth", [".", "a", "a/b", "a/b/c", "a/b/c/d"], { includeFiles: false, }));
Deno.test("walk() accepts ext option as strings", async () => await assertWalkPaths("ext", ["y.rs", "x.ts"], { exts: [".rs", ".ts"], }));
Deno.test("walkSync() accepts ext option as strings", () => assertWalkSyncPaths("ext", ["y.rs", "x.ts"], { exts: [".rs", ".ts"], }));
Deno.test("walk() accepts ext option as regExps", async () => await assertWalkPaths("match", ["x", "y"], { match: [/x/, /y/], }));
Deno.test("walkSync() accepts ext option as regExps", () => assertWalkSyncPaths("match", ["x", "y"], { match: [/x/, /y/], }));
Deno.test("walk() accepts skip option as regExps", async () => await assertWalkPaths("match", [".", "z"], { skip: [/x/, /y/], }));
Deno.test("walkSync() accepts skip option as regExps", () => assertWalkSyncPaths("match", [".", "z"], { skip: [/x/, /y/], }));
// https://github.com/denoland/deno_std/issues/1358Deno.test("walk() accepts followSymlinks option set to true", async () => await assertWalkPaths("symlink", [".", "a", "a/z", "a", "a/z", "x", "x"], { followSymlinks: true, }));
Deno.test("walkSync() accepts followSymlinks option set to true", () => assertWalkSyncPaths("symlink", [".", "a", "a/z", "a", "a/z", "x", "x"], { followSymlinks: true, }));
Deno.test("walk() accepts followSymlinks option set to true with canonicalize option set to false", async () => await assertWalkPaths("symlink", [".", "a", "a/z", "b", "b/z", "x", "y"], { followSymlinks: true, canonicalize: false, }));
Deno.test("walkSync() accepts followSymlinks option set to true with canonicalize option set to false", () => assertWalkSyncPaths("symlink", [".", "a", "a/z", "b", "b/z", "x", "y"], { followSymlinks: true, canonicalize: false, }));
Deno.test("walk() accepts followSymlinks option set to false", async () => { await assertWalkPaths("symlink", [".", "a", "a/z", "b", "x", "y"], { followSymlinks: false, });});
Deno.test("walkSync() accepts followSymlinks option set to false", () => { assertWalkSyncPaths("symlink", [".", "a", "a/z", "b", "x", "y"], { followSymlinks: false, });});
Deno.test("walk() rejects Deno.errors.NotFound for non-existent root", async () => { const root = resolve(testdataDir, "non_existent"); await assertRejects( async () => await Array.fromAsync(walk(root)), Deno.errors.NotFound, );});
Deno.test("walkSync() throws Deno.errors.NotFound for non-existent root", () => { const root = resolve(testdataDir, "non_existent"); assertThrows(() => Array.from(walkSync(root)), Deno.errors.NotFound);});
// https://github.com/denoland/deno_std/issues/1789Deno.test({ name: "walk() walks unix socket", ignore: Deno.build.os === "windows", async fn() { const path = resolve(testdataDir, "socket", "a.sock"); try { using _listener = Deno.listen({ path, transport: "unix" }); await assertWalkPaths("socket", [".", "a.sock", ".gitignore"], { followSymlinks: true, }); } finally { await Deno.remove(path); } },});
// https://github.com/denoland/deno_std/issues/1789Deno.test({ name: "walkSync() walks unix socket", ignore: Deno.build.os === "windows", async fn() { const path = resolve(testdataDir, "socket", "a.sock"); try { using _listener = Deno.listen({ path, transport: "unix" }); assertWalkSyncPaths("socket", [".", "a.sock", ".gitignore"], { followSymlinks: true, }); } finally { await Deno.remove(path); } },});
Deno.test({ name: "walk() walks fifo files on unix", 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({ name: "walkSync() walks fifo files on unix", ignore: Deno.build.os === "windows", async fn() { const command = new Deno.Command("mkfifo", { args: [resolve(testdataDir, "fifo", "fifo")], }); await command.output(); assertWalkSyncPaths("fifo", [".", "fifo", ".gitignore"], { followSymlinks: true, }); },});
Deno.test("walk() rejects with WalkError when root is removed during execution", async () => { const root = resolve(testdataDir, "error"); await Deno.mkdir(root); try { await assertRejects(async () => { await Array.fromAsync( walk(root), async () => await Deno.remove(root, { recursive: true }), ); }, WalkError); } catch (err) { await Deno.remove(root, { recursive: true }); throw err; }});
std

Version Info

Tagged at
3 weeks ago