Bug 1897186 - Add android-components tests to mach test. r=ohall,nalexander,jmaher

Commands added:

`mach test android-components` or `mach test ac` runs all tests for android-components
`mach test mobile/android/android-components/components/browser/state/src/test/java/mozilla/components/browser/state/engine/middleware/TranslationsMiddlewareTest.kt` will run all tests in the file

Differential Revision: https://phabricator.services.mozilla.com/D226392
This commit is contained in:
Aaditya Dhingra 2024-10-22 16:49:37 +00:00
parent 4d8e6ae8a2
commit 77f806f492
2 changed files with 95 additions and 9 deletions

View File

@ -20,6 +20,19 @@ def classname_for_test(test, test_path):
)
def project_for_ac(test, test_path):
"""Get project name for android-component subprojects from path of test file"""
dir = os.path.normpath("mobile/android/android-components/components")
return (
os.path.normpath(test)
.split(os.path.normpath(dir))[-1]
.split(os.path.normpath(test_path))[0]
.removeprefix(os.path.sep)
.removesuffix(os.path.sep)
.replace(os.path.sep, "-")
)
@Command(
"android-test",
category="testing",
@ -28,7 +41,7 @@ def classname_for_test(test, test_path):
@CommandArgument(
"--subproject",
default="fenix",
choices=["fenix", "focus"],
choices=["fenix", "focus", "android-components", "ac"],
help="Android subproject to run tests for.",
)
@CommandArgument(
@ -37,13 +50,15 @@ def classname_for_test(test, test_path):
help="Test to run",
)
def run_android_test(command_context, subproject, test=None, test_objects=[], **kwargs):
gradle_command = []
AC = ("android-components", "ac")
if subproject == "fenix":
gradle_subcommand = ":fenix:testDebug"
gradle_command = [":fenix:testDebug"]
test_path = os.path.join(
"mobile", "android", "fenix", "app", "src", "test", "java"
)
elif subproject == "focus":
gradle_subcommand = ":focus-android:testFocusDebugUnitTest"
gradle_command = [":focus-android:testFocusDebugUnitTest"]
test_path = os.path.join(
"mobile",
"android",
@ -53,17 +68,37 @@ def run_android_test(command_context, subproject, test=None, test_objects=[], **
"test",
"java",
)
elif subproject in AC:
if not test_objects or test:
return command_context._mach_context.commands.dispatch(
"gradle",
command_context._mach_context,
args=["-q", "test"],
topsrcdir=os.path.join(
command_context.topsrcdir, "mobile", "android", "android-components"
),
)
test_path = os.path.join("src", "test", "java")
else:
return None
test_classes = []
for test_object in test_objects:
test_classes.append("--tests")
test_classes.append(classname_for_test(test_object["name"], test_path))
if subproject in AC:
gradle_command.append(
":"
+ project_for_ac(test_object["name"], test_path)
+ ":testDebugUnitTest"
)
gradle_command.append("--tests")
gradle_command.append(classname_for_test(test_object["name"], test_path))
if test:
test_classes.append("--tests")
test_classes.append(classname_for_test(test, test_path))
if subproject in AC:
gradle_command.append(
":" + project_for_ac(test, test_path) + ":testDebugUnitTest"
)
gradle_command.append("--tests")
gradle_command.append(classname_for_test(test, test_path))
return command_context._mach_context.commands.dispatch(
"gradle",
command_context._mach_context,
args=[gradle_subcommand, "-q"] + test_classes,
args=["-q"] + gradle_command,
)

View File

@ -382,6 +382,12 @@ TEST_SUITES = {
"mach_command": "android-test",
"kwargs": {"subproject": "focus"},
},
"android-components": {
"aliases": ("ac",),
"build_flavor": "android-components",
"mach_command": "android-test",
"kwargs": {"subproject": "android-components"},
},
"geckoview": {
"aliases": ("gv",),
"build_flavor": "geckoview",
@ -446,6 +452,7 @@ _test_flavors = {
"xpcshell": "xpcshell",
"fenix": "fenix",
"focus": "focus",
"android-components": "android-components",
"geckoview": "geckoview",
}
@ -659,6 +666,7 @@ class TestResolver(MozbuildObject):
# These suites aren't registered in moz.build so require special handling.
self._puppeteer_loaded = False
self._fenix_loaded = False
self._ac_loaded = False
self._focus_loaded = False
self._geckoview_junit_loaded = False
self._tests_loaded = False
@ -840,6 +848,11 @@ class TestResolver(MozbuildObject):
):
self.add_focus_manifest_data()
if flavor in ("", "android-components", None) and (
any(self.is_ac_path(p) for p in paths) or paths == [None]
):
self.add_ac_manifest_data()
if flavor in ("", "geckoview", "junit", None) and (
any(self.is_geckoview_junit_path(p) for p in paths) or paths == [None]
):
@ -899,6 +912,11 @@ class TestResolver(MozbuildObject):
return True
return mozpath.match(path, "mobile/android/focus-android/**")
def is_ac_path(self, path):
if path is None:
return True
return mozpath.match(path, "mobile/android/android-components/**")
def is_geckoview_junit_path(self, path):
if path is None:
return True
@ -1001,6 +1019,39 @@ class TestResolver(MozbuildObject):
self._focus_loaded = True
def add_ac_manifest_data(self):
if self._ac_loaded:
return
self._reset_state()
test_path = os.path.join(
self.topsrcdir, "mobile", "android", "android-components"
)
test_subdir_path = os.path.join("src", "test", "java")
for root, dirs, paths in os.walk(test_path):
if test_subdir_path in root:
for filename in fnmatch.filter(paths, "*.kt"):
path = os.path.join(root, filename)
self._tests.append(
{
"path": os.path.abspath(path),
"flavor": "android-components",
"here": os.path.dirname(path),
"manifest": None,
"name": path,
"file_relpath": path,
"head": "",
"support-files": "",
"subsuite": "",
"dir_relpath": os.path.dirname(path),
"srcdir_relpath": path,
}
)
self._ac_loaded = True
def add_geckoview_junit_manifest_data(self):
if self._geckoview_junit_loaded:
return