Enable test_suite to take a configurable server type.
As part of the work to enable Puppeteer component tests, we need to
configure the test suite to run either the hosted mode server or the
component docs server. This CL updates it to take a flag, and makes some
updates to the component docs server, which now has to run either
directly or in the out/Default/gen directory depending on how it is run.
I suspect I'll make a follow up CL to always run the component server in
out/Default/gen, but for now enabling it to detect its context is the
quickest way to unblock running it in tests. The next CL will add a
component test suite that can run a basic test against the component doc
server, but I have manually verified locally that I can run tests
against that server.
Bug: 1153281
Change-Id: I55bda4edd0a983d03bfecff0446b0f3e3a008b52
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2562707
Reviewed-by: Paul Lewis <aerotwist@chromium.org>
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
diff --git a/scripts/component_server/BUILD.gn b/scripts/component_server/BUILD.gn
new file mode 100644
index 0000000..ad9b5e0
--- /dev/null
+++ b/scripts/component_server/BUILD.gn
@@ -0,0 +1,9 @@
+# Copyright 2020 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("../build/ninja/copy.gni")
+
+copy_to_gen("component_server") {
+ sources = [ "server.js" ]
+}
diff --git a/scripts/component_server/server.js b/scripts/component_server/server.js
index ae46054..092ab98 100644
--- a/scripts/component_server/server.js
+++ b/scripts/component_server/server.js
@@ -11,7 +11,17 @@
const serverPort = parseInt(process.env.PORT, 10) || 8090;
const target = argv.target || 'Default';
-const devtoolsFrontendFolder = path.resolve(path.join(__dirname, '..', '..', 'out', target, 'gen', 'front_end'));
+
+/**
+ * When you run npm run components-server we run the script as is from scripts/,
+ * but when this server is run as part of a test suite it's run from
+ * out/Default/gen/scripts, so we have to do a bit of path mangling to figure
+ * out where we are.
+ */
+const isRunningInGen = __dirname.includes(path.join(path.sep, 'gen', path.sep, 'scripts'));
+const pathToOutDirectory = isRunningInGen ? path.resolve(path.join(__dirname), '..', '..', '..', '..') :
+ path.resolve(path.join(__dirname, '..', '..', 'out'));
+const devtoolsFrontendFolder = path.resolve(path.join(pathToOutDirectory, target, 'gen', 'front_end'));
if (!fs.existsSync(devtoolsFrontendFolder)) {
console.error(`ERROR: Generated front_end folder (${devtoolsFrontendFolder}) does not exist.`);
@@ -21,8 +31,21 @@
process.exit(1);
}
-http.createServer(requestHandler).listen(serverPort);
-console.log(`Started components server at http://localhost:${serverPort}\n`);
+const server = http.createServer(requestHandler);
+server.listen(serverPort);
+server.once('listening', () => {
+ if (process.send) {
+ process.send(serverPort);
+ }
+ console.log(`Started components server at http://localhost:${serverPort}\n`);
+});
+
+server.once('error', error => {
+ if (process.send) {
+ process.send('ERROR');
+ }
+ throw error;
+});
function createComponentIndexFile(componentPath, componentExamples) {
const componentName = componentPath.replace('/', '').replace(/_/g, ' ');