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
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
function assertMissing(path: string): void {
let caughtErr = false;
let info;
try {
info = Deno.lstatSync(path);
} catch (e) {
caughtErr = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtErr);
assertEquals(info, undefined);
}
function assertFile(path: string): void {
const info = Deno.lstatSync(path);
assert(info.isFile);
}
function assertDirectory(path: string, mode?: number): void {
const info = Deno.lstatSync(path);
assert(info.isDirectory);
if (Deno.build.os !== "windows" && mode !== undefined) {
assertEquals(info.mode! & 0o777, mode & ~Deno.umask());
}
}
unitTest(
{ perms: { read: true, write: true } },
function renameSyncSuccess(): void {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
Deno.renameSync(oldpath, newpath);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
unitTest(
{ perms: { read: false, write: true } },
function renameSyncReadPerm(): void {
assertThrows(() => {
const oldpath = "/oldbaddir";
const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath);
}, Deno.errors.PermissionDenied);
},
);
unitTest(
{ perms: { read: true, write: false } },
function renameSyncWritePerm(): void {
assertThrows(() => {
const oldpath = "/oldbaddir";
const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath);
}, Deno.errors.PermissionDenied);
},
);
unitTest(
{ perms: { read: true, write: true } },
async function renameSuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
await Deno.rename(oldpath, newpath);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
function readFileString(filename: string): string {
const dataRead = Deno.readFileSync(filename);
const dec = new TextDecoder("utf-8");
return dec.decode(dataRead);
}
function writeFileString(filename: string, s: string): void {
const enc = new TextEncoder();
const data = enc.encode(s);
Deno.writeFileSync(filename, data, { mode: 0o666 });
}
unitTest(
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
function renameSyncErrorsUnix(): void {
const testDir = Deno.makeTempDirSync();
const oldfile = testDir + "/oldfile";
const olddir = testDir + "/olddir";
const emptydir = testDir + "/empty";
const fulldir = testDir + "/dir";
const file = fulldir + "/file";
writeFileString(oldfile, "Hello");
Deno.mkdirSync(olddir);
Deno.mkdirSync(emptydir);
Deno.mkdirSync(fulldir);
writeFileString(file, "world");
assertThrows(
(): void => {
Deno.renameSync(oldfile, emptydir);
},
Error,
"Is a directory",
);
assertThrows(
(): void => {
Deno.renameSync(olddir, fulldir);
},
Error,
"Directory not empty",
);
assertThrows(
(): void => {
Deno.renameSync(olddir, file);
},
Error,
"Not a directory",
);
const fileLink = testDir + "/fileLink";
const dirLink = testDir + "/dirLink";
const danglingLink = testDir + "/danglingLink";
Deno.symlinkSync(file, fileLink);
Deno.symlinkSync(emptydir, dirLink);
Deno.symlinkSync(testDir + "/nonexistent", danglingLink);
assertThrows(
(): void => {
Deno.renameSync(olddir, fileLink);
},
Error,
"Not a directory",
);
assertThrows(
(): void => {
Deno.renameSync(olddir, dirLink);
},
Error,
"Not a directory",
);
assertThrows(
(): void => {
Deno.renameSync(olddir, danglingLink);
},
Error,
"Not a directory",
);
// should succeed on Unix
Deno.renameSync(olddir, emptydir);
Deno.renameSync(oldfile, dirLink);
Deno.renameSync(dirLink, danglingLink);
assertFile(danglingLink);
assertEquals("Hello", readFileString(danglingLink));
},
);
unitTest(
{ ignore: Deno.build.os !== "windows", perms: { read: true, write: true } },
function renameSyncErrorsWin(): void {
const testDir = Deno.makeTempDirSync();
const oldfile = testDir + "/oldfile";
const olddir = testDir + "/olddir";
const emptydir = testDir + "/empty";
const fulldir = testDir + "/dir";
const file = fulldir + "/file";
writeFileString(oldfile, "Hello");
Deno.mkdirSync(olddir);
Deno.mkdirSync(emptydir);
Deno.mkdirSync(fulldir);
writeFileString(file, "world");
assertThrows(
(): void => {
Deno.renameSync(oldfile, emptydir);
},
Deno.errors.PermissionDenied,
"Access is denied",
);
assertThrows(
(): void => {
Deno.renameSync(olddir, fulldir);
},
Deno.errors.PermissionDenied,
"Access is denied",
);
assertThrows(
(): void => {
Deno.renameSync(olddir, emptydir);
},
Deno.errors.PermissionDenied,
"Access is denied",
);
// should succeed on Windows
Deno.renameSync(olddir, file);
assertDirectory(file);
},
);
|