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

_fs_writeFile_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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertNotEquals, assertRejects, assertThrows,} from "../../testing/asserts.ts";import { writeFile, writeFileSync } from "./_fs_writeFile.ts";import type { TextEncodings } from "../_utils.ts";import * as path from "../../path/mod.ts";import { isWindows } from "../../_util/os.ts";import { AbortError } from "./../internal/errors.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testDataDir = path.resolve(moduleDir, "testdata");const decoder = new TextDecoder("utf-8");
Deno.test("Callback must be a function error", function fn() { assertThrows( () => { writeFile("some/path", "some data", "utf8"); }, TypeError, "Callback must be a function.", );});
Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrows( () => { // @ts-expect-error Type '"made-up-encoding"' is not assignable to type writeFile("some/path", "some data", "made-up-encoding", () => {}); }, Error, `The value "made-up-encoding" is invalid for option "encoding"`, );
assertThrows( () => { // @ts-expect-error Type '"made-up-encoding"' is not assignable to type writeFileSync("some/path", "some data", "made-up-encoding"); }, Error, `The value "made-up-encoding" is invalid for option "encoding"`, );
assertThrows( () => { writeFile( "some/path", "some data", { // @ts-expect-error Type '"made-up-encoding"' is not assignable to type encoding: "made-up-encoding", }, () => {}, ); }, Error, `The value "made-up-encoding" is invalid for option "encoding"`, );
assertThrows( () => { writeFileSync("some/path", "some data", { // @ts-expect-error Type '"made-up-encoding"' is not assignable to type encoding: "made-up-encoding", }); }, Error, `The value "made-up-encoding" is invalid for option "encoding"`, );});
Deno.test( "Unsupported encoding results in error()", function testUnsupportedEncoding() { assertThrows( () => { writeFile("some/path", "some data", "utf16le", () => {}); }, Error, `Not implemented: "utf16le" encoding`, );
assertThrows( () => { writeFileSync("some/path", "some data", "utf16le"); }, Error, `Not implemented: "utf16le" encoding`, ); },);
Deno.test( "Data is written to correct rid", async function testCorrectWriteUsingRid() { 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) => { writeFile(file.rid, "hello world", (err) => { if (err) return reject(err); resolve(); }); }); Deno.close(file.rid);
const data = await Deno.readFile(tempFile); await Deno.remove(tempFile); assertEquals(decoder.decode(data), "hello world"); },);
Deno.test( "Data is written to correct file", async function testCorrectWriteUsingPath() { const res = await new Promise((resolve) => { writeFile("_fs_writeFile_test_file.txt", "hello world", resolve); });
const data = await Deno.readFile("_fs_writeFile_test_file.txt"); await Deno.remove("_fs_writeFile_test_file.txt"); assertEquals(res, null); assertEquals(decoder.decode(data), "hello world"); },);
Deno.test( "Data is written to correct file encodings", async function testCorrectWriteUsingDifferentEncodings() { const encodings = [ ["hex", "68656c6c6f20776f726c64"], ["HEX", "68656c6c6f20776f726c64"], ["base64", "aGVsbG8gd29ybGQ="], ["BASE64", "aGVsbG8gd29ybGQ="], ["utf8", "hello world"], ["utf-8", "hello world"], ];
for (const [encoding, value] of encodings) { const res = await new Promise((resolve) => { writeFile( "_fs_writeFile_test_file.txt", value, encoding as TextEncodings, resolve, ); });
const data = await Deno.readFile("_fs_writeFile_test_file.txt"); await Deno.remove("_fs_writeFile_test_file.txt"); assertEquals(res, null); assertEquals(decoder.decode(data), "hello world"); } },);
Deno.test("Path can be an URL", async function testCorrectWriteUsingURL() { const url = new URL( isWindows ? "file:///" + path .join(testDataDir, "_fs_writeFile_test_file_url.txt") .replace(/\\/g, "/") : "file://" + path.join(testDataDir, "_fs_writeFile_test_file_url.txt"), ); const filePath = path.fromFileUrl(url); const res = await new Promise((resolve) => { writeFile(url, "hello world", resolve); }); assert(res === null);
const data = await Deno.readFile(filePath); await Deno.remove(filePath); assertEquals(res, null); assertEquals(decoder.decode(data), "hello world");});
Deno.test("Mode is correctly set", async function testCorrectFileMode() { if (isWindows) return; const filename = "_fs_writeFile_test_file.txt";
const res = await new Promise((resolve) => { writeFile(filename, "hello world", { mode: 0o777 }, resolve); });
const fileInfo = await Deno.stat(filename); await Deno.remove(filename); assertEquals(res, null); assert(fileInfo && fileInfo.mode); assertEquals(fileInfo.mode & 0o777, 0o777);});
Deno.test( "Mode is not set when rid is passed", async function testCorrectFileModeRid() { if (isWindows) return;
const filename: string = await Deno.makeTempFile(); const file: Deno.FsFile = await Deno.open(filename, { create: true, write: true, read: true, });
await new Promise<void>((resolve, reject) => { writeFile(file.rid, "hello world", { mode: 0o777 }, (err) => { if (err) return reject(err); resolve(); }); }); Deno.close(file.rid);
const fileInfo = await Deno.stat(filename); await Deno.remove(filename); assert(fileInfo.mode); assertNotEquals(fileInfo.mode & 0o777, 0o777); },);
Deno.test( "Is cancellable with an AbortSignal", async function testIsCancellableWithAbortSignal() { const tempFile: string = await Deno.makeTempFile(); const controller = new AbortController(); const signal = controller.signal;
const writeFilePromise = new Promise<void>((resolve, reject) => { writeFile(tempFile, "hello world", { signal }, (err) => { if (err) return reject(err); resolve(); }); }); controller.abort();
await assertRejects( () => writeFilePromise, AbortError, );
Deno.removeSync(tempFile); },);
Deno.test( "Data is written synchronously to correct rid", function testCorrectWriteSyncUsingRid() { const tempFile: string = Deno.makeTempFileSync(); const file: Deno.FsFile = Deno.openSync(tempFile, { create: true, write: true, read: true, });
writeFileSync(file.rid, "hello world"); Deno.close(file.rid);
const data = Deno.readFileSync(tempFile); Deno.removeSync(tempFile); assertEquals(decoder.decode(data), "hello world"); },);
Deno.test( "Data is written to correct file encodings", function testCorrectWriteSyncUsingDifferentEncodings() { const encodings = [ ["hex", "68656c6c6f20776f726c64"], ["HEX", "68656c6c6f20776f726c64"], ["base64", "aGVsbG8gd29ybGQ="], ["BASE64", "aGVsbG8gd29ybGQ="], ["utf8", "hello world"], ["utf-8", "hello world"], ];
for (const [encoding, value] of encodings) { const file = "_fs_writeFileSync_test_file"; writeFileSync(file, value, encoding as TextEncodings);
const data = Deno.readFileSync(file); Deno.removeSync(file); assertEquals(decoder.decode(data), "hello world"); } },);
Deno.test( "Data is written synchronously to correct file", function testCorrectWriteSyncUsingPath() { const file = "_fs_writeFileSync_test_file";
writeFileSync(file, "hello world");
const data = Deno.readFileSync(file); Deno.removeSync(file); assertEquals(decoder.decode(data), "hello world"); },);
Deno.test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() { const filePath = path.join( testDataDir, "_fs_writeFileSync_test_file_url.txt", ); const url = new URL( isWindows ? "file:///" + filePath.replace(/\\/g, "/") : "file://" + filePath, ); writeFileSync(url, "hello world");
const data = Deno.readFileSync(filePath); Deno.removeSync(filePath); assertEquals(decoder.decode(data), "hello world");});
Deno.test( "Mode is correctly set when writing synchronously", function testCorrectFileModeSync() { if (isWindows) return; const filename = "_fs_writeFileSync_test_file.txt";
writeFileSync(filename, "hello world", { mode: 0o777 });
const fileInfo = Deno.statSync(filename); Deno.removeSync(filename); assert(fileInfo && fileInfo.mode); assertEquals(fileInfo.mode & 0o777, 0o777); },);
std

Version Info

Tagged at
a year ago