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
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assertEquals,
assertThrows,
assertThrowsAsync,
unitTest,
} from "./test_util.ts";
function readFileString(filename: string | URL): string {
const dataRead = Deno.readFileSync(filename);
const dec = new TextDecoder("utf-8");
return dec.decode(dataRead);
}
function writeFileString(filename: string | URL, s: string) {
const enc = new TextEncoder();
const data = enc.encode(s);
Deno.writeFileSync(filename, data, { mode: 0o666 });
}
function assertSameContent(
filename1: string | URL,
filename2: string | URL,
) {
const data1 = Deno.readFileSync(filename1);
const data2 = Deno.readFileSync(filename2);
assertEquals(data1, data2);
}
unitTest(
{ perms: { read: true, write: true } },
function copyFileSyncSuccess() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
writeFileString(fromFilename, "Hello world!");
Deno.copyFileSync(fromFilename, toFilename);
// No change to original file
assertEquals(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { read: true, write: true } },
function copyFileSyncByUrl() {
const tempDir = Deno.makeTempDirSync();
const fromUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt`,
);
const toUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt`,
);
writeFileString(fromUrl, "Hello world!");
Deno.copyFileSync(fromUrl, toUrl);
// No change to original file
assertEquals(readFileString(fromUrl), "Hello world!");
// Original == Dest
assertSameContent(fromUrl, toUrl);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { write: true, read: true } },
function copyFileSyncFailure() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist
assertThrows(() => {
Deno.copyFileSync(fromFilename, toFilename);
}, Deno.errors.NotFound);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { write: true, read: false } },
function copyFileSyncPerm1() {
assertThrows(() => {
Deno.copyFileSync("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
unitTest(
{ perms: { write: false, read: true } },
function copyFileSyncPerm2() {
assertThrows(() => {
Deno.copyFileSync("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
unitTest(
{ perms: { read: true, write: true } },
function copyFileSyncOverwrite() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
writeFileString(fromFilename, "Hello world!");
// Make Dest exist and have different content
writeFileString(toFilename, "Goodbye!");
Deno.copyFileSync(fromFilename, toFilename);
// No change to original file
assertEquals(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { read: true, write: true } },
async function copyFileSuccess() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
writeFileString(fromFilename, "Hello world!");
await Deno.copyFile(fromFilename, toFilename);
// No change to original file
assertEquals(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { read: true, write: true } },
async function copyFileByUrl() {
const tempDir = Deno.makeTempDirSync();
const fromUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt`,
);
const toUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt`,
);
writeFileString(fromUrl, "Hello world!");
await Deno.copyFile(fromUrl, toUrl);
// No change to original file
assertEquals(readFileString(fromUrl), "Hello world!");
// Original == Dest
assertSameContent(fromUrl, toUrl);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { read: true, write: true } },
async function copyFileFailure() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist
await assertThrowsAsync(async () => {
await Deno.copyFile(fromFilename, toFilename);
}, Deno.errors.NotFound);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { read: true, write: true } },
async function copyFileOverwrite() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
writeFileString(fromFilename, "Hello world!");
// Make Dest exist and have different content
writeFileString(toFilename, "Goodbye!");
await Deno.copyFile(fromFilename, toFilename);
// No change to original file
assertEquals(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
Deno.removeSync(tempDir, { recursive: true });
},
);
unitTest(
{ perms: { read: false, write: true } },
async function copyFilePerm1() {
await assertThrowsAsync(async () => {
await Deno.copyFile("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
unitTest(
{ perms: { read: true, write: false } },
async function copyFilePerm2() {
await assertThrowsAsync(async () => {
await Deno.copyFile("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
|