-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_util.ts
69 lines (64 loc) · 1.66 KB
/
test_util.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
import { join } from "./deps.ts";
import { locateChrome } from "./locate.ts";
const chromeExecutable = await locateChrome();
const chromeDoesNotExist = !chromeExecutable;
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function test(
name: string,
fn: (t: Deno.TestContext) => Promise<void>,
): void {
const allowedEnvVars = Deno.build.os === "windows"
? ["LocalAppData", "ProgramFiles", "ProgramFiles(x86)"]
: [];
Deno.test({
ignore: chromeDoesNotExist,
permissions: {
env: ["CI", ...allowedEnvVars],
read: true,
write: true,
run: [chromeExecutable],
net: true,
},
name,
fn: async (t) => {
try {
await fn(t);
} finally {
// FIXME: Workaround for flaky tests...
if (Deno.env.get("CI")) {
await sleep(5000);
}
}
},
});
}
interface FileServer {
close(): Promise<void>;
}
export function startFileServer(port: number): FileServer {
const listener = Deno.listen({ port });
const serverPromise = (async () => {
for await (const conn of listener) {
const httpConn = Deno.serveHttp(conn);
const event = await httpConn.nextRequest();
if (event == null) {
conn.close();
break;
}
const { request, respondWith } = event;
const url = new URL(request.url);
const body = await Deno.readFile(join("testdata/http", url.pathname));
const resp = new Response(body);
await respondWith(resp);
httpConn.close();
}
})();
return {
async close() {
listener.close();
await serverPromise;
},
};
}