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 pathwriteDirectory.test.js
102 lines (91 loc) · 2.94 KB
/
writeDirectory.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
import { assert } from "@jsenv/assert"
import {
writeDirectory,
ensureEmptyDirectory,
writeFileSystemNodePermissions,
urlToFileSystemPath,
writeFile,
writeSymbolicLink,
resolveUrl,
} from "@jsenv/util"
const isWindows = process.platform === "win32"
const tempDirectoryUrl = resolveUrl("./temp/", import.meta.url)
await ensureEmptyDirectory(tempDirectoryUrl)
// destination inside parent directory without write permission
if (!isWindows) {
const parentDirectoryUrl = resolveUrl("dir/", tempDirectoryUrl)
const destinationUrl = resolveUrl("dest", parentDirectoryUrl)
await writeDirectory(parentDirectoryUrl)
await writeFileSystemNodePermissions(parentDirectoryUrl, {
owner: { read: true, write: false },
})
try {
await writeDirectory(destinationUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`EACCES: permission denied, lstat '${urlToFileSystemPath(destinationUrl)}'`,
)
expected.errno = -13
expected.code = "EACCES"
expected.syscall = "lstat"
expected.path = urlToFileSystemPath(destinationUrl)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// destination is a directory
{
const destinationUrl = resolveUrl("dest/", tempDirectoryUrl)
await writeDirectory(destinationUrl)
try {
await writeDirectory(destinationUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(`directory already exists at ${urlToFileSystemPath(destinationUrl)}`)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// destination is a file
{
const destinationUrl = resolveUrl("dest", tempDirectoryUrl)
await writeFile(destinationUrl)
const directoryUrl = resolveUrl("dest/", tempDirectoryUrl)
try {
await writeDirectory(destinationUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`cannot write directory at ${urlToFileSystemPath(directoryUrl)} because there is a file`,
)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// destination is a link to nothing
{
const destinationUrl = resolveUrl("dest", tempDirectoryUrl)
await writeSymbolicLink(destinationUrl, "./whatever")
const directoryUrl = resolveUrl("dest/", tempDirectoryUrl)
try {
await writeDirectory(destinationUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`cannot write directory at ${urlToFileSystemPath(
directoryUrl,
)} because there is a symbolic-link`,
)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// destination is a directory and allowUseless is true
{
const destinationUrl = resolveUrl("dest/", tempDirectoryUrl)
await writeDirectory(destinationUrl)
const actual = await writeDirectory(destinationUrl, { allowUseless: true })
const expected = undefined
assert({ actual, expected })
}