This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveFileSystemNode.test.js
346 lines (306 loc) · 10.3 KB
/
removeFileSystemNode.test.js
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
import { assert } from "@jsenv/assert"
import {
removeFileSystemNode,
ensureEmptyDirectory,
writeFile,
resolveUrl,
writeDirectory,
writeFileSystemNodePermissions,
urlToFileSystemPath,
writeSymbolicLink,
} from "@jsenv/util"
import {
makeBusyFile,
testFilePresence,
testDirectoryPresence,
testSymbolicLinkPresence,
} from "@jsenv/util/test/testHelpers.js"
const isWindows = process.platform === "win32"
const tempDirectoryUrl = resolveUrl("./temp/", import.meta.url)
await ensureEmptyDirectory(tempDirectoryUrl)
// remove nothing
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
try {
await removeFileSystemNode(sourceUrl)
} catch (actual) {
const expected = new Error(`nothing to remove at ${urlToFileSystemPath(sourceUrl)}`)
assert({ actual, expected })
}
}
// remove nothing and allowUseless enabled
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const actual = await removeFileSystemNode(sourceUrl, { allowUseless: true })
const expected = undefined
assert({ actual, expected })
}
// remove opened filed
if (!isWindows) {
// on windows it woul EPERM
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await makeBusyFile(sourceUrl, async () => {
await removeFileSystemNode(sourceUrl)
const actual = await testFilePresence(sourceUrl)
const expected = false
assert({ actual, expected })
})
}
// remove file inside a directory without execute permission
if (!isWindows) {
const sourceUrl = resolveUrl("dir/source", tempDirectoryUrl)
const directoryUrl = resolveUrl("dir", tempDirectoryUrl)
await writeDirectory(directoryUrl)
await writeFile(sourceUrl)
await writeFileSystemNodePermissions(directoryUrl, {
owner: { read: true, write: true, execute: false },
})
try {
await removeFileSystemNode(sourceUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`EACCES: permission denied, lstat '${urlToFileSystemPath(sourceUrl)}'`,
)
expected.errno = actual.errno
expected.code = "EACCES"
expected.syscall = "lstat"
expected.path = urlToFileSystemPath(sourceUrl)
assert({ actual, expected })
} finally {
await writeFileSystemNodePermissions(directoryUrl, {
owner: { read: true, write: true, execute: true },
})
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// remove file without permission
if (!isWindows) {
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeFile(sourceUrl, "noperm")
await writeFileSystemNodePermissions(sourceUrl, {
owner: { read: false, write: false, execute: false },
})
await removeFileSystemNode(sourceUrl)
const actual = await testFilePresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove file
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeFile(sourceUrl, "normal")
await removeFileSystemNode(sourceUrl)
const actual = await testFilePresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove destination with trailing slash being a file
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const sourceUrlWithTrailingSlash = `${sourceUrl}/`
await writeFile(sourceUrl, "trailing")
await removeFileSystemNode(sourceUrlWithTrailingSlash)
const actual = await testFilePresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove directory
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await removeFileSystemNode(sourceUrl)
const actual = await testFilePresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove directory without permission
if (!isWindows) {
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await writeFileSystemNodePermissions(sourceUrl, {
other: { read: false, write: false, execute: false },
})
await removeFileSystemNode(sourceUrl)
const actual = await testFilePresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove directory with a file
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("source/file", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await writeFile(fileUrl)
try {
await removeFileSystemNode(sourceUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`ENOTEMPTY: directory not empty, rmdir '${urlToFileSystemPath(sourceUrl)}'`,
)
expected.errno = actual.errno
expected.code = "ENOTEMPTY"
expected.syscall = "rmdir"
expected.path = urlToFileSystemPath(sourceUrl)
assert({ actual, expected })
} finally {
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// remove directory with a file and recursive enabled
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("source/file", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await writeFile(fileUrl)
await removeFileSystemNode(sourceUrl, { recursive: true })
const actual = await testDirectoryPresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove directory with content without permission and recursive enabled
if (!isWindows) {
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("source/file", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await writeFile(fileUrl)
await writeFileSystemNodePermissions(sourceUrl, {
owner: { read: true, write: true, execute: false },
})
try {
await removeFileSystemNode(sourceUrl, { recursive: true })
throw new Error("should throw")
} catch (actual) {
const expected = new Error(`EACCES: permission denied, lstat '${urlToFileSystemPath(fileUrl)}'`)
expected.errno = -13
expected.code = "EACCES"
expected.syscall = "lstat"
expected.path = urlToFileSystemPath(fileUrl)
assert({ actual, expected })
} finally {
await writeFileSystemNodePermissions(sourceUrl, {
owner: { read: true, write: true, execute: true },
})
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// remove directory with a busy file
if (!isWindows) {
// on windows it would EPERM
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("source/file", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await makeBusyFile(fileUrl, async () => {
await removeFileSystemNode(sourceUrl, { recursive: true })
const actual = await testDirectoryPresence(sourceUrl)
const expected = false
assert({ actual, expected })
})
}
// remove directory with a file without write permission
if (!isWindows) {
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("source/file", tempDirectoryUrl)
await writeFile(fileUrl)
await writeFileSystemNodePermissions(fileUrl, {
owner: { read: false, write: false, execute: false },
})
await removeFileSystemNode(sourceUrl, { recursive: true })
const actual = await testDirectoryPresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove directory with file nested and recursive enabled
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileAUrl = resolveUrl("source/dir/file", tempDirectoryUrl)
const fileBUrl = resolveUrl("source/dir2/file", tempDirectoryUrl)
await writeFile(fileAUrl, "contentA")
await writeFile(fileBUrl, "contentB")
await removeFileSystemNode(sourceUrl, { recursive: true })
const actual = await testDirectoryPresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove directory with a link to nothing
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const linkUrl = resolveUrl("source/link", tempDirectoryUrl)
await writeSymbolicLink(linkUrl, "./whatever")
await removeFileSystemNode(sourceUrl, { recursive: true })
const actual = await testDirectoryPresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove link to nothing
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeSymbolicLink(sourceUrl, "./whatever")
await removeFileSystemNode(sourceUrl)
const actual = await testSymbolicLinkPresence(sourceUrl)
const expected = false
assert({ actual, expected })
}
// remove link to file
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("file", tempDirectoryUrl)
await writeFile(fileUrl)
await writeSymbolicLink(sourceUrl, "./file")
await removeFileSystemNode(sourceUrl)
const actual = {
linkPresence: await testSymbolicLinkPresence(sourceUrl),
filePresence: await testFilePresence(fileUrl),
}
const expected = {
linkPresence: false,
filePresence: true,
}
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
// remove link to directory
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const directoryUrl = resolveUrl("dir", tempDirectoryUrl)
await writeDirectory(directoryUrl)
await writeSymbolicLink(sourceUrl, "./dir")
await removeFileSystemNode(sourceUrl)
const actual = {
linkPresence: await testSymbolicLinkPresence(sourceUrl),
directoryPresence: await testDirectoryPresence(directoryUrl),
}
const expected = {
linkPresence: false,
directoryPresence: true,
}
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
// remove directory without execute permission and link inside
if (!isWindows) {
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const linkUrl = resolveUrl("source/link", tempDirectoryUrl)
await writeDirectory(sourceUrl)
await writeSymbolicLink(linkUrl, "whatever")
await writeFileSystemNodePermissions(sourceUrl, {
owner: { read: true, write: true, execute: false },
})
try {
await removeFileSystemNode(sourceUrl, { recursive: true })
throw new Error("should throw")
} catch (actual) {
const expected = new Error(`EACCES: permission denied, lstat '${urlToFileSystemPath(linkUrl)}'`)
expected.errno = -13
expected.code = "EACCES"
expected.syscall = "lstat"
expected.path = urlToFileSystemPath(linkUrl)
assert({ actual, expected })
} finally {
await writeFileSystemNodePermissions(sourceUrl, {
owner: { read: true, write: true, execute: true },
})
await ensureEmptyDirectory(tempDirectoryUrl)
}
}