From 00215b02449ee703ad46982ceac7397e0f962c4e Mon Sep 17 00:00:00 2001 From: dizzy Date: Tue, 6 Jun 2023 04:30:14 -0700 Subject: [PATCH] allow running JS tests for a single plugin in tests:run-js (#20816) --- plugins/TestRunner/Commands/TestsRunJS.php | 8 ++++++++ tests/javascript/testrunnerNode.js | 12 ++++++++++-- tests/javascript/testrunnerPhantom.js | 20 ++++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/plugins/TestRunner/Commands/TestsRunJS.php b/plugins/TestRunner/Commands/TestsRunJS.php index 0d9f6c9fe4f..195ad6bca7f 100644 --- a/plugins/TestRunner/Commands/TestsRunJS.php +++ b/plugins/TestRunner/Commands/TestsRunJS.php @@ -16,6 +16,7 @@ protected function configure() $this->setName('tests:run-js'); $this->setDescription('Run javascript tests'); $this->addRequiredValueOption('matomo-url', null, 'Custom matomo url. Defaults to http://localhost'); + $this->addOptionalValueOption('plugin', null, 'The plugin to run tests for. If not supplied, all tests are run.'); } protected function doExecute(): int @@ -23,11 +24,15 @@ protected function doExecute(): int $input = $this->getInput(); $output = $this->getOutput(); $matomoUrl = $input->getOption('matomo-url') ?? 'http://localhost'; + $plugin = $input->getOption('plugin'); $screenshotTestingDir = PIWIK_INCLUDE_PATH . "/tests/lib/screenshot-testing"; $javascriptTestingDir = PIWIK_INCLUDE_PATH . "/tests/javascript"; $cmdNode = "cd '$javascriptTestingDir' && NODE_PATH='$screenshotTestingDir/node_modules' node testrunnerNode.js '$matomoUrl/tests/javascript/'"; + if (!empty($plugin)) { + $cmdNode .= ' --plugin=' . escapeshellarg($plugin); + } $output->writeln('Executing command: ' . $cmdNode . ''); $output->writeln(''); @@ -35,6 +40,9 @@ protected function doExecute(): int passthru($cmdNode, $returnCodeNode); $cmdPhantom = "phantomjs $javascriptTestingDir/testrunnerPhantom.js '$matomoUrl/tests/javascript/'"; + if (!empty($plugin)) { + $cmdPhantom .= ' --plugin=' . escapeshellarg($plugin); + } $output->writeln(''); $output->writeln(''); diff --git a/tests/javascript/testrunnerNode.js b/tests/javascript/testrunnerNode.js index 218039e6eb9..d65d89a8201 100644 --- a/tests/javascript/testrunnerNode.js +++ b/tests/javascript/testrunnerNode.js @@ -9,7 +9,10 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; // ignore ssl errors const puppeteer = require('puppeteer'); -const url = process.argv[2] || 'http://localhost/tests/javascript/'; +const baseUrl = process.argv[2] || 'http://localhost/tests/javascript/'; + +const pluginArg = process.argv.find((arg) => /--plugin=(.*?)/.test(arg)); +const plugin = pluginArg ?pluginArg.split('=', 2)[1] : null; main(); @@ -22,6 +25,11 @@ async function main() { console.log("[" + consoleMessage.type() + "] " + consoleMessage.text()); }); + let url = baseUrl; + if (plugin) { + url += `?module=${encodeURIComponent(plugin)}`; + } + await page.goto(url); await page.waitForFunction(() => window.QUnit); @@ -64,4 +72,4 @@ async function main() { }); process.exit(success ? 0 : 1); -} \ No newline at end of file +} diff --git a/tests/javascript/testrunnerPhantom.js b/tests/javascript/testrunnerPhantom.js index 7194540ec82..608cc3566a8 100644 --- a/tests/javascript/testrunnerPhantom.js +++ b/tests/javascript/testrunnerPhantom.js @@ -23,7 +23,23 @@ var fs = require("fs"); var system = require("system"); -var url = system.args[1] || 'http://localhost/tests/javascript/'; +var baseUrl = system.args[1] || 'http://localhost/tests/javascript/'; + +function getPluginArg() { + for (var i = 0; i < system.args.length; ++i) { + if (/--plugin=(.*?)/.test(system.args[i])) { + return system.args[i].split('=', 2)[1]; + } + } + return null; +} + +var plugin = getPluginArg(); + +var url = baseUrl; +if (plugin) { + url += '?module=' + encodeURIComponent(plugin); +} function printError(message) { console.error(message + "\n"); @@ -95,4 +111,4 @@ page.open(url, function(success) { printError("Failure opening " + url); phantom.exit(1); } -}); \ No newline at end of file +});