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

_fs_appendFile_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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertThrows, fail } from "../../testing/asserts.ts";import { appendFile, appendFileSync } from "./_fs_appendFile.ts";import { fromFileUrl } from "../path.ts";import { assertCallbackErrorUncaught } from "../_test_utils.ts";
const decoder = new TextDecoder("utf-8");
Deno.test({ name: "No callback Fn results in Error", fn() { assertThrows( () => { appendFile("some/path", "some data", "utf8"); }, Error, "The \"cb\" argument must be of type function. Received type string ('utf8')", ); },});
Deno.test({ name: "Unsupported encoding results in error()", fn() { assertThrows( () => { // @ts-expect-error Type '"made-up-encoding"' is not assignable to type appendFile("some/path", "some data", "made-up-encoding", () => {}); }, Error, "The argument 'made-up-encoding' is invalid encoding. Received 'encoding'", ); assertThrows( () => { appendFile( "some/path", "some data", // @ts-expect-error Type '"made-up-encoding"' is not assignable to type { encoding: "made-up-encoding" }, () => {}, ); }, Error, "The argument 'made-up-encoding' is invalid encoding. Received 'encoding'", ); assertThrows( // @ts-expect-error Type '"made-up-encoding"' is not assignable to type () => appendFileSync("some/path", "some data", "made-up-encoding"), Error, "The argument 'made-up-encoding' is invalid encoding. Received 'encoding'", ); assertThrows( () => appendFileSync("some/path", "some data", { // @ts-expect-error Type '"made-up-encoding"' is not assignable to type encoding: "made-up-encoding", }), Error, "The argument 'made-up-encoding' is invalid encoding. Received 'encoding'", ); },});
Deno.test({ name: "Async: Data is written to passed in rid", async fn() { const tempFile: string = await Deno.makeTempFile(); const file: Deno.FsFile = await Deno.open(tempFile, { create: true, write: true, read: true, }); await new Promise<void>((resolve, reject) => { appendFile(file.rid, "hello world", (err) => { if (err) reject(); else resolve(); }); }) .then(async () => { const data = await Deno.readFile(tempFile); assertEquals(decoder.decode(data), "hello world"); }, () => { fail("No error expected"); }) .finally(async () => { Deno.close(file.rid); await Deno.remove(tempFile); }); },});
Deno.test({ name: "Async: Data is written to passed in file path", async fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); await new Promise<void>((resolve, reject) => { appendFile("_fs_appendFile_test_file.txt", "hello world", (err) => { if (err) reject(err); else resolve(); }); }) .then(async () => { assertEquals(Deno.resources(), openResourcesBeforeAppend); const data = await Deno.readFile("_fs_appendFile_test_file.txt"); assertEquals(decoder.decode(data), "hello world"); }, (err) => { fail("No error was expected: " + err); }) .finally(async () => { await Deno.remove("_fs_appendFile_test_file.txt"); }); },});
Deno.test({ name: "Async: Data is written to passed in URL", async fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); const fileURL = new URL("_fs_appendFile_test_file.txt", import.meta.url); await new Promise<void>((resolve, reject) => { appendFile(fileURL, "hello world", (err) => { if (err) reject(err); else resolve(); }); }) .then(async () => { assertEquals(Deno.resources(), openResourcesBeforeAppend); const data = await Deno.readFile(fromFileUrl(fileURL)); assertEquals(decoder.decode(data), "hello world"); }, (err) => { fail("No error was expected: " + err); }) .finally(async () => { await Deno.remove(fromFileUrl(fileURL)); }); },});
Deno.test({ name: "Async: Callback is made with error if attempting to append data to an existing file with 'ax' flag", async fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); const tempFile: string = await Deno.makeTempFile(); await new Promise<void>((resolve, reject) => { appendFile(tempFile, "hello world", { flag: "ax" }, (err) => { if (err) reject(err); else resolve(); }); }) .then(() => { fail("Expected error to be thrown"); }, () => { assertEquals(Deno.resources(), openResourcesBeforeAppend); }) .finally(async () => { await Deno.remove(tempFile); }); },});
Deno.test({ name: "Sync: Data is written to passed in rid", fn() { const tempFile: string = Deno.makeTempFileSync(); const file: Deno.FsFile = Deno.openSync(tempFile, { create: true, write: true, read: true, }); appendFileSync(file.rid, "hello world"); Deno.close(file.rid); const data = Deno.readFileSync(tempFile); assertEquals(decoder.decode(data), "hello world"); Deno.removeSync(tempFile); },});
Deno.test({ name: "Sync: Data is written to passed in file path", fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); appendFileSync("_fs_appendFile_test_file_sync.txt", "hello world"); assertEquals(Deno.resources(), openResourcesBeforeAppend); const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt"); assertEquals(decoder.decode(data), "hello world"); Deno.removeSync("_fs_appendFile_test_file_sync.txt"); },});
Deno.test({ name: "Sync: error thrown if attempting to append data to an existing file with 'ax' flag", fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); const tempFile: string = Deno.makeTempFileSync(); assertThrows( () => appendFileSync(tempFile, "hello world", { flag: "ax" }), Error, "", ); assertEquals(Deno.resources(), openResourcesBeforeAppend); Deno.removeSync(tempFile); },});
Deno.test({ name: "Sync: Data is written in Uint8Array to passed in file path", fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); const testData = new TextEncoder().encode("hello world"); appendFileSync("_fs_appendFile_test_file_sync.txt", testData); assertEquals(Deno.resources(), openResourcesBeforeAppend); const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt"); assertEquals(data, testData); Deno.removeSync("_fs_appendFile_test_file_sync.txt"); },});
Deno.test({ name: "Async: Data is written in Uint8Array to passed in file path", async fn() { const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources(); const testData = new TextEncoder().encode("hello world"); await new Promise<void>((resolve, reject) => { appendFile("_fs_appendFile_test_file.txt", testData, (err) => { if (err) reject(err); else resolve(); }); }) .then(async () => { assertEquals(Deno.resources(), openResourcesBeforeAppend); const data = await Deno.readFile("_fs_appendFile_test_file.txt"); assertEquals(data, testData); }, (err) => { fail("No error was expected: " + err); }) .finally(async () => { await Deno.remove("_fs_appendFile_test_file.txt"); }); },});
Deno.test("[std/node/fs] appendFile callback isn't called twice if error is thrown", async () => { const tempFile = await Deno.makeTempFile(); const importUrl = new URL("./_fs_appendFile.ts", import.meta.url); await assertCallbackErrorUncaught({ prelude: `import { appendFile } from ${JSON.stringify(importUrl)}`, invocation: `appendFile(${JSON.stringify(tempFile)}, "hello world", `, async cleanup() { await Deno.remove(tempFile); }, });});
std

Version Info

Tagged at
a year ago