deno.land / std@0.166.0 / node / _fs / _fs_dir_test.ts

_fs_dir_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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, fail } from "../../testing/asserts.ts";import { assertCallbackErrorUncaught } from "../_test_utils.ts";import Dir from "./_fs_dir.ts";import type Dirent from "./_fs_dirent.ts";
Deno.test({ name: "Closing current directory with callback is successful", fn() { let calledBack = false; // deno-lint-ignore no-explicit-any new Dir(".").close((valOrErr: any) => { assert(!valOrErr); calledBack = true; }); assert(calledBack); },});
Deno.test({ name: "Closing current directory without callback returns void Promise", async fn() { await new Dir(".").close(); },});
Deno.test({ name: "Closing current directory synchronously works", fn() { new Dir(".").closeSync(); },});
Deno.test({ name: "Path is correctly returned", fn() { assertEquals(new Dir("std/node").path, "std/node");
const enc: Uint8Array = new TextEncoder().encode("std/node"); assertEquals(new Dir(enc).path, "std/node"); },});
Deno.test({ name: "read returns null for empty directory", async fn() { const testDir: string = Deno.makeTempDirSync(); try { const file: Dirent | null = await new Dir(testDir).read(); assert(file === null);
let calledBack = false; const fileFromCallback: Dirent | null = await new Dir( testDir, // deno-lint-ignore no-explicit-any ).read((err: any, res: Dirent) => { assert(res === null); assert(err === null); calledBack = true; }); assert(fileFromCallback === null); assert(calledBack);
assertEquals(new Dir(testDir).readSync(), null); } finally { Deno.removeSync(testDir); } },});
Deno.test({ name: "Async read returns one file at a time", async fn() { const testDir: string = Deno.makeTempDirSync(); const f1 = Deno.createSync(testDir + "/foo.txt"); f1.close(); const f2 = Deno.createSync(testDir + "/bar.txt"); f2.close();
try { let secondCallback = false; const dir: Dir = new Dir(testDir); const firstRead: Dirent | null = await dir.read(); const secondRead: Dirent | null = await dir.read( // deno-lint-ignore no-explicit-any (_err: any, secondResult: Dirent) => { assert( secondResult.name === "bar.txt" || secondResult.name === "foo.txt", ); secondCallback = true; }, ); const thirdRead: Dirent | null = await dir.read(); const fourthRead: Dirent | null = await dir.read();
if (firstRead?.name === "foo.txt") { assertEquals(secondRead?.name, "bar.txt"); } else if (firstRead?.name === "bar.txt") { assertEquals(secondRead?.name, "foo.txt"); } else { fail("File not found during read"); } assert(secondCallback); assert(thirdRead === null); assert(fourthRead === null); } finally { Deno.removeSync(testDir, { recursive: true }); } },});
Deno.test({ name: "Sync read returns one file at a time", fn() { const testDir: string = Deno.makeTempDirSync(); const f1 = Deno.createSync(testDir + "/foo.txt"); f1.close(); const f2 = Deno.createSync(testDir + "/bar.txt"); f2.close();
try { const dir: Dir = new Dir(testDir); const firstRead: Dirent | null = dir.readSync(); const secondRead: Dirent | null = dir.readSync(); const thirdRead: Dirent | null = dir.readSync(); const fourthRead: Dirent | null = dir.readSync();
if (firstRead?.name === "foo.txt") { assertEquals(secondRead?.name, "bar.txt"); } else if (firstRead?.name === "bar.txt") { assertEquals(secondRead?.name, "foo.txt"); } else { fail("File not found during read"); } assert(thirdRead === null); assert(fourthRead === null); } finally { Deno.removeSync(testDir, { recursive: true }); } },});
Deno.test({ name: "Async iteration over existing directory", async fn() { const testDir: string = Deno.makeTempDirSync(); const f1 = Deno.createSync(testDir + "/foo.txt"); f1.close(); const f2 = Deno.createSync(testDir + "/bar.txt"); f2.close();
try { const dir: Dir = new Dir(testDir); const results: Array<string | null> = [];
for await (const file of dir[Symbol.asyncIterator]()) { results.push(file.name); }
assert(results.length === 2); assert(results.includes("foo.txt")); assert(results.includes("bar.txt")); } finally { Deno.removeSync(testDir, { recursive: true }); } },});
Deno.test("[std/node/fs] Dir.close callback isn't called twice if error is thrown", async () => { const tempDir = await Deno.makeTempDir(); const importUrl = new URL("./_fs_dir.ts", import.meta.url); await assertCallbackErrorUncaught({ prelude: ` import Dir from ${JSON.stringify(importUrl)};
const dir = new Dir(${JSON.stringify(tempDir)}); `, invocation: "dir.close(", async cleanup() { await Deno.remove(tempDir); }, });});
Deno.test("[std/node/fs] Dir.read callback isn't called twice if error is thrown", async () => { const tempDir = await Deno.makeTempDir(); const importUrl = new URL("./_fs_dir.ts", import.meta.url); await assertCallbackErrorUncaught({ prelude: ` import Dir from ${JSON.stringify(importUrl)};
const dir = new Dir(${JSON.stringify(tempDir)}); `, invocation: "dir.read(", async cleanup() { await Deno.remove(tempDir); }, });});
std

Version Info

Tagged at
a year ago