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
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertNotEquals,
assertThrowsAsync,
} from "../../../testing/asserts.ts";
import { writeFile } from "./_fs_writeFile.ts";
const decoder = new TextDecoder("utf-8");
Deno.test("Invalid encoding results in error()", function testEncodingErrors() {
assertThrowsAsync(
async () => {
await writeFile("some/path", "some data", "made-up-encoding");
},
Error,
`The value "made-up-encoding" is invalid for option "encoding"`
);
assertThrowsAsync(
async () => {
await writeFile("some/path", "some data", {
encoding: "made-up-encoding",
});
},
Error,
`The value "made-up-encoding" is invalid for option "encoding"`
);
});
Deno.test(
"Unsupported encoding results in error()",
function testUnsupportedEncoding() {
assertThrowsAsync(
async () => {
await writeFile("some/path", "some data", "hex");
},
Error,
`Not implemented: "hex" encoding`
);
assertThrowsAsync(
async () => {
await writeFile("some/path", "some data", {
encoding: "base64",
});
},
Error,
`Not implemented: "base64" encoding`
);
}
);
Deno.test(
"Data is written to correct rid",
async function testCorrectWriteUsingRid() {
const tempFile: string = await Deno.makeTempFile();
const file: Deno.File = await Deno.open(tempFile, {
create: true,
write: true,
read: true,
});
await writeFile(file.rid, "hello world");
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 openResourcesBeforeWrite: Deno.ResourceMap = Deno.resources();
await writeFile("_fs_writeFile_test_file.txt", "hello world");
assertEquals(Deno.resources(), openResourcesBeforeWrite);
const data = await Deno.readFile("_fs_writeFile_test_file.txt");
await Deno.remove("_fs_writeFile_test_file.txt");
assertEquals(decoder.decode(data), "hello world");
}
);
Deno.test("Mode is correctly set", async function testCorrectFileMode() {
if (Deno.build.os === "windows") return;
const filename = "_fs_writeFile_test_file.txt";
await writeFile(filename, "hello world", { mode: 0o777 });
const fileInfo = await Deno.stat(filename);
await Deno.remove(filename);
assert(fileInfo && fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777);
});
Deno.test(
"Mode is not set when rid is passed",
async function testCorrectFileModeRid() {
if (Deno.build.os === "windows") return;
const filename: string = await Deno.makeTempFile();
const file: Deno.File = await Deno.open(filename, {
create: true,
write: true,
read: true,
});
await writeFile(file.rid, "hello world", { mode: 0o777 });
Deno.close(file.rid);
const fileInfo = await Deno.stat(filename);
await Deno.remove(filename);
assert(fileInfo.mode);
assertNotEquals(fileInfo.mode & 0o777, 0o777);
}
);
|