2012-09-26 20:43:53 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
2012-10-10 22:08:09 +04:00
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2012-09-26 20:43:53 +04:00
|
|
|
|
2012-10-10 22:08:09 +04:00
|
|
|
# Integrates the xpcshell test runner with mach.
|
2012-09-26 20:43:53 +04:00
|
|
|
|
2016-05-26 21:56:20 +03:00
|
|
|
import errno
|
2020-04-21 14:58:04 +03:00
|
|
|
import logging
|
2012-10-10 22:08:09 +04:00
|
|
|
import os
|
2012-11-14 22:05:18 +04:00
|
|
|
import sys
|
2022-12-21 17:38:37 +03:00
|
|
|
from multiprocessing import cpu_count
|
2012-09-26 20:43:53 +04:00
|
|
|
|
2022-12-21 17:38:37 +03:00
|
|
|
from mach.decorators import Command
|
2023-03-20 16:06:28 +03:00
|
|
|
from mozbuild.base import BinaryNotFoundException, MozbuildObject
|
2022-12-21 17:38:37 +03:00
|
|
|
from mozbuild.base import MachCommandConditions as conditions
|
2014-10-22 23:53:42 +04:00
|
|
|
from mozlog import structured
|
2016-11-09 22:50:45 +03:00
|
|
|
from xpcshellcommandline import parser_desktop, parser_remote
|
2014-03-11 18:08:25 +04:00
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
2012-09-26 20:43:53 +04:00
|
|
|
|
2016-11-09 22:50:45 +03:00
|
|
|
|
2012-11-29 00:19:32 +04:00
|
|
|
# This should probably be consolidated with similar classes in other test
|
|
|
|
# runners.
|
|
|
|
class InvalidTestPathError(Exception):
|
|
|
|
"""Exception raised when the test path is not valid."""
|
|
|
|
|
|
|
|
|
2012-09-26 20:43:53 +04:00
|
|
|
class XPCShellRunner(MozbuildObject):
|
|
|
|
"""Run xpcshell tests."""
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2012-10-31 21:29:26 +04:00
|
|
|
def run_suite(self, **kwargs):
|
2015-08-27 15:05:50 +03:00
|
|
|
return self._run_xpcshell_harness(**kwargs)
|
|
|
|
|
|
|
|
def run_test(self, **kwargs):
|
2012-09-26 20:43:53 +04:00
|
|
|
"""Runs an individual xpcshell test."""
|
2013-10-23 03:54:40 +04:00
|
|
|
|
2012-12-20 12:43:19 +04:00
|
|
|
# TODO Bug 794506 remove once mach integrates with virtualenv.
|
|
|
|
build_path = os.path.join(self.topobjdir, "build")
|
|
|
|
if build_path not in sys.path:
|
|
|
|
sys.path.append(build_path)
|
2012-10-31 21:29:26 +04:00
|
|
|
|
2015-07-29 18:50:16 +03:00
|
|
|
src_build_path = os.path.join(self.topsrcdir, "mozilla", "build")
|
|
|
|
if os.path.isdir(src_build_path):
|
|
|
|
sys.path.append(src_build_path)
|
2015-01-30 18:54:00 +03:00
|
|
|
|
2016-10-06 00:24:36 +03:00
|
|
|
return self.run_suite(**kwargs)
|
2012-09-26 20:43:53 +04:00
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
def _run_xpcshell_harness(self, **kwargs):
|
2012-09-26 20:43:53 +04:00
|
|
|
# Obtain a reference to the xpcshell test runner.
|
|
|
|
import runxpcshelltests
|
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
log = kwargs.pop("log")
|
|
|
|
|
2014-10-22 23:53:42 +04:00
|
|
|
xpcshell = runxpcshelltests.XPCShellTests(log=log)
|
2012-09-26 20:43:53 +04:00
|
|
|
self.log_manager.enable_unstructured()
|
|
|
|
|
|
|
|
tests_dir = os.path.join(self.topobjdir, "_tests", "xpcshell")
|
2014-02-06 06:51:00 +04:00
|
|
|
# We want output from the test to be written immediately if we are only
|
|
|
|
# running a single test.
|
2015-09-29 22:59:55 +03:00
|
|
|
single_test = (
|
|
|
|
len(kwargs["testPaths"]) == 1
|
|
|
|
and os.path.isfile(kwargs["testPaths"][0])
|
|
|
|
or kwargs["manifest"]
|
|
|
|
and (len(kwargs["manifest"].test_paths()) == 1)
|
2020-10-26 21:34:53 +03:00
|
|
|
)
|
2015-09-29 22:59:55 +03:00
|
|
|
|
|
|
|
if single_test:
|
|
|
|
kwargs["verbose"] = True
|
2015-08-27 15:05:50 +03:00
|
|
|
|
|
|
|
if kwargs["xpcshell"] is None:
|
2020-04-21 14:58:04 +03:00
|
|
|
try:
|
|
|
|
kwargs["xpcshell"] = self.get_binary_path("xpcshell")
|
|
|
|
except BinaryNotFoundException as e:
|
|
|
|
self.log(
|
|
|
|
logging.ERROR, "xpcshell-test", {"error": str(e)}, "ERROR: {error}"
|
|
|
|
)
|
|
|
|
self.log(logging.INFO, "xpcshell-test", {"help": e.help()}, "{help}")
|
|
|
|
return 1
|
2015-08-27 15:05:50 +03:00
|
|
|
|
|
|
|
if kwargs["mozInfo"] is None:
|
|
|
|
kwargs["mozInfo"] = os.path.join(self.topobjdir, "mozinfo.json")
|
|
|
|
|
|
|
|
if kwargs["symbolsPath"] is None:
|
|
|
|
kwargs["symbolsPath"] = os.path.join(self.distdir, "crashreporter-symbols")
|
|
|
|
|
|
|
|
if kwargs["logfiles"] is None:
|
|
|
|
kwargs["logfiles"] = False
|
|
|
|
|
|
|
|
if kwargs["profileName"] is None:
|
|
|
|
kwargs["profileName"] = "firefox"
|
|
|
|
|
2015-09-18 17:00:40 +03:00
|
|
|
if kwargs["testingModulesDir"] is None:
|
|
|
|
kwargs["testingModulesDir"] = os.path.join(self.topobjdir, "_tests/modules")
|
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
if kwargs["utility_path"] is None:
|
|
|
|
kwargs["utility_path"] = self.bindir
|
|
|
|
|
|
|
|
if kwargs["manifest"] is None:
|
2023-12-21 02:16:43 +03:00
|
|
|
kwargs["manifest"] = os.path.join(tests_dir, "xpcshell.toml")
|
2015-08-27 15:05:50 +03:00
|
|
|
|
|
|
|
if kwargs["failure_manifest"] is None:
|
|
|
|
kwargs["failure_manifest"] = os.path.join(
|
2023-12-21 02:16:43 +03:00
|
|
|
self.statedir, "xpcshell.failures.toml"
|
2015-08-27 15:05:50 +03:00
|
|
|
)
|
2013-08-21 20:26:46 +04:00
|
|
|
|
2016-05-26 21:56:20 +03:00
|
|
|
# Use the object directory for the temp directory to minimize the chance
|
|
|
|
# of file scanning. The overhead from e.g. search indexers and anti-virus
|
|
|
|
# scanners like Windows Defender can add tons of overhead to test execution.
|
|
|
|
# We encourage people to disable these things in the object directory.
|
|
|
|
temp_dir = os.path.join(self.topobjdir, "temp")
|
|
|
|
try:
|
|
|
|
os.mkdir(temp_dir)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
kwargs["tempDir"] = temp_dir
|
|
|
|
|
2020-07-09 19:49:48 +03:00
|
|
|
result = xpcshell.runTests(kwargs)
|
2012-09-26 20:43:53 +04:00
|
|
|
|
|
|
|
self.log_manager.disable_unstructured()
|
2012-10-10 22:08:09 +04:00
|
|
|
|
2013-08-21 03:07:33 +04:00
|
|
|
if not result and not xpcshell.sequential:
|
2013-08-16 02:22:35 +04:00
|
|
|
print(
|
|
|
|
"Tests were run in parallel. Try running with --sequential "
|
|
|
|
"to make sure the failures were not caused by this."
|
|
|
|
)
|
2012-12-06 03:42:11 +04:00
|
|
|
return int(not result)
|
|
|
|
|
2016-11-09 22:50:45 +03:00
|
|
|
|
2013-12-14 08:20:39 +04:00
|
|
|
class AndroidXPCShellRunner(MozbuildObject):
|
|
|
|
"""Run Android xpcshell tests."""
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
def run_test(self, **kwargs):
|
2013-12-14 08:20:39 +04:00
|
|
|
# TODO Bug 794506 remove once mach integrates with virtualenv.
|
|
|
|
build_path = os.path.join(self.topobjdir, "build")
|
|
|
|
if build_path not in sys.path:
|
|
|
|
sys.path.append(build_path)
|
|
|
|
|
|
|
|
import remotexpcshelltests
|
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
log = kwargs.pop("log")
|
|
|
|
self.log_manager.enable_unstructured()
|
|
|
|
|
|
|
|
if kwargs["xpcshell"] is None:
|
|
|
|
kwargs["xpcshell"] = "xpcshell"
|
|
|
|
|
|
|
|
if not kwargs["objdir"]:
|
|
|
|
kwargs["objdir"] = self.topobjdir
|
|
|
|
|
|
|
|
if not kwargs["localBin"]:
|
|
|
|
kwargs["localBin"] = os.path.join(self.topobjdir, "dist/bin")
|
|
|
|
|
|
|
|
if not kwargs["testingModulesDir"]:
|
|
|
|
kwargs["testingModulesDir"] = os.path.join(self.topobjdir, "_tests/modules")
|
|
|
|
|
|
|
|
if not kwargs["mozInfo"]:
|
|
|
|
kwargs["mozInfo"] = os.path.join(self.topobjdir, "mozinfo.json")
|
|
|
|
|
|
|
|
if not kwargs["manifest"]:
|
|
|
|
kwargs["manifest"] = os.path.join(
|
2023-12-21 02:16:43 +03:00
|
|
|
self.topobjdir, "_tests/xpcshell/xpcshell.toml"
|
2015-08-27 15:05:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if not kwargs["symbolsPath"]:
|
|
|
|
kwargs["symbolsPath"] = os.path.join(self.distdir, "crashreporter-symbols")
|
|
|
|
|
2024-02-29 18:53:30 +03:00
|
|
|
if not kwargs["localAPK"]:
|
2019-08-21 21:24:08 +03:00
|
|
|
for root, _, paths in os.walk(os.path.join(kwargs["objdir"], "gradle")):
|
|
|
|
for file_name in paths:
|
|
|
|
if file_name.endswith(".apk") and file_name.startswith(
|
2021-11-02 21:56:26 +03:00
|
|
|
"test_runner-withGeckoBinaries"
|
2019-08-21 21:24:08 +03:00
|
|
|
):
|
|
|
|
kwargs["localAPK"] = os.path.join(root, file_name)
|
|
|
|
print("using APK: %s" % kwargs["localAPK"])
|
|
|
|
break
|
|
|
|
if kwargs["localAPK"]:
|
2013-12-14 08:20:39 +04:00
|
|
|
break
|
|
|
|
else:
|
2016-03-11 01:18:40 +03:00
|
|
|
raise Exception("APK not found in objdir. You must specify an APK.")
|
|
|
|
|
2018-03-17 18:28:23 +03:00
|
|
|
xpcshell = remotexpcshelltests.XPCShellRemote(kwargs, log)
|
2013-12-14 08:20:39 +04:00
|
|
|
|
2017-08-15 18:06:16 +03:00
|
|
|
result = xpcshell.runTests(
|
|
|
|
kwargs,
|
|
|
|
testClass=remotexpcshelltests.RemoteXPCShellTestThread,
|
|
|
|
mobileArgs=xpcshell.mobileArgs,
|
|
|
|
)
|
2013-12-14 08:20:39 +04:00
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
self.log_manager.disable_unstructured()
|
2013-12-14 08:20:39 +04:00
|
|
|
|
|
|
|
return int(not result)
|
2012-10-10 22:08:09 +04:00
|
|
|
|
2014-03-11 18:08:25 +04:00
|
|
|
|
2015-08-27 15:05:50 +03:00
|
|
|
def get_parser():
|
|
|
|
build_obj = MozbuildObject.from_environment(cwd=here)
|
2024-02-29 18:53:30 +03:00
|
|
|
if conditions.is_android(build_obj):
|
2015-08-27 15:05:50 +03:00
|
|
|
return parser_remote()
|
|
|
|
else:
|
|
|
|
return parser_desktop()
|
2014-03-11 18:08:25 +04:00
|
|
|
|
2016-11-09 22:50:45 +03:00
|
|
|
|
2021-09-27 21:12:51 +03:00
|
|
|
@Command(
|
|
|
|
"xpcshell-test",
|
|
|
|
category="testing",
|
|
|
|
description="Run XPCOM Shell tests (API direct unit testing)",
|
|
|
|
conditions=[lambda *args: True],
|
|
|
|
parser=get_parser,
|
|
|
|
)
|
|
|
|
def run_xpcshell_test(command_context, test_objects=None, **params):
|
|
|
|
from mozbuild.controller.building import BuildDriver
|
|
|
|
|
|
|
|
if test_objects is not None:
|
|
|
|
from manifestparser import TestManifest
|
|
|
|
|
|
|
|
m = TestManifest()
|
|
|
|
m.tests.extend(test_objects)
|
|
|
|
params["manifest"] = m
|
|
|
|
|
|
|
|
driver = command_context._spawn(BuildDriver)
|
|
|
|
driver.install_tests()
|
|
|
|
|
|
|
|
# We should probably have a utility function to ensure the tree is
|
|
|
|
# ready to run tests. Until then, we just create the state dir (in
|
|
|
|
# case the tree wasn't built with mach).
|
|
|
|
command_context._ensure_state_subdir_exists(".")
|
|
|
|
|
|
|
|
if not params.get("log"):
|
|
|
|
log_defaults = {
|
|
|
|
command_context._mach_context.settings["test"]["format"]: sys.stdout
|
|
|
|
}
|
|
|
|
fmt_defaults = {
|
|
|
|
"level": command_context._mach_context.settings["test"]["level"],
|
|
|
|
"verbose": True,
|
|
|
|
}
|
|
|
|
params["log"] = structured.commandline.setup_logging(
|
|
|
|
"XPCShellTests", params, log_defaults, fmt_defaults
|
|
|
|
)
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2021-09-27 21:12:51 +03:00
|
|
|
if not params["threadCount"]:
|
|
|
|
# pylint --py3k W1619
|
|
|
|
params["threadCount"] = int((cpu_count() * 3) / 2)
|
2017-03-06 07:39:00 +03:00
|
|
|
|
2024-02-29 18:53:30 +03:00
|
|
|
if conditions.is_android(command_context):
|
2021-09-27 21:12:51 +03:00
|
|
|
from mozrunner.devices.android_device import (
|
|
|
|
InstallIntent,
|
2022-12-21 17:38:37 +03:00
|
|
|
get_adb_path,
|
|
|
|
verify_android_device,
|
2021-09-27 21:12:51 +03:00
|
|
|
)
|
2012-11-29 00:19:32 +04:00
|
|
|
|
2021-09-27 21:12:51 +03:00
|
|
|
install = InstallIntent.YES if params["setup"] else InstallIntent.NO
|
|
|
|
device_serial = params.get("deviceSerial")
|
|
|
|
verify_android_device(
|
|
|
|
command_context,
|
|
|
|
network=True,
|
|
|
|
install=install,
|
|
|
|
device_serial=device_serial,
|
|
|
|
)
|
|
|
|
if not params["adbPath"]:
|
|
|
|
params["adbPath"] = get_adb_path(command_context)
|
|
|
|
xpcshell = command_context._spawn(AndroidXPCShellRunner)
|
|
|
|
else:
|
|
|
|
xpcshell = command_context._spawn(XPCShellRunner)
|
|
|
|
xpcshell.cwd = command_context._mach_context.cwd
|
|
|
|
|
|
|
|
try:
|
|
|
|
return xpcshell.run_test(**params)
|
|
|
|
except InvalidTestPathError as e:
|
|
|
|
print(str(e))
|
|
|
|
return 1
|