From abb872231bb1f0b06519f5f2a63e97f6705db95e Mon Sep 17 00:00:00 2001 From: Geoff Brown Date: Mon, 14 Sep 2015 08:40:35 -0600 Subject: [PATCH] Bug 1090276 - Support mach cppunittest on Android; r=dminor --- testing/mach_commands.py | 69 +++++++++++++++++++++++++++++------ testing/remotecppunittests.py | 57 ++++++++++++++++------------- testing/runcppunittests.py | 22 ++++++----- 3 files changed, 102 insertions(+), 46 deletions(-) diff --git a/testing/mach_commands.py b/testing/mach_commands.py index 9134d9d2062..9173e61ce1c 100644 --- a/testing/mach_commands.py +++ b/testing/mach_commands.py @@ -18,6 +18,7 @@ from mach.decorators import ( ) from mozbuild.base import MachCommandBase +from mozbuild.base import MachCommandConditions as conditions from argparse import ArgumentParser @@ -293,30 +294,76 @@ class MachCommands(MachCommandBase): def run_cppunit_test(self, **params): import mozinfo from mozlog import commandline - import runcppunittests as cppunittests - log = commandline.setup_logging("cppunittest", {}, {"tbpl": sys.stdout}) - if len(params['test_files']) == 0: - testdir = os.path.join(self.distdir, 'cppunittests') - manifest = os.path.join(self.topsrcdir, 'testing', 'cppunittest.ini') - tests = cppunittests.extract_unittests_from_args([testdir], mozinfo.info, manifest) - else: - tests = cppunittests.extract_unittests_from_args(params['test_files'], mozinfo.info, None) - # See if we have crash symbols symbols_path = os.path.join(self.distdir, 'crashreporter-symbols') if not os.path.isdir(symbols_path): symbols_path = None - tester = cppunittests.CPPUnitTests() + # If no tests specified, run all tests in main manifest + tests = params['test_files'] + if len(tests) == 0: + tests = [os.path.join(self.distdir, 'cppunittests')] + manifest_path = os.path.join(self.topsrcdir, 'testing', 'cppunittest.ini') + else: + manifest_path = None + + if conditions.is_android(self): + from mozrunner.devices.android_device import verify_android_device + verify_android_device(self, install=False) + return self.run_android_test(tests, symbols_path, manifest_path, log) + + return self.run_desktop_test(tests, symbols_path, manifest_path, log) + + def run_desktop_test(self, tests, symbols_path, manifest_path, log): + import runcppunittests as cppunittests + from mozlog import commandline + + parser = cppunittests.CPPUnittestOptions() + commandline.add_logging_group(parser) + options, args = parser.parse_args() + + options.symbols_path = symbols_path + options.manifest_path = manifest_path + options.xre_path = self.bindir + try: - result = tester.run_tests(tests, self.bindir, symbols_path, interactive=True) + result = cppunittests.run_test_harness(options, tests) except Exception as e: log.error("Caught exception running cpp unit tests: %s" % str(e)) result = False + raise + + return 0 if result else 1 + + def run_android_test(self, tests, symbols_path, manifest_path, log): + import remotecppunittests as remotecppunittests + from mozlog import commandline + + parser = remotecppunittests.RemoteCPPUnittestOptions() + commandline.add_logging_group(parser) + options, args = parser.parse_args() + + options.symbols_path = symbols_path + options.manifest_path = manifest_path + options.xre_path = self.bindir + options.dm_trans = "adb" + options.local_lib = self.bindir.replace('bin', 'fennec') + for file in os.listdir(os.path.join(self.topobjdir, "dist")): + if file.endswith(".apk") and file.startswith("fennec"): + options.local_apk = os.path.join(self.topobjdir, "dist", file) + log.info("using APK: " + options.local_apk) + break + + try: + result = remotecppunittests.run_test_harness(options, tests) + except Exception as e: + log.error("Caught exception running cpp unit tests: %s" % str(e)) + result = False + raise return 0 if result else 1 diff --git a/testing/remotecppunittests.py b/testing/remotecppunittests.py index cb007770a80..821d556052c 100644 --- a/testing/remotecppunittests.py +++ b/testing/remotecppunittests.py @@ -220,13 +220,14 @@ class RemoteCPPUnittestOptions(cppunittests.CPPUnittestOptions): self.add_option("--remoteTestRoot", action = "store", type = "string", dest = "remote_test_root", help = "remote directory to use as test root (eg. /data/local/tests)") - self.add_option("--with-b2g-emulator", action = "store", - type = "string", dest = "with_b2g_emulator", - help = "Start B2G Emulator (specify path to b2g home)") # /data/local/tests is used because it is usually not possible to set +x permissions # on binaries on /mnt/sdcard defaults["remote_test_root"] = "/data/local/tests" + self.add_option("--with-b2g-emulator", action = "store", + type = "string", dest = "with_b2g_emulator", + help = "Start B2G Emulator (specify path to b2g home)") + self.add_option("--addEnv", action = "append", type = "string", dest = "add_env", help = "additional remote environment variable definitions (eg. --addEnv \"somevar=something\")") @@ -234,22 +235,7 @@ class RemoteCPPUnittestOptions(cppunittests.CPPUnittestOptions): self.set_defaults(**defaults) -def main(): - parser = RemoteCPPUnittestOptions() - mozlog.commandline.add_logging_group(parser) - options, args = parser.parse_args() - if not args: - print >>sys.stderr, """Usage: %s [...]""" % sys.argv[0] - sys.exit(1) - if options.local_lib is not None and not os.path.isdir(options.local_lib): - print >>sys.stderr, """Error: --localLib directory %s not found""" % options.local_lib - sys.exit(1) - if options.local_apk is not None and not os.path.isfile(options.local_apk): - print >>sys.stderr, """Error: --apk file %s not found""" % options.local_apk - sys.exit(1) - if not options.xre_path: - print >>sys.stderr, """Error: --xre-path is required""" - sys.exit(1) +def run_test_harness(options, args): if options.with_b2g_emulator: from mozrunner import B2GEmulatorRunner runner = B2GEmulatorRunner(b2g_home=options.with_b2g_emulator) @@ -277,9 +263,6 @@ def main(): print "Error: you must provide a device IP to connect to via the --deviceIP option" sys.exit(1) - log = mozlog.commandline.setup_logging("remotecppunittests", options, - {"tbpl": sys.stdout}) - options.xre_path = os.path.abspath(options.xre_path) cppunittests.update_mozinfo() progs = cppunittests.extract_unittests_from_args(args, @@ -288,12 +271,36 @@ def main(): tester = RemoteCPPUnitTests(dm, options, [item[0] for item in progs]) try: result = tester.run_tests(progs, options.xre_path, options.symbols_path) + finally: + if options.with_b2g_emulator: + runner.cleanup() + runner.wait() + return result + +def main(): + parser = RemoteCPPUnittestOptions() + mozlog.commandline.add_logging_group(parser) + options, args = parser.parse_args() + if not args: + print >>sys.stderr, """Usage: %s [...]""" % sys.argv[0] + sys.exit(1) + if options.local_lib is not None and not os.path.isdir(options.local_lib): + print >>sys.stderr, """Error: --localLib directory %s not found""" % options.local_lib + sys.exit(1) + if options.local_apk is not None and not os.path.isfile(options.local_apk): + print >>sys.stderr, """Error: --apk file %s not found""" % options.local_apk + sys.exit(1) + if not options.xre_path: + print >>sys.stderr, """Error: --xre-path is required""" + sys.exit(1) + + log = mozlog.commandline.setup_logging("remotecppunittests", options, + {"tbpl": sys.stdout}) + try: + result = run_test_harness(options, args) except Exception, e: log.error(str(e)) result = False - if options.with_b2g_emulator: - runner.cleanup() - runner.wait() sys.exit(0 if result else 1) if __name__ == '__main__': diff --git a/testing/runcppunittests.py b/testing/runcppunittests.py index d22a58524a7..6629d995736 100644 --- a/testing/runcppunittests.py +++ b/testing/runcppunittests.py @@ -227,6 +227,17 @@ def update_mozinfo(): path = os.path.split(path)[0] mozinfo.find_and_update_from_json(*dirs) +def run_test_harness(options, args): + update_mozinfo() + progs = extract_unittests_from_args(args, mozinfo.info, options.manifest_path) + options.xre_path = os.path.abspath(options.xre_path) + if mozinfo.isMac: + options.xre_path = os.path.join(os.path.dirname(options.xre_path), 'Resources') + tester = CPPUnitTests() + result = tester.run_tests(progs, options.xre_path, options.symbols_path) + + return result + def main(): parser = CPPUnittestOptions() mozlog.commandline.add_logging_group(parser) @@ -240,19 +251,10 @@ def main(): if options.manifest_path and len(args) > 1: print >>sys.stderr, "Error: multiple arguments not supported with --test-manifest" sys.exit(1) - log = mozlog.commandline.setup_logging("cppunittests", options, {"tbpl": sys.stdout}) - - update_mozinfo() - progs = extract_unittests_from_args(args, mozinfo.info, options.manifest_path) - options.xre_path = os.path.abspath(options.xre_path) - if mozinfo.isMac: - options.xre_path = os.path.join(os.path.dirname(options.xre_path), 'Resources') - tester = CPPUnitTests() - try: - result = tester.run_tests(progs, options.xre_path, options.symbols_path) + result = run_test_harness(options, args) except Exception as e: log.error(str(e)) result = False