From d7c2d098af74b6aa0d760b5ac27a526055033692 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Mon, 18 Nov 2019 14:18:03 +0300 Subject: [PATCH] [Debugger, JS] Add configuring debuggable browser --- .../kotlin-test-js-runner/karma-debug.js | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/libraries/tools/kotlin-test-js-runner/karma-debug.js b/libraries/tools/kotlin-test-js-runner/karma-debug.js index b6991e6ee34..74b87ee2ca7 100644 --- a/libraries/tools/kotlin-test-js-runner/karma-debug.js +++ b/libraries/tools/kotlin-test-js-runner/karma-debug.js @@ -3,12 +3,18 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +'use strict'; + const karma = require('karma'); const cfg = karma.config; const karmaConfig = cfg.parseConfig(process.argv[2]); +karmaConfig.singleRun = false; + +configureBrowsers(karmaConfig); + const Server = karma.Server; const server = new Server(karmaConfig, function (exitCode) { console.log('Karma has exited with ' + exitCode); @@ -26,4 +32,64 @@ server.on('browsers_ready', function () { }, 1000) }); -server.start(); \ No newline at end of file +server.start(); + +function configureBrowsers(config) { + let newBrowsers = config.browsers; + if (!Array.isArray(newBrowsers)) { + newBrowsers = []; + } + + let browser = newBrowsers.find(browserName => isDebuggableBrowser(browserName, config)); + + if (!browser) { + console.warn( + 'Unable to find debuggable browser: Only Google Chrome with 9222 remote debugging port supported\n', + 'Fallback on Chrome Headless' + ); + browser = 'ChromeHeadless' + } + + config.browsers = [browser]; +} + +const REMOTE_DEBUGGING_PORT = '--remote-debugging-port'; + +function isDebuggableBrowser(browserName, config) { + if ([ + 'ChromeHeadless', + 'ChromeCanaryHeadless', + 'ChromiumHeadless' + ].includes(browserName)) { + return true + } + + const customLaunchers = config.customLaunchers; + if (!customLaunchers) { + return false; + } + + let customLauncher = customLaunchers[browserName]; + if (!customLauncher) { + return false; + } + + const flags = customLauncher.flags; + if (!Array.isArray(flags)) { + return false; + } + + const prefix = REMOTE_DEBUGGING_PORT + '='; + const value = flags.find(flag => typeof flag === 'string' && flag.indexOf(prefix) === 0); + if (value == null) { + return false; + } + + const port = parseInt(value.substring(prefix.length), 10); + if (isNaN(port) || port !== 9222) { + console.warn(`Debugger expect 9222 port, but ${port} found`); + return false; + } + + return true; +} \ No newline at end of file