mach_commands.py (3821B)
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 # Integrates android tests with mach 6 7 import os 8 9 from mach.decorators import Command, CommandArgument 10 11 12 def classname_for_test(test, test_path): 13 """Convert path of test file to gradle recognized test suite name""" 14 # Example: 15 # test = mobile/android/android-components/components/feature/addons/src/test/java/mozilla/components/feature/addons/ui/PermissionsDialogFragmentTest.kt 16 # test_path = src/test/java 17 # returns = mozilla.components.feature.addons.ui.PermissionsDialogFragmentTest 18 return ( 19 os.path.normpath(test) 20 .split(os.path.normpath(test_path))[-1] 21 .removeprefix(os.path.sep) 22 .replace(os.path.sep, ".") 23 .removesuffix(".kt") 24 ) 25 26 27 def project_for_ac(test, test_path): 28 """Get project name for android-component subprojects from path of test file""" 29 # Example: 30 # test = mobile/android/android-components/components/feature/addons/src/test/java/mozilla/components/feature/addons/ui/PermissionsDialogFragmentTest.kt 31 # test_path = src/test/java 32 # returns = feature-addons 33 dir = os.path.normpath("mobile/android/android-components/components") 34 return ( 35 os.path.normpath(test) 36 .split(os.path.normpath(dir))[-1] 37 .split(os.path.normpath(test_path))[0] 38 .removeprefix(os.path.sep) 39 .removesuffix(os.path.sep) 40 .replace(os.path.sep, "-") 41 ) 42 43 44 @Command( 45 "android-test", 46 category="testing", 47 description="Run Android tests.", 48 ) 49 @CommandArgument( 50 "--subproject", 51 default="fenix", 52 choices=["fenix", "focus", "android-components", "ac"], 53 help="Android subproject to run tests for.", 54 ) 55 @CommandArgument( 56 "--test", 57 default=None, 58 help="Test to run", 59 ) 60 def run_android_test(command_context, subproject, test=None, test_objects=[], **kwargs): 61 gradle_command = [] 62 AC = ("android-components", "ac") 63 if subproject == "fenix": 64 gradle_command = ["testDebug", "testDebugUnitTest"] 65 subdir = os.path.join("mobile", "android", "fenix") 66 test_path = os.path.join(subdir, "app", "src", "test", "java") 67 elif subproject == "focus": 68 gradle_command = ["testFocusDebugUnitTest"] 69 subdir = os.path.join("mobile", "android", "focus-android") 70 test_path = os.path.join(subdir, "app", "src", "test", "java") 71 elif subproject in AC: 72 subdir = os.path.join("mobile", "android", "android-components") 73 if not test_objects and not test: 74 return command_context._mach_context.commands.dispatch( 75 "gradle", 76 command_context._mach_context, 77 args=["-q", "test", "--rerun", "-p", subdir], 78 ) 79 test_path = os.path.join("src", "test", "java") 80 else: 81 return None 82 for test_object in test_objects: 83 if subproject in AC: 84 gradle_command.append( 85 ":components:" 86 + project_for_ac(test_object["name"], test_path) 87 + ":testDebugUnitTest" 88 ) 89 gradle_command.append("--tests") 90 gradle_command.append(classname_for_test(test_object["name"], test_path)) 91 if test: 92 if subproject in AC: 93 gradle_command.append( 94 ":components:" + project_for_ac(test, test_path) + ":testDebugUnitTest" 95 ) 96 gradle_command.append("--tests") 97 gradle_command.append(classname_for_test(test, test_path)) 98 return command_context._mach_context.commands.dispatch( 99 "gradle", 100 command_context._mach_context, 101 args=gradle_command + ["-q", "--rerun", "-p", subdir], 102 )