2021-04-20 01:27:36 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-06-06 12:32:06 -04:00
|
|
|
import { delay, join, readLines, ROOT_PATH, toFileUrl } from "../util.js";
|
2021-06-14 07:48:57 -04:00
|
|
|
import { assert, denoBinary, ManifestTestOptions, runPy } from "./utils.ts";
|
2021-01-27 09:06:18 -05:00
|
|
|
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> {
|
2021-06-07 13:16:27 -04:00
|
|
|
const proc = runPy([
|
|
|
|
"wpt",
|
|
|
|
"serve",
|
|
|
|
"--config",
|
|
|
|
"../../tools/wpt/config.json",
|
|
|
|
], {
|
2021-01-27 09:06:18 -05:00
|
|
|
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;
|
|
|
|
}
|
2021-05-18 11:24:01 -04:00
|
|
|
} catch (_err) {
|
2021-01-27 09:06:18 -05:00
|
|
|
// do nothing if this fails
|
|
|
|
}
|
|
|
|
const passedTime = performance.now() - start;
|
|
|
|
if (passedTime > 15000) {
|
2021-04-18 19:00:13 -04:00
|
|
|
proc.kill(2);
|
|
|
|
await proc.status();
|
|
|
|
proc.close();
|
2021-01-27 09:06:18 -05:00
|
|
|
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(2);
|
|
|
|
await proc.status();
|
|
|
|
proc.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TestResult {
|
|
|
|
cases: TestCaseResult[];
|
2021-06-06 12:08:50 -04:00
|
|
|
harnessStatus: TestHarnessStatus | null;
|
|
|
|
duration: number;
|
2021-01-27 09:06:18 -05:00
|
|
|
status: number;
|
|
|
|
stderr: string;
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:08:50 -04:00
|
|
|
export interface TestHarnessStatus {
|
|
|
|
status: number;
|
|
|
|
message: string | null;
|
|
|
|
stack: string | null;
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:06:18 -05:00
|
|
|
export interface TestCaseResult {
|
|
|
|
name: string;
|
|
|
|
passed: boolean;
|
|
|
|
status: number;
|
|
|
|
message: string | null;
|
|
|
|
stack: string | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function runSingleTest(
|
|
|
|
url: URL,
|
2021-05-18 11:24:01 -04:00
|
|
|
_options: ManifestTestOptions,
|
2021-01-27 09:06:18 -05:00
|
|
|
reporter: (result: TestCaseResult) => void,
|
|
|
|
): Promise<TestResult> {
|
|
|
|
const bundle = await generateBundle(url);
|
|
|
|
const tempFile = await Deno.makeTempFile({
|
|
|
|
prefix: "wpt-bundle-",
|
|
|
|
suffix: ".js",
|
|
|
|
});
|
|
|
|
await Deno.writeTextFile(tempFile, bundle);
|
|
|
|
|
2021-06-06 12:08:50 -04:00
|
|
|
const startTime = new Date().getTime();
|
|
|
|
|
2021-01-27 09:06:18 -05:00
|
|
|
const proc = Deno.run({
|
|
|
|
cmd: [
|
2021-06-14 07:48:57 -04:00
|
|
|
denoBinary(),
|
2021-01-27 09:06:18 -05:00
|
|
|
"run",
|
|
|
|
"-A",
|
2021-05-22 12:08:24 -04:00
|
|
|
"--unstable",
|
2021-01-27 09:06:18 -05:00
|
|
|
"--location",
|
|
|
|
url.toString(),
|
2021-05-09 10:32:30 -04:00
|
|
|
"--cert",
|
2021-06-07 13:16:27 -04:00
|
|
|
join(ROOT_PATH, `./tools/wpt/certs/cacert.pem`),
|
2021-01-27 09:06:18 -05:00
|
|
|
tempFile,
|
|
|
|
"[]",
|
|
|
|
],
|
|
|
|
env: {
|
|
|
|
NO_COLOR: "1",
|
|
|
|
},
|
|
|
|
stdout: "null",
|
|
|
|
stderr: "piped",
|
|
|
|
});
|
|
|
|
|
|
|
|
const cases = [];
|
|
|
|
let stderr = "";
|
|
|
|
|
2021-06-06 12:08:50 -04:00
|
|
|
let harnessStatus = null;
|
|
|
|
|
2021-01-27 09:06:18 -05:00
|
|
|
const lines = readLines(proc.stderr);
|
|
|
|
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);
|
2021-06-06 12:08:50 -04:00
|
|
|
} else if (line.startsWith("#$#$#{")) {
|
|
|
|
harnessStatus = JSON.parse(line.slice(5));
|
2021-01-27 09:06:18 -05:00
|
|
|
} else {
|
|
|
|
stderr += line + "\n";
|
2021-06-21 13:53:52 -04:00
|
|
|
console.error(line);
|
2021-01-27 09:06:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:08:50 -04:00
|
|
|
const duration = new Date().getTime() - startTime;
|
|
|
|
|
2021-01-27 09:06:18 -05:00
|
|
|
const { code } = await proc.status();
|
|
|
|
return {
|
|
|
|
status: code,
|
2021-06-06 12:08:50 -04:00
|
|
|
harnessStatus,
|
|
|
|
duration,
|
2021-01-27 09:06:18 -05:00
|
|
|
cases,
|
|
|
|
stderr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = [];
|
2021-06-06 12:32:06 -04:00
|
|
|
let inlineScriptCount = 0;
|
2021-01-27 09:06:18 -05:00
|
|
|
for (const script of scripts) {
|
|
|
|
const src = script.getAttribute("src");
|
|
|
|
if (src === "/resources/testharnessreport.js") {
|
2021-06-06 12:32:06 -04:00
|
|
|
const url = toFileUrl(
|
|
|
|
join(ROOT_PATH, "./tools/wpt/testharnessreport.js"),
|
2021-01-27 09:06:18 -05:00
|
|
|
);
|
2021-06-06 12:32:06 -04:00
|
|
|
const contents = await Deno.readTextFile(url);
|
|
|
|
scriptContents.push([url.href, contents]);
|
2021-01-27 09:06:18 -05:00
|
|
|
} else if (src) {
|
2021-06-06 12:32:06 -04:00
|
|
|
const url = new URL(src, location);
|
|
|
|
const res = await fetch(url);
|
|
|
|
if (res.ok) {
|
|
|
|
const contents = await res.text();
|
|
|
|
scriptContents.push([url.href, contents]);
|
|
|
|
}
|
2021-01-27 09:06:18 -05:00
|
|
|
} else {
|
2021-06-06 12:32:06 -04:00
|
|
|
const url = new URL(`#${inlineScriptCount}`, location);
|
|
|
|
inlineScriptCount++;
|
|
|
|
scriptContents.push([url.href, script.textContent]);
|
2021-01-27 09:06:18 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-06 12:32:06 -04:00
|
|
|
|
2021-06-11 15:31:53 -04:00
|
|
|
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");
|
2021-01-27 09:06:18 -05:00
|
|
|
}
|