Skip to content

Commit

Permalink
Support running just the server component
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Oct 4, 2019
1 parent a46ee1e commit 2ecd5c5
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 77 deletions.
26 changes: 26 additions & 0 deletions bin/wpt-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node
"use strict";

const wptServer = require("../lib/wpt-server");

const argv = require("yargs")
.command("$0 <path>", "Serves the web platform tests at the given path, e.g. wpt/dom/nodes/")
.option("root-url", {
description: "The relative URL path for the tests at <path>, e.g. dom/nodes/",
alias: "u",
type: "string",
requiresArg: true
})
.option("port", {
description: "The port number on which to run the server, e.g. 8000",
alias: "p",
type: "number",
requiresArg: true
})
.argv;

const testsPath = argv.path;
const rootURL = argv["root-url"];
const port = argv.port || 0;

wptServer(testsPath, { rootURL, port });
80 changes: 4 additions & 76 deletions lib/wpt-runner.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
"use strict";

const http = require("http");
const path = require("path");
const fs = require("fs");
const { URL } = require("url");
const st = require("st");
const { JSDOM, VirtualConsole } = require("jsdom");
const recursiveReaddirCb = require("recursive-readdir");
const consoleReporter = require("./console-reporter.js");
const { SourceFile } = require("./internal/sourcefile.js");
const { AnyHtmlHandler, WindowHandler } = require("./internal/serve.js");

const testharnessPath = path.resolve(__dirname, "../testharness/testharness.js");
const testdriverDummy = fs.readFileSync(path.resolve(__dirname, "./testdriver-dummy.js"));
const wptServer = require("./wpt-server.js");

module.exports = (testsPath, {
rootURL = "/",
Expand All @@ -27,9 +20,11 @@ module.exports = (testsPath, {
rootURL += "/";
}

const server = setupServer(testsPath, rootURL);
const server = wptServer(testsPath, { rootURL, verbose: false });
const urlPrefix = `http://127.0.0.1:${server.address().port}${rootURL}`;

server.unref();

return readTestPaths(testsPath).then(testPaths => {
let totalFailures = 0;
return doTest(0).then(() => totalFailures);
Expand Down Expand Up @@ -58,73 +53,6 @@ module.exports = (testsPath, {
});
};

function setupServer(testsPath, rootURL) {
const staticFileServer = st({ path: testsPath, url: rootURL, passthrough: true });

const routes = [
[".window.html", new WindowHandler(testsPath, rootURL)],
[".any.html", new AnyHtmlHandler(testsPath, rootURL)]
];

const server = http.createServer((req, res) => {
staticFileServer(req, res, () => {
const { pathname } = new URL(req.url, `http://${req.headers.host}`);

for (const [pathNameSuffix, handler] of routes) {
if (pathname.endsWith(pathNameSuffix)) {
handler.handleRequest(req, res);
return;
}
}

switch (pathname) {
case "/resources/testharness.js": {
fs.createReadStream(testharnessPath).pipe(res);
break;
}

case "/service-workers/service-worker/resources/test-helpers.sub.js": {
res.end("window.service_worker_test = () => {};");
break;
}

case "/resources/testharnessreport.js": {
res.end("window.__setupJSDOMReporter();");
break;
}

case "/streams/resources/test-initializer.js": {
res.end("window.worker_test = () => {};");
break;
}

case "/resources/testharness.css": {
res.end("");
break;
}

case "/resources/testdriver.js": {
res.end(testdriverDummy);
break;
}

case "/resources/testdriver-vendor.js": {
res.end("");
break;
}

default: {
throw new Error(`Unexpected URL: ${req.url}`);
}
}
});
}).listen();

server.unref();

return server;
}

function runTest(url, setup, reporter) {
return new Promise(resolve => {
const virtualConsole = new VirtualConsole()
Expand Down
105 changes: 105 additions & 0 deletions lib/wpt-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"use strict";

const http = require("http");
const path = require("path");
const fs = require("fs");
const { URL } = require("url");
const st = require("st");
const { AnyHtmlHandler, WindowHandler } = require("./internal/serve.js");

const testharnessPath = path.resolve(__dirname, "../testharness/testharness.js");
const testdriverDummy = fs.readFileSync(path.resolve(__dirname, "./testdriver-dummy.js"));

module.exports = (testsPath, {
rootURL = testsPath,
verbose = true,
port = 0
} = {}) => {
if (!rootURL.startsWith("/")) {
rootURL = "/" + rootURL;
}
if (!rootURL.endsWith("/")) {
rootURL += "/";
}

const server = setupServer(testsPath, rootURL, port);
const urlPrefix = `http://127.0.0.1:${server.address().port}${rootURL}`;

if (verbose) {
console.log(`Server running at ${urlPrefix}`);
}

return server;
};


function setupServer(testsPath, rootURL, port) {
const staticFileServer = st({ path: testsPath, url: rootURL, passthrough: true });

const routes = [
[".window.html", new WindowHandler(testsPath, rootURL)],
[".any.html", new AnyHtmlHandler(testsPath, rootURL)]
];

const server = http.createServer((req, res) => {
const { pathname } = new URL(req.url, `http://${req.headers.host}`);

for (const [pathNameSuffix, handler] of routes) {
if (pathname.endsWith(pathNameSuffix)) {
handler.handleRequest(req, res);
return;
}
}

staticFileServer(req, res, () => {
switch (pathname) {
case "/resources/testharness.js": {
fs.createReadStream(testharnessPath).pipe(res);
break;
}

case "/service-workers/service-worker/resources/test-helpers.sub.js": {
res.end("window.service_worker_test = () => {};");
break;
}

case "/resources/testharnessreport.js": {
res.end("if ('__setupJSDOMReporter' in window) window.__setupJSDOMReporter();");
break;
}

case "/streams/resources/test-initializer.js": {
res.end("window.worker_test = () => {};");
break;
}

case "/resources/testharness.css": {
res.end("");
break;
}

case "/resources/testdriver.js": {
res.end(testdriverDummy);
break;
}

case "/resources/testdriver-vendor.js": {
res.end("");
break;
}

case "/favicon.ico": {
res.writeHead(404);
res.end("");
break;
}

default: {
throw new Error(`Unexpected URL: ${req.url}`);
}
}
});
}).listen(port);

return server;
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"license": "WTFPL",
"repository": "domenic/wpt-runner",
"main": "lib/wpt-runner.js",
"bin": "bin/wpt-runner.js",
"bin": {
"wpt-runner": "bin/wpt-runner.js",
"wpt-server": "bin/wpt-server.js"
},
"files": [
"lib/",
"bin/",
Expand Down

0 comments on commit 2ecd5c5

Please sign in to comment.