File: mach_commands.py

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (198 lines) | stat: -rw-r--r-- 6,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.

# Integrates android tests with mach

import os

from mach.decorators import Command, CommandArgument


def classname_for_test(test, test_path):
    """Convert path of test file to gradle recognized test suite name"""
    # Example:
    # test      = mobile/android/android-components/components/feature/addons/src/test/java/mozilla/components/feature/addons/ui/PermissionsDialogFragmentTest.kt
    # test_path = src/test/java
    # returns   = mozilla.components.feature.addons.ui.PermissionsDialogFragmentTest
    return (
        os.path.normpath(test)
        .split(os.path.normpath(test_path))[-1]
        .removeprefix(os.path.sep)
        .replace(os.path.sep, ".")
        .removesuffix(".kt")
        .removesuffix(".java")
    )


def project_for_test(test, prefix):
    """Get android project that test belongs to"""
    # Example:
    # test      = mobile/android/android-components/components/feature/addons/src/test/java/mozilla/components/feature/addons/ui/PermissionsDialogFragmentTest.kt
    # prefix    = mobile/android
    # returns   = android-components
    return (
        os.path.normpath(test)
        .split(os.path.normpath(prefix))[-1]
        .removeprefix(os.path.sep)
        .split(os.path.sep)[0]
    )


def project_for_ac(test, prefix, test_path):
    """Get project name for android-component subprojects from path of test file"""
    # Example:
    # test      = mobile/android/android-components/components/feature/addons/src/test/java/mozilla/components/feature/addons/ui/PermissionsDialogFragmentTest.kt
    # prefix    = mobile/android/android-components/components
    # test_path = src/test/java
    # returns   = feature-addons
    return (
        os.path.normpath(test)
        .split(os.path.normpath(prefix))[-1]
        .split(os.path.normpath(test_path))[0]
        .removeprefix(os.path.sep)
        .removesuffix(os.path.sep)
        .replace(os.path.sep, "-")
    )


def flavor_for_test(test):
    """Get the type of the test"""
    # Example:
    # test      = mobile/android/fenix/app/src/androidTest/java/org/mozilla/fenix/components/MenuItemTest.kt
    # returns   = android

    android_prefix = os.path.join("src", "androidTest", "java")
    if android_prefix in os.path.normpath(test):
        return "android"
    else:
        return "unit"


@Command(
    "android-test",
    category="testing",
    description="Run Android tests.",
)
@CommandArgument(
    "--subproject",
    default="fenix",
    choices=["fenix", "focus", "android-components", "ac", "geckoview", "gv"],
    help="Android subproject to run tests for.",
)
@CommandArgument(
    "--flavor",
    default="unit",
    choices=["unit", "android", "both"],
    help="The unit test suite runs on host using JUnit or Robolectric, while"
    + " the android suite requires an emulator or device. This is determined"
    + " automatically if a specific test is specified.",
)
@CommandArgument(
    "--gradle-variant",
    default=None,
    help="The gradle project variant to use (eg. Debug, FocusNightly).",
)
@CommandArgument(
    "--test",
    default=None,
    help="File path of test to run.",
)
def run_android_test(
    command_context,
    subproject,
    flavor="unit",
    gradle_variant=None,
    test=None,
    test_objects=[],
    **kwargs,
):
    # Test paths may be a single command line, or a list from the test harness
    tests = [test["name"] for test in test_objects]
    if test:
        tests.append(test.strip())

    # Override subproject if test explicitly set
    if test:
        prefix = os.path.join("mobile", "android")
        subproject = project_for_test(test, prefix)

    # Resolve subproject aliases to match directory name as the canonical one
    ALIASES = {
        "focus": "focus-android",
        "ac": "android-components",
        "gv": "geckoview",
    }
    subproject = ALIASES.get(subproject, subproject)

    # Determine default gradle variant
    if not gradle_variant:
        gradle_variant = "FocusDebug" if (subproject == "focus-android") else "Debug"

    # Runs gradle in "quiet" mode
    gradle_command = ["-q"]

    # Determine gradle project directory
    if subproject == "fenix":
        subdir = os.path.join("mobile", "android", "fenix", "app")
    elif subproject == "focus-android":
        subdir = os.path.join("mobile", "android", "focus-android", "app")
    elif subproject == "android-components":
        subdir = os.path.join("mobile", "android", "android-components")
    elif subproject == "geckoview":
        subdir = os.path.join("mobile", "android", "geckoview")
    else:
        return None
    gradle_command.append("-p")
    gradle_command.append(subdir)

    # Partition tests based on type
    unit_tests = [t for t in tests if flavor_for_test(t) == "unit"]
    android_tests = [t for t in tests if flavor_for_test(t) == "android"]

    def project_name(test, test_path):
        prefix = os.path.join(subdir, "components")
        return project_for_ac(test, prefix, test_path)

    # Tests based on 'testUnitTest' family of tasks
    gradle_unittest = f"test{gradle_variant}UnitTest"
    if unit_tests:
        test_path = os.path.join("src", "test", "java")
        if subproject == "android-components":
            for p in dict.fromkeys(project_name(t, test_path) for t in unit_tests):
                gradle_command.append(f":components:{p}:{gradle_unittest}")
        else:
            gradle_command.append(gradle_unittest)

        # Compute the class names from file names
        gradle_command.append("--rerun")
        for t in unit_tests:
            gradle_command.append("--tests")
            gradle_command.append(classname_for_test(t, test_path))

    # Tests based on 'connectedAndroidTest' family of tasks
    gradle_androidtest = f"connected{gradle_variant}AndroidTest"
    if android_tests:
        test_path = os.path.join("src", "androidTest", "java")
        if subproject == "android-components":
            for p in dict.fromkeys(project_name(t, test_path) for t in android_tests):
                gradle_command.append(f":components:{p}:{gradle_androidtest}")
        else:
            gradle_command.append(gradle_androidtest)

        for t in android_tests:
            name = classname_for_test(t, test_path)
            gradle_command.append(
                f"-Pandroid.testInstrumentationRunnerArguments.class={name}"
            )

    # If no tests specified, run whole suite based on flavor
    if not tests:
        if flavor in ("both", "unit"):
            gradle_command.append(gradle_unittest)
        if flavor in ("both", "android"):
            gradle_command.append(gradle_androidtest)

    return command_context._mach_context.commands.dispatch(
        "gradle", command_context._mach_context, args=gradle_command
    )