deno.land / x / deno@v1.28.2 / tools / wpt / runner.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { delay, join, ROOT_PATH, TextLineStream, toFileUrl } from "../util.js";import { assert, denoBinary, ManifestTestOptions, runPy } from "./utils.ts";import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.3-alpha2/deno-dom-wasm.ts";
export async function runWithTestUtil<T>( verbose: boolean, f: () => Promise<T>,): Promise<T> { const proc = runPy([ "wpt", "serve", "--config", "../../tools/wpt/config.json", ], { stdout: verbose ? "inherit" : "piped", stderr: verbose ? "inherit" : "piped", });
const start = performance.now(); while (true) { await delay(1000); try { const req = await fetch("http://localhost:8000/"); await req.body?.cancel(); if (req.status == 200) { break; } } catch (_err) { // do nothing if this fails } const passedTime = performance.now() - start; if (passedTime > 15000) { proc.kill("SIGINT"); await proc.status; throw new Error("Timed out while trying to start wpt test util."); } }
if (verbose) console.log(`Started wpt test util.`);
try { return await f(); } finally { if (verbose) console.log("Killing wpt test util."); proc.kill("SIGINT"); await proc.status; }}
export interface TestResult { cases: TestCaseResult[]; harnessStatus: TestHarnessStatus | null; duration: number; status: number; stderr: string;}
export interface TestHarnessStatus { status: number; message: string | null; stack: string | null;}
export interface TestCaseResult { name: string; passed: boolean; status: number; message: string | null; stack: string | null;}
export async function runSingleTest( url: URL, _options: ManifestTestOptions, reporter: (result: TestCaseResult) => void, inspectBrk: boolean,): Promise<TestResult> { const bundle = await generateBundle(url); const tempFile = await Deno.makeTempFile({ prefix: "wpt-bundle-", suffix: ".js", });
try { await Deno.writeTextFile(tempFile, bundle);
const startTime = new Date().getTime();
const args = [ "run", "-A", "--unstable", ];
if (inspectBrk) { args.push("--inspect-brk"); }
args.push( "--enable-testing-features-do-not-use", "--location", url.toString(), "--cert", join(ROOT_PATH, `./tools/wpt/certs/cacert.pem`), tempFile, "[]", );
const proc = Deno.spawnChild(denoBinary(), { args, env: { NO_COLOR: "1", }, stdout: "null", stderr: "piped", });
const cases = []; let stderr = "";
let harnessStatus = null;
const lines = proc.stderr.pipeThrough(new TextDecoderStream()).pipeThrough( new TextLineStream(), ); for await (const line of lines) { if (line.startsWith("{")) { const data = JSON.parse(line); const result = { ...data, passed: data.status == 0 }; cases.push(result); reporter(result); } else if (line.startsWith("#$#$#{")) { harnessStatus = JSON.parse(line.slice(5)); } else { stderr += line + "\n"; console.error(line); } }
const duration = new Date().getTime() - startTime;
const { code } = await proc.status; return { status: code, harnessStatus, duration, cases, stderr, }; } finally { await Deno.remove(tempFile); }}
async function generateBundle(location: URL): Promise<string> { const res = await fetch(location); const body = await res.text(); const doc = new DOMParser().parseFromString(body, "text/html"); assert(doc, "document should have been parsed"); const scripts = doc.getElementsByTagName("script"); const scriptContents = []; let inlineScriptCount = 0; for (const script of scripts) { const src = script.getAttribute("src"); if (src === "/resources/testharnessreport.js") { const url = toFileUrl( join(ROOT_PATH, "./tools/wpt/testharnessreport.js"), ); const contents = await Deno.readTextFile(url); scriptContents.push([url.href, contents]); } else if (src) { const url = new URL(src, location); const res = await fetch(url); if (res.ok) { const contents = await res.text(); scriptContents.push([url.href, contents]); } } else { const url = new URL(`#${inlineScriptCount}`, location); inlineScriptCount++; scriptContents.push([url.href, script.textContent]); } }
return scriptContents.map(([url, contents]) => `(function() { const [_,err] = Deno.core.evalContext(${JSON.stringify(contents)}, ${ JSON.stringify(url) }); if (err !== null) { throw err?.thrown; }})();`).join("\n");}
deno

Version Info

Tagged at
a year ago