deno.land / x / esm@v135_2 / test / bootstrap.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-write --allow-net
async function startServer(onStart: () => Promise<void>, verbose: boolean) { const { code, success } = await run("go", "build", "-o", "esmd", "main.go"); if (!success) { Deno.exit(code); } const p = new Deno.Command("./esmd", { stdout: verbose ? "inherit" : "null", stderr: "inherit", }).spawn(); addEventListener("unload", () => { console.log("%cClosing esm.sh server...", "color: grey"); p.kill("SIGINT"); }); while (true) { try { await new Promise((resolve) => setTimeout(resolve, 100)); const res = await fetch("http://localhost:8080/status.json"); const ret = await res.json(); if (ret.version) { console.log("esm.sh server started."); onStart(); break; } } catch (_) { // ignore } } await p.status;}
async function runTest(name: string, retry?: boolean): Promise<number> { const execBegin = Date.now(); const args = [ "test", "-A", "--unstable-fs", "--check", "--no-lock", "--reload=http://localhost:8080", "--location=http://0.0.0.0/", ]; const dir = `test/${name}/`; if (await existsFile(dir + "deno.json")) { args.push("--config", dir + "deno.json"); } args.push(dir);
console.log(`\n[test ${name}]`);
const { code, success } = await run(Deno.execPath(), ...args); if (!success) { if (!retry) { console.log("something wrong, retry..."); await new Promise((resolve) => setTimeout(resolve, 500)); return await runTest(name, true); } else { Deno.exit(code); } } return Date.now() - execBegin;}
function run(name: string, ...args: string[]) { const p = new Deno.Command(name, { args, stdout: "inherit", stderr: "inherit", }).spawn(); return p.status;}
async function existsFile(path: string): Promise<boolean> { try { const fi = await Deno.lstat(path); return fi.isFile; } catch (err) { if (err instanceof Deno.errors.NotFound) { return false; } throw err; }}
if (import.meta.main) { const rootDir = new URL(import.meta.url).pathname.split("/").slice(0, -2) .join("/"); Deno.chdir(rootDir); const tests = Deno.args.filter((arg) => !arg.startsWith("-")); const clean = Deno.args.includes("--clean"); if (clean) { try { console.log("Cleaning up..."); await Promise.all([ Deno.remove("./.esmd/log", { recursive: true }), Deno.remove("./.esmd/storage", { recursive: true }), Deno.remove("./.esmd/esm.db"), ]); } catch (_) { // ignore } } console.log("Starting esm.sh server..."); startServer(async () => { let timeUsed = 0; if (tests.length > 0) { for (const testDir of tests) { timeUsed += await runTest(testDir, true); } } else { for await (const entry of Deno.readDir("./test")) { if (entry.isDirectory && !entry.name.startsWith("_")) { timeUsed += await runTest(entry.name); } } } timeUsed = Math.ceil(timeUsed / 1000); console.log( `Done! Total time spent: %c${Math.floor(timeUsed / 60)}m${ timeUsed % 60 }s`, "color: blue", ); Deno.exit(0); }, tests.length > 0);}
esm

Version Info

Tagged at
2 months ago