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
/
assertDirectoryPresence.test.js
106 lines (94 loc) · 2.89 KB
/
assertDirectoryPresence.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
import { assert } from "@jsenv/assert"
import {
resolveUrl,
ensureEmptyDirectory,
assertDirectoryPresence,
urlToFileSystemPath,
writeFile,
writeDirectory,
writeSymbolicLink,
writeFileSystemNodePermissions,
} from "@jsenv/util"
const tempDirectoryUrl = resolveUrl("./temp/", import.meta.url)
await ensureEmptyDirectory(tempDirectoryUrl)
await writeFileSystemNodePermissions(tempDirectoryUrl, {
owner: { read: true, write: true, execute: true },
group: { read: true, write: true, execute: true },
others: { read: true, write: true, execute: true },
})
// on nothing
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
try {
await assertDirectoryPresence(sourceUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(`directory not found at ${urlToFileSystemPath(sourceUrl)}`)
assert({ actual, expected })
}
}
// on file
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeFile(sourceUrl)
try {
await assertDirectoryPresence(sourceUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`directory expected at ${urlToFileSystemPath(sourceUrl)} and found file instead`,
)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// on directory
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeDirectory(sourceUrl)
const actual = await assertDirectoryPresence(sourceUrl)
const expected = undefined
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
// on symlink to file
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const fileUrl = resolveUrl("file", tempDirectoryUrl)
await writeFile(fileUrl)
await writeSymbolicLink(sourceUrl, "./file")
try {
await assertDirectoryPresence(sourceUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(
`directory expected at ${urlToFileSystemPath(sourceUrl)} and found file instead`,
)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// on symlink to nothing
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
await writeSymbolicLink(sourceUrl, "./file")
try {
await assertDirectoryPresence(sourceUrl)
throw new Error("should throw")
} catch (actual) {
const expected = new Error(`directory not found at ${urlToFileSystemPath(sourceUrl)}`)
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}
}
// on symlink to directory
{
const sourceUrl = resolveUrl("source", tempDirectoryUrl)
const directoryUrl = resolveUrl("dir", tempDirectoryUrl)
await writeDirectory(directoryUrl)
await writeSymbolicLink(sourceUrl, "./dir")
const actual = await assertDirectoryPresence(sourceUrl)
const expected = undefined
assert({ actual, expected })
await ensureEmptyDirectory(tempDirectoryUrl)
}