-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
fileAssert.ts
99 lines (86 loc) · 2.44 KB
/
fileAssert.ts
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
import { exists, statOrNull } from "builder-util/out/fs"
import { promises as fs } from "fs"
import * as path from "path"
// http://joel-costigliola.github.io/assertj/
export function assertThat(actual: any): Assertions {
return new Assertions(actual)
}
class Assertions {
constructor(private actual: any) {
}
containsAll<T>(expected: Iterable<T>) {
expect(this.actual.slice().sort()).toEqual(Array.from(expected).slice().sort())
}
isAbsolute() {
if (!path.isAbsolute(this.actual)) {
throw new Error(`Path ${this.actual} is not absolute`)
}
}
async isFile() {
const info = await statOrNull(this.actual)
if (info == null) {
throw new Error(`Path ${this.actual} doesn't exist`)
}
if (!info.isFile()) {
throw new Error(`Path ${this.actual} is not a file`)
}
}
async isSymbolicLink() {
const info = await fs.lstat(this.actual)
if (!info.isSymbolicLink()) {
throw new Error(`Path ${this.actual} is not a symlink`)
}
}
async isDirectory() {
const file = this.actual
const info = await statOrNull(file)
if (info == null) {
throw new Error(`Path ${file} doesn't exist`)
}
if (!info.isDirectory()) {
throw new Error(`Path ${file} is not a directory`)
}
}
async doesNotExist() {
if (await exists(this.actual)) {
throw new Error(`Path ${this.actual} must not exist`)
}
}
async throws(customErrorAssert?: (error: Error) => void) {
let actualError: Error | null = null
let result: any
try {
result = await this.actual
}
catch (e) {
actualError = e
}
let m
if (actualError == null) {
m = result
}
else {
m = (actualError as NodeJS.ErrnoException).code || actualError.message
if (m.includes("HttpError: ") && m.indexOf("\n") > 0) {
m = m.substring(0, m.indexOf("\n"))
}
if (m.startsWith("Cannot find specified resource")) {
m = m.substring(0, m.indexOf(","))
}
m = m.replace(/\((C:)?(\/|\\)[^(]+(\/|\\)([^(\/\\]+)\)/g, `(<path>/$4)`)
m = m.replace(/"(C:)?(\/|\\)[^"]+(\/|\\)([^"\/\\]+)"/g, `"<path>/$4"`)
m = m.replace(/'(C:)?(\/|\\)[^']+(\/|\\)([^'\/\\]+)'/g, `'<path>/$4'`)
}
try {
if (customErrorAssert == null) {
expect(m).toMatchSnapshot()
}
else {
customErrorAssert(actualError!!)
}
}
catch (matchError) {
throw new Error(matchError + " " + actualError)
}
}
}