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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts";
const REMOVE_METHODS = ["remove", "removeSync"] as const;
unitTest(
{ perms: { write: true, read: true } },
async function removeDirSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
// REMOVE EMPTY DIRECTORY
const path = Deno.makeTempDirSync() + "/subdir";
Deno.mkdirSync(path);
const pathInfo = Deno.statSync(path);
assert(pathInfo.isDirectory); // check exist first
await Deno[method](path); // remove
// We then check again after remove
let err;
try {
Deno.statSync(path);
} catch (e) {
err = e;
}
// Directory is gone
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest(
{ perms: { write: true, read: true } },
async function removeFileSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
// REMOVE FILE
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
Deno.writeFileSync(filename, data, { mode: 0o666 });
const fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile); // check exist first
await Deno[method](filename); // remove
// We then check again after remove
let err;
try {
Deno.statSync(filename);
} catch (e) {
err = e;
}
// File is gone
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest(
{ perms: { write: true, read: true } },
async function removeFileByUrl(): Promise<void> {
for (const method of REMOVE_METHODS) {
// REMOVE FILE
const enc = new TextEncoder();
const data = enc.encode("Hello");
const tempDir = Deno.makeTempDirSync();
const fileUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`
);
Deno.writeFileSync(fileUrl, data, { mode: 0o666 });
const fileInfo = Deno.statSync(fileUrl);
assert(fileInfo.isFile); // check exist first
await Deno[method](fileUrl); // remove
// We then check again after remove
let err;
try {
Deno.statSync(fileUrl);
} catch (e) {
err = e;
}
// File is gone
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest(
{ perms: { write: true, read: true } },
async function removeFail(): Promise<void> {
for (const method of REMOVE_METHODS) {
// NON-EMPTY DIRECTORY
const path = Deno.makeTempDirSync() + "/dir/subdir";
const subPath = path + "/subsubdir";
Deno.mkdirSync(path, { recursive: true });
Deno.mkdirSync(subPath);
const pathInfo = Deno.statSync(path);
assert(pathInfo.isDirectory); // check exist first
const subPathInfo = Deno.statSync(subPath);
assert(subPathInfo.isDirectory); // check exist first
let err;
try {
// Should not be able to recursively remove
await Deno[method](path);
} catch (e) {
err = e;
}
// TODO(ry) Is Other really the error we should get here? What would Go do?
assert(err instanceof Error);
// NON-EXISTENT DIRECTORY/FILE
try {
// Non-existent
await Deno[method]("/baddir");
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest(
{ perms: { write: true, read: true } },
async function removeDanglingSymlinkSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
const danglingSymlinkPath = Deno.makeTempDirSync() + "/dangling_symlink";
if (Deno.build.os === "windows") {
Deno.symlinkSync("unexistent_file", danglingSymlinkPath, {
type: "file",
});
} else {
Deno.symlinkSync("unexistent_file", danglingSymlinkPath);
}
const pathInfo = Deno.lstatSync(danglingSymlinkPath);
assert(pathInfo.isSymlink);
await Deno[method](danglingSymlinkPath);
let err;
try {
Deno.lstatSync(danglingSymlinkPath);
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest(
{ perms: { write: true, read: true } },
async function removeValidSymlinkSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
const encoder = new TextEncoder();
const data = encoder.encode("Test");
const tempDir = Deno.makeTempDirSync();
const filePath = tempDir + "/test.txt";
const validSymlinkPath = tempDir + "/valid_symlink";
Deno.writeFileSync(filePath, data, { mode: 0o666 });
if (Deno.build.os === "windows") {
Deno.symlinkSync(filePath, validSymlinkPath, { type: "file" });
} else {
Deno.symlinkSync(filePath, validSymlinkPath);
}
const symlinkPathInfo = Deno.statSync(validSymlinkPath);
assert(symlinkPathInfo.isFile);
await Deno[method](validSymlinkPath);
let err;
try {
Deno.statSync(validSymlinkPath);
} catch (e) {
err = e;
}
await Deno[method](filePath);
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest({ perms: { write: false } }, async function removePerm(): Promise<
void
> {
for (const method of REMOVE_METHODS) {
let err;
try {
await Deno[method]("/baddir");
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
});
unitTest(
{ perms: { write: true, read: true } },
async function removeAllDirSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
// REMOVE EMPTY DIRECTORY
let path = Deno.makeTempDirSync() + "/dir/subdir";
Deno.mkdirSync(path, { recursive: true });
let pathInfo = Deno.statSync(path);
assert(pathInfo.isDirectory); // check exist first
await Deno[method](path, { recursive: true }); // remove
// We then check again after remove
let err;
try {
Deno.statSync(path);
} catch (e) {
err = e;
}
// Directory is gone
assert(err instanceof Deno.errors.NotFound);
// REMOVE NON-EMPTY DIRECTORY
path = Deno.makeTempDirSync() + "/dir/subdir";
const subPath = path + "/subsubdir";
Deno.mkdirSync(path, { recursive: true });
Deno.mkdirSync(subPath);
pathInfo = Deno.statSync(path);
assert(pathInfo.isDirectory); // check exist first
const subPathInfo = Deno.statSync(subPath);
assert(subPathInfo.isDirectory); // check exist first
await Deno[method](path, { recursive: true }); // remove
// We then check parent directory again after remove
try {
Deno.statSync(path);
} catch (e) {
err = e;
}
// Directory is gone
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest(
{ perms: { write: true, read: true } },
async function removeAllFileSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
// REMOVE FILE
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
Deno.writeFileSync(filename, data, { mode: 0o666 });
const fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile); // check exist first
await Deno[method](filename, { recursive: true }); // remove
// We then check again after remove
let err;
try {
Deno.statSync(filename);
} catch (e) {
err = e;
}
// File is gone
assert(err instanceof Deno.errors.NotFound);
}
}
);
unitTest({ perms: { write: true } }, async function removeAllFail(): Promise<
void
> {
for (const method of REMOVE_METHODS) {
// NON-EXISTENT DIRECTORY/FILE
let err;
try {
// Non-existent
await Deno[method]("/baddir", { recursive: true });
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}
});
unitTest({ perms: { write: false } }, async function removeAllPerm(): Promise<
void
> {
for (const method of REMOVE_METHODS) {
let err;
try {
await Deno[method]("/baddir", { recursive: true });
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
});
unitTest(
{
ignore: Deno.build.os === "windows",
perms: { write: true, read: true },
},
async function removeUnixSocketSuccess(): Promise<void> {
for (const method of REMOVE_METHODS) {
// MAKE TEMPORARY UNIX SOCKET
const path = Deno.makeTempDirSync() + "/test.sock";
const listener = Deno.listen({ transport: "unix", path });
listener.close();
Deno.statSync(path); // check if unix socket exists
await Deno[method](path);
let err;
try {
Deno.statSync(path);
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}
}
);
if (Deno.build.os === "windows") {
unitTest(
{ perms: { run: true, write: true, read: true } },
async function removeFileSymlink(): Promise<void> {
const symlink = Deno.run({
cmd: ["cmd", "/c", "mklink", "file_link", "bar"],
stdout: "null",
});
assert(await symlink.status());
symlink.close();
await Deno.remove("file_link");
let err;
try {
await Deno.lstat("file_link");
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}
);
unitTest(
{ perms: { run: true, write: true, read: true } },
async function removeDirSymlink(): Promise<void> {
const symlink = Deno.run({
cmd: ["cmd", "/c", "mklink", "/d", "dir_link", "bar"],
stdout: "null",
});
assert(await symlink.status());
symlink.close();
await Deno.remove("dir_link");
let err;
try {
await Deno.lstat("dir_link");
} catch (e) {
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}
);
}
|