deno.land / std@0.91.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.import { walk, WalkEntry, WalkOptions, walkSync } from "./walk.ts";import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
export function testWalk( setup: (arg0: string) => void | Promise<void>, t: () => void | Promise<void>, ignore = false,): void { const name = t.name; async function fn(): Promise<void> { const origCwd = Deno.cwd(); const d = await Deno.makeTempDir(); Deno.chdir(d); try { await setup(d); await t(); } finally { Deno.chdir(origCwd); await Deno.remove(d, { recursive: true }); } } Deno.test({ ignore, name: `[walk] ${name}`, fn });}
function normalize({ path }: WalkEntry): string { return path.replace(/\\/g, "/");}
export async function walkArray( root: string, options: WalkOptions = {},): Promise<string[]> { const arr: string[] = []; for await (const w of walk(root, { ...options })) { arr.push(normalize(w)); } arr.sort(); // TODO(ry) Remove sort. The order should be deterministic. const arrSync = Array.from(walkSync(root, options), normalize); arrSync.sort(); // TODO(ry) Remove sort. The order should be deterministic. assertEquals(arr, arrSync); return arr;}
export async function touch(path: string): Promise<void> { const f = await Deno.create(path); f.close();}
function assertReady(expectedLength: number): void { const arr = Array.from(walkSync("."), normalize);
assertEquals(arr.length, expectedLength);}
testWalk( async (d: string): Promise<void> => { await Deno.mkdir(d + "/empty"); }, async function emptyDir(): Promise<void> { const arr = await walkArray("."); assertEquals(arr, [".", "empty"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x"); }, async function singleFile(): Promise<void> { const arr = await walkArray("."); assertEquals(arr, [".", "x"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x"); }, async function iteratable(): Promise<void> { let count = 0; for (const _ of walkSync(".")) { count += 1; } assertEquals(count, 2); for await (const _ of walk(".")) { count += 1; } assertEquals(count, 4); },);
testWalk( async (d: string): Promise<void> => { await Deno.mkdir(d + "/a"); await touch(d + "/a/x"); }, async function nestedSingleFile(): Promise<void> { const arr = await walkArray("."); assertEquals(arr, [".", "a", "a/x"]); },);
testWalk( async (d: string): Promise<void> => { await Deno.mkdir(d + "/a/b/c/d", { recursive: true }); await touch(d + "/a/b/c/d/x"); }, async function depth(): Promise<void> { assertReady(6); const arr3 = await walkArray(".", { maxDepth: 3 }); assertEquals(arr3, [".", "a", "a/b", "a/b/c"]); const arr5 = await walkArray(".", { maxDepth: 5 }); assertEquals(arr5, [".", "a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/x"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/a"); await Deno.mkdir(d + "/b"); await touch(d + "/b/c"); }, async function includeDirs(): Promise<void> { assertReady(4); const arr = await walkArray(".", { includeDirs: false }); assertEquals(arr, ["a", "b/c"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/a"); await Deno.mkdir(d + "/b"); await touch(d + "/b/c"); }, async function includeFiles(): Promise<void> { assertReady(4); const arr = await walkArray(".", { includeFiles: false }); assertEquals(arr, [".", "b"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x.ts"); await touch(d + "/y.rs"); }, async function ext(): Promise<void> { assertReady(3); const arr = await walkArray(".", { exts: [".ts"] }); assertEquals(arr, ["x.ts"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x.ts"); await touch(d + "/y.rs"); await touch(d + "/z.py"); }, async function extAny(): Promise<void> { assertReady(4); const arr = await walkArray(".", { exts: [".rs", ".ts"] }); assertEquals(arr, ["x.ts", "y.rs"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x"); await touch(d + "/y"); }, async function match(): Promise<void> { assertReady(3); const arr = await walkArray(".", { match: [/x/] }); assertEquals(arr, ["x"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x"); await touch(d + "/y"); await touch(d + "/z"); }, async function matchAny(): Promise<void> { assertReady(4); const arr = await walkArray(".", { match: [/x/, /y/] }); assertEquals(arr, ["x", "y"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x"); await touch(d + "/y"); }, async function skip(): Promise<void> { assertReady(3); const arr = await walkArray(".", { skip: [/x/] }); assertEquals(arr, [".", "y"]); },);
testWalk( async (d: string): Promise<void> => { await touch(d + "/x"); await touch(d + "/y"); await touch(d + "/z"); }, async function skipAny(): Promise<void> { assertReady(4); const arr = await walkArray(".", { skip: [/x/, /y/] }); assertEquals(arr, [".", "z"]); },);
testWalk( async (d: string): Promise<void> => { await Deno.mkdir(d + "/a"); await Deno.mkdir(d + "/b"); await touch(d + "/a/x"); await touch(d + "/a/y"); await touch(d + "/b/z"); }, async function subDir(): Promise<void> { assertReady(6); const arr = await walkArray("b"); assertEquals(arr, ["b", "b/z"]); },);
testWalk( async (_d: string): Promise<void> => {}, async function nonexistentRoot(): Promise<void> { await assertThrowsAsync(async () => { await walkArray("nonexistent"); }, Deno.errors.NotFound); },);
testWalk( async (d: string): Promise<void> => { await Deno.mkdir(d + "/a"); await Deno.mkdir(d + "/b"); await touch(d + "/a/x"); await touch(d + "/a/y"); await touch(d + "/b/z"); await Deno.symlink(d + "/b", d + "/a/bb"); }, async function symlink(): Promise<void> { assertReady(6); const files = await walkArray("a"); assertEquals(files.length, 3); assert(!files.includes("a/bb/z"));
const arr = await walkArray("a", { followSymlinks: true }); assertEquals(arr.length, 5); assert(arr.some((f): boolean => f.endsWith("/b/z"))); },);
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉