forked from webxdc/webxdc-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.test.ts
103 lines (91 loc) · 2.83 KB
/
location.test.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
100
101
102
103
import path from "path";
import fs from "fs";
import { getLocation, LocationError } from "./location";
test("directory location with index.html", () => {
const dir = path.resolve(__dirname, "fixtures/minimal");
const location = getLocation(dir);
expect(location.type).toEqual("directory");
// help ts
if (location.type !== "directory") {
return;
}
expect(location.path).toEqual(dir);
expect(location.derivedName).toEqual("minimal");
expect(() => location.dispose()).not.toThrow();
});
test("directory location with index.html with trailing slash", () => {
const dir = path.resolve(__dirname, "fixtures/minimal/");
const location = getLocation(dir);
expect(location.type).toEqual("directory");
// help ts
if (location.type !== "directory") {
return;
}
expect(location.path).toEqual(dir);
expect(location.derivedName).toEqual("minimal");
expect(() => location.dispose()).not.toThrow();
});
test("invalid directory location without index.html", () => {
const dir = path.resolve(__dirname, "fixtures/invalid");
try {
getLocation(dir);
} catch (e) {
expect(e).toBeInstanceOf(LocationError);
}
expect.assertions(1);
});
test("xdc file", () => {
const filePath = path.resolve(__dirname, "fixtures/clean.xdc");
const location = getLocation(filePath);
expect(location.type).toEqual("xdc");
// help ts
if (location.type !== "xdc") {
return;
}
expect(location.filePath).toEqual(filePath);
expect(location.derivedName).toEqual("clean");
const stats = fs.statSync(location.path);
expect(stats.isDirectory()).toBeTruthy();
expect(() => location.dispose()).not.toThrow();
expect(fs.existsSync(location.path)).toBeFalsy();
});
test("xdc file with invalid zip", () => {
const filePath = path.resolve(__dirname, "fixtures/invalid.xdc");
try {
getLocation(filePath);
} catch (e) {
expect(e).toBeInstanceOf(LocationError);
}
expect.assertions(1);
});
test("a non-xdc file cannot be handled", () => {
const filePath = path.resolve(__dirname, "fixtures/notXdc");
try {
getLocation(filePath);
} catch (e) {
expect(e).toBeInstanceOf(LocationError);
}
expect.assertions(1);
});
test("directory is never xdc file", () => {
const filePath = path.resolve(__dirname, "fixtures/notXdcDir.xdc");
const location = getLocation(filePath);
expect(location.type).toEqual("directory");
// help ts
if (location.type !== "directory") {
return;
}
expect(location.path).toEqual(filePath);
expect(location.derivedName).toEqual("notXdcDir.xdc");
expect(() => location.dispose()).not.toThrow();
});
test("url", () => {
const location = getLocation("http://example.com");
expect(location.type).toEqual("url");
// help ts
if (location.type !== "url") {
return;
}
expect(location.url).toEqual("http://example.com");
expect(() => location.dispose()).not.toThrow();
});