Backed out changeset 0a133d5fd155 (bug 1864534) for causing screenshot related failur...
[gecko.git] / testing / marionette / mach_commands.py
blob7736806d1e5bc449978f5d66b4d85df5e8da8e25
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/.
5 import argparse
6 import functools
7 import logging
8 import os
9 import sys
11 from six import iteritems
13 from mach.decorators import (
14 Command,
17 from mozbuild.base import (
18 MachCommandConditions as conditions,
19 BinaryNotFoundException,
22 SUPPORTED_APPS = ["firefox", "android", "thunderbird"]
25 def create_parser_tests():
26 from marionette_harness.runtests import MarionetteArguments
27 from mozlog.structured import commandline
29 parser = MarionetteArguments()
30 commandline.add_logging_group(parser)
31 return parser
34 def run_marionette(tests, binary=None, topsrcdir=None, **kwargs):
35 from mozlog.structured import commandline
37 from marionette_harness.runtests import MarionetteTestRunner, MarionetteHarness
39 parser = create_parser_tests()
41 args = argparse.Namespace(tests=tests)
43 args.binary = binary
44 args.logger = kwargs.pop("log", None)
46 for k, v in iteritems(kwargs):
47 setattr(args, k, v)
49 parser.verify_usage(args)
51 # Causes Firefox to crash when using non-local connections.
52 os.environ["MOZ_DISABLE_NONLOCAL_CONNECTIONS"] = "1"
54 if not args.logger:
55 args.logger = commandline.setup_logging(
56 "Marionette Unit Tests", args, {"mach": sys.stdout}
58 failed = MarionetteHarness(MarionetteTestRunner, args=vars(args)).run()
59 if failed > 0:
60 return 1
61 else:
62 return 0
65 @Command(
66 "marionette-test",
67 category="testing",
68 description="Remote control protocol to Gecko, used for browser automation.",
69 conditions=[functools.partial(conditions.is_buildapp_in, apps=SUPPORTED_APPS)],
70 parser=create_parser_tests,
72 def marionette_test(command_context, tests, **kwargs):
73 if "test_objects" in kwargs:
74 tests = []
75 for obj in kwargs["test_objects"]:
76 tests.append(obj["file_relpath"])
77 del kwargs["test_objects"]
79 if not tests:
80 if conditions.is_thunderbird(command_context):
81 tests = [
82 os.path.join(
83 command_context.topsrcdir,
84 "comm/testing/marionette/unit-tests.ini",
87 else:
88 tests = [
89 os.path.join(
90 command_context.topsrcdir,
91 "testing/marionette/harness/marionette_harness/tests/unit-tests.toml",
95 if not kwargs.get("binary") and (
96 conditions.is_firefox(command_context)
97 or conditions.is_thunderbird(command_context)
99 try:
100 kwargs["binary"] = command_context.get_binary_path("app")
101 except BinaryNotFoundException as e:
102 command_context.log(
103 logging.ERROR,
104 "marionette-test",
105 {"error": str(e)},
106 "ERROR: {error}",
108 command_context.log(
109 logging.INFO, "marionette-test", {"help": e.help()}, "{help}"
111 return 1
113 return run_marionette(tests, topsrcdir=command_context.topsrcdir, **kwargs)