tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mach_commands.py (2859B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import argparse
      6 import os
      7 import subprocess
      8 
      9 from mach.decorators import Command, CommandArgument
     10 from mozbuild.base import MozbuildObject
     11 from mozbuild.nodeutil import find_node_executable
     12 
     13 suites = [
     14    "aboutdebugging",
     15    "accessibility",
     16    "all",
     17    "application",
     18    "compatibility",
     19    "debugger",
     20    "framework",
     21    "netmonitor",
     22    "performance",
     23    "shared_components",
     24    "webconsole",
     25 ]
     26 
     27 
     28 class DevToolsNodeTestRunner(MozbuildObject):
     29    """Run DevTools node tests."""
     30 
     31    def run_node_tests(self, suite=None, artifact=None):
     32        """Run the DevTools node test suites."""
     33        devtools_bin_dir = os.path.join(self.topsrcdir, "devtools", "client", "bin")
     34        test_runner_script = os.path.join(
     35            devtools_bin_dir, "devtools-node-test-runner.js"
     36        )
     37 
     38        if suite and suite not in suites:
     39            print(
     40                f"ERROR: Invalid suite '{suite}'. Valid suites are: {', '.join(suites)}"
     41            )
     42            return 1
     43 
     44        # Build the command to run
     45        node_binary, _ = find_node_executable()
     46        cmd = [node_binary, test_runner_script]
     47 
     48        # Add artifact argument if specified
     49        if artifact:
     50            cmd.append(f"--artifact={artifact}")
     51 
     52        # Add suite argument
     53        cmd.append(f"--suite={suite}")
     54 
     55        print(f"Running: {' '.join(cmd)}")
     56        print(f"Working directory: {devtools_bin_dir}")
     57 
     58        try:
     59            # Run the test runner from the devtools bin directory
     60            result = subprocess.run(cmd, cwd=devtools_bin_dir, check=False)
     61            return result.returncode
     62        except FileNotFoundError:
     63            print(
     64                "ERROR: Node.js not found. Please ensure Node.js is installed and in your PATH."
     65            )
     66            return 1
     67        except Exception as e:
     68            print(f"ERROR: Failed to run DevTools node tests: {e}")
     69            return 1
     70 
     71 
     72 @Command(
     73    "devtools-node-test",
     74    category="testing",
     75    description="Run DevTools node tests",
     76    parser=argparse.ArgumentParser(),
     77 )
     78 @CommandArgument(
     79    "--suite",
     80    default="all",
     81    help=f"(optional) Test suite to run. Runs all suites when omitted. Available suites: {', '.join(suites)}",
     82 )
     83 @CommandArgument(
     84    "--artifact",
     85    help="Path to write test error artifacts as JSON. Useful for CI integration "
     86    "and error reporting.",
     87 )
     88 def run_devtools_node_test(command_context, suite=None, artifact=None, **kwargs):
     89    """Run DevTools node tests."""
     90    runner = DevToolsNodeTestRunner.from_environment(
     91        cwd=os.getcwd(), detect_virtualenv_mozinfo=False
     92    )
     93 
     94    return runner.run_node_tests(suite=suite, artifact=artifact)