-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
start-wpt-server.js
74 lines (63 loc) · 2.27 KB
/
start-wpt-server.js
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
"use strict";
/* eslint-disable no-console, global-require */
const path = require("path");
const dns = require("dns");
const childProcess = require("child_process");
const q = require("q");
const { inBrowserContext } = require("../util.js");
const requestHead = require("request-promise-native").head;
const dnsLookup = q.denodeify(dns.lookup);
const wptDir = path.resolve(__dirname, "tests");
const configPaths = {
default: path.resolve(__dirname, "wpt-config.json"),
toUpstream: path.resolve(__dirname, "tuwpt-config.json")
};
const configs = {
default: require(configPaths.default),
toUpstream: require(configPaths.toUpstream)
};
module.exports = ({ toUpstream = false } = {}) => {
if (inBrowserContext()) {
return Promise.resolve();
}
const configType = toUpstream ? "toUpstream" : "default";
const configPath = configPaths[configType];
const config = configs[configType];
const urlPrefix = `http://${config.host}:${config.ports.http[0]}/`;
return dnsLookup("web-platform.test").then(
() => {
const configArg = path.relative(path.resolve(wptDir), configPath);
const args = ["./wpt.py", "serve", "--config", configArg];
const python = childProcess.spawn("python", args, {
cwd: wptDir,
stdio: "inherit"
});
return new Promise((resolve, reject) => {
python.on("error", e => {
reject(new Error("Error starting python server process:", e.message));
});
resolve(pollForServer(urlPrefix));
process.on("exit", () => {
// Python doesn't register a default handler for SIGTERM and it doesn't run __exit__() methods of context
// managers when it gets that signal. Using SIGINT avoids this problem.
python.kill("SIGINT");
});
});
},
() => {
throw new Error("Host entries not present for web platform tests. See " +
"https://github.com/w3c/web-platform-tests#running-the-tests");
}
);
};
function pollForServer(url) {
return requestHead(url)
.then(() => {
console.log(`WPT server at ${url} is up!`);
return url;
})
.catch(err => {
console.log(`WPT server at ${url} is not up yet (${err.message}); trying again`);
return q.delay(500).then(() => pollForServer(url));
});
}