Bug 818819 - Revamp mach's interaction with mochitests; r=Ms2ger
authorGregory Szorc <gps@mozilla.com>
Tue, 26 Mar 2013 15:00:43 -0700
changeset 126305 4edaad0bb4544e45feb8aedbe54c459d33bdace8
parent 126304 c8509229f2057a265a490d840bcc96036e7f5907
child 126306 bbaf5b6ab7b35b7c41b2cbbba7fc7ec78edce666
push id24481
push userryanvm@gmail.com
push dateWed, 27 Mar 2013 16:41:34 +0000
treeherdermozilla-central@5dbcbd03d7ba [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
bugs818819
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 818819 - Revamp mach's interaction with mochitests; r=Ms2ger mach now invokes mochitests using the native Python API (as opposed to make targets). mach now supports --shuffle, --rerun-failures, --keep-open, --no-autorun, --repeat, and --slow.
build/mach_bootstrap.py
testing/mochitest/mach_commands.py
--- a/build/mach_bootstrap.py
+++ b/build/mach_bootstrap.py
@@ -17,16 +17,18 @@ SEARCH_PATHS = [
     'python/psutil',
     'python/which',
     'build/pymake',
     'config',
     'other-licenses/ply',
     'xpcom/idl-parser',
     'testing',
     'testing/xpcshell',
+    'testing/mozbase/mozcrash',
+    'testing/mozbase/mozlog',
     'testing/mozbase/mozprocess',
     'testing/mozbase/mozfile',
     'testing/mozbase/mozinfo',
 ]
 
 # Individual files providing mach commands.
 MACH_MODULES = [
     'addon-sdk/mach_commands.py',
--- a/testing/mochitest/mach_commands.py
+++ b/testing/mochitest/mach_commands.py
@@ -1,158 +1,226 @@
 # 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/.
 
 from __future__ import unicode_literals
 
 import os
 import platform
+import sys
 
 from mozbuild.base import (
     MachCommandBase,
     MozbuildObject,
 )
 
 from mach.decorators import (
     CommandArgument,
     CommandProvider,
     Command,
 )
 
 
-generic_help = 'Test to run. Can be specified as a single file, a ' +\
-'directory, or omitted. If omitted, the entire test suite is executed.'
-
-debugger_help = 'Debugger binary to run test in. Program name or path.'
-
-
 class MochitestRunner(MozbuildObject):
     """Easily run mochitests.
 
     This currently contains just the basics for running mochitests. We may want
     to hook up result parsing, etc.
     """
-    def run_plain_suite(self):
-        """Runs all plain mochitests."""
-        # TODO hook up Python harness runner.
-        self._run_make(directory='.', target='mochitest-plain')
-
-    def run_chrome_suite(self):
-        """Runs all chrome mochitests."""
-        # TODO hook up Python harness runner.
-        self._run_make(directory='.', target='mochitest-chrome')
-
-    def run_browser_chrome_suite(self):
-        """Runs browser chrome mochitests."""
-        # TODO hook up Python harness runner.
-        self._run_make(directory='.', target='mochitest-browser-chrome')
-
-    def run_metro_chrome_suite(self):
-        """Runs Windows 8 Metro browser chrome mochitests."""
-        # TODO hook up Python harness runner.
-        self._run_make(directory='.', target='mochitest-metro-chrome')
-
-    def run_all(self):
-        self.run_plain_suite()
-        self.run_chrome_suite()
-        self.run_browser_chrome_suite()
-        # Run Metro tests only on Windows 8 and higher
-        if platform.system() is 'Windows':
-            version = tuple(map(int, platform.version().split('.')))
-            if version >= (6, 2):
-                self.run_metro_chrome_suite()
-
-    def run_mochitest_test(self, suite=None, test_file=None, debugger=None):
+    def run_mochitest_test(self, suite=None, test_file=None, debugger=None,
+        shuffle=False, keep_open=False, rerun_failures=False, no_autorun=False,
+        repeat=0, slow=False):
         """Runs a mochitest.
 
         test_file is a path to a test file. It can be a relative path from the
         top source directory, an absolute filename, or a directory containing
         test files.
 
         suite is the type of mochitest to run. It can be one of ('plain',
         'chrome', 'browser', 'metro', 'a11y').
 
         debugger is a program name or path to a binary (presumably a debugger)
         to run the test in. e.g. 'gdb'
+
+        shuffle is whether test order should be shuffled (defaults to false).
+
+        keep_open denotes whether to keep the browser open after tests
+        complete.
         """
+        # TODO Bug 794506 remove once mach integrates with virtualenv.
+        build_path = os.path.join(self.topobjdir, 'build')
+        if build_path not in sys.path:
+            sys.path.append(build_path)
+
+        if rerun_failures and test_file:
+            print('Cannot specify both --rerun-failures and a test path.')
+            return 1
+
+        # Need to perform path test before os.chdir() below.
+        if test_file:
+            path_arg = self._wrap_path_argument(test_file)
+            if not os.path.exists(path_arg.srcdir_path()):
+                print('Specified test path does not exist: %s' % test_file)
+                return 1
+
+            test_file = path_arg.relpath()
+
+        failure_file_path = os.path.join(self.statedir, 'mochitest_failures.json')
+
+        if rerun_failures and not os.path.exists(failure_file_path):
+            print('No failure file present. Did you run mochitests before?')
+            return 1
+
+        from automation import Automation
+
+        # runtests.py is ambiguous, so we load the file/module manually.
+        if 'mochitest' not in sys.modules:
+            import imp
+            path = os.path.join(self.topobjdir, '_tests', 'testing',
+                'mochitest', 'runtests.py')
+            with open(path, 'r') as fh:
+                imp.load_module('mochitest', fh, path,
+                    ('.py', 'r', imp.PY_SOURCE))
 
-        # TODO hook up harness via native Python
-        target = None
+        import mochitest
+
+        # This is required to make other components happy. Sad, isn't it?
+        os.chdir(self.topobjdir)
+
+        automation = Automation()
+        runner = mochitest.Mochitest(automation)
+
+        opts = mochitest.MochitestOptions(automation,
+            os.path.join(self.topobjdir, '_tests'))
+        options, args = opts.parse_args([])
+
+        options = opts.verifyOptions(options, runner)
+
+        if options is None:
+            raise Exception('mochitest option validator failed.')
+
+        tests_dir = os.path.join(self.topobjdir, '_tests')
+
+        if test_file:
+            env = {'TEST_PATH': test_file}
+
+        options.autorun = not no_autorun
+        options.closeWhenDone = not keep_open
+        options.shuffle = shuffle
+        options.consoleLevel = 'INFO'
+        options.repeat = repeat
+        options.runSlower = slow
+        options.testingModulesDir = os.path.join(tests_dir, 'modules')
+        options.extraProfileFiles.append(os.path.join(self.distdir, 'plugins'))
+        options.symbolsPath = os.path.join(self.distdir, 'crashreporter-symbols')
+
+        options.failureFile = failure_file_path
+
+        automation.setServerInfo(options.webServer, options.httpPort,
+            options.sslPort, options.webSocketPort)
+
         if suite == 'plain':
-            target = 'mochitest-plain'
+            # Don't need additional options for plain.
+            pass
         elif suite == 'chrome':
-            target = 'mochitest-chrome'
+            options.chrome = True
         elif suite == 'browser':
-            target = 'mochitest-browser-chrome'
+            options.browserChrome = True
         elif suite == 'metro':
-            target = 'mochitest-metro-chrome'
+            options.immersiveMode = True
+            options.browser = True
         elif suite == 'a11y':
-            target = 'mochitest-a11y'
+            options.a11y = True
         else:
             raise Exception('None or unrecognized mochitest suite type.')
 
         if test_file:
-            path_arg = self._wrap_path_argument(test_file)
-            if not os.path.exists(path_arg.srcdir_path()):
-                raise Exception('No test file was found at %s.' % test_file)
-            env = {'TEST_PATH': path_arg.relpath()}
-        else:
-            env = {}
+            options.testPath = test_file
 
-        pass_thru = False
+        if rerun_failures:
+            options.testManifest = failure_file_path
 
         if debugger:
-            env[b'EXTRA_TEST_ARGS'] = '--debugger=%s' % debugger
-            pass_thru = True
+            options.debugger = debugger
+
+        return runner.runTests(options)
+
+
+def MochitestCommand(func):
+    """Decorator that adds shared command arguments to mochitest commands."""
+
+    # This employs light Python magic. Keep in mind a decorator is just a
+    # function that takes a function, does something with it, then returns a
+    # (modified) function. Here, we chain decorators onto the passed in
+    # function.
+
+    debugger = CommandArgument('--debugger', '-d', metavar='DEBUGGER',
+        help='Debugger binary to run test in. Program name or path.')
+    func = debugger(func)
+
+    shuffle = CommandArgument('--shuffle', action='store_true',
+        help='Shuffle execution order.')
+    func = shuffle(func)
+
+    keep_open = CommandArgument('--keep-open', action='store_true',
+        help='Keep the browser open after tests complete.')
+    func = keep_open(func)
 
-        return self._run_make(directory='.', target=target, append_env=env,
-            ensure_exit_code=False, pass_thru=pass_thru)
+    rerun = CommandArgument('--rerun-failures', action='store_true',
+        help='Run only the tests that filed during the last test run.')
+    func = rerun(func)
+
+    autorun = CommandArgument('--no-autorun', action='store_true',
+        help='Do not starting running tests automatically.')
+    func = autorun(func)
+
+    repeat = CommandArgument('--repeat', type=int, default=0,
+        help='Repeat the test the given number of times.')
+    func = repeat(func)
+
+    slow = CommandArgument('--slow', action='store_true',
+        help='Delay execution between tests.')
+    func = slow(func)
+
+    path = CommandArgument('test_file', default=None, nargs='?',
+        metavar='TEST',
+        help='Test to run. Can be specified as a single file, a ' \
+            'directory, or omitted. If omitted, the entire test suite is ' \
+            'executed.')
+    func = path(func)
+
+    return func
 
 
 @CommandProvider
 class MachCommands(MachCommandBase):
     @Command('mochitest-plain', help='Run a plain mochitest.')
-    @CommandArgument('--debugger', '-d', metavar='DEBUGGER',
-        help=debugger_help)
-    @CommandArgument('test_file', default=None, nargs='?', metavar='TEST',
-        help=generic_help)
-    def run_mochitest_plain(self, test_file, debugger=None):
-        return self.run_mochitest(test_file, 'plain', debugger=debugger)
+    @MochitestCommand
+    def run_mochitest_plain(self, test_file, **kwargs):
+        return self.run_mochitest(test_file, 'plain', **kwargs)
 
     @Command('mochitest-chrome', help='Run a chrome mochitest.')
-    @CommandArgument('--debugger', '-d', metavar='DEBUGGER',
-        help=debugger_help)
-    @CommandArgument('test_file', default=None, nargs='?', metavar='TEST',
-        help=generic_help)
-    def run_mochitest_chrome(self, test_file, debugger=None):
-        return self.run_mochitest(test_file, 'chrome', debugger=debugger)
+    @MochitestCommand
+    def run_mochitest_chrome(self, test_file, **kwargs):
+        return self.run_mochitest(test_file, 'chrome', **kwargs)
 
     @Command('mochitest-browser', help='Run a mochitest with browser chrome.')
-    @CommandArgument('--debugger', '-d', metavar='DEBUGGER',
-        help=debugger_help)
-    @CommandArgument('test_file', default=None, nargs='?', metavar='TEST',
-        help=generic_help)
-    def run_mochitest_browser(self, test_file, debugger=None):
-        return self.run_mochitest(test_file, 'browser', debugger=debugger)
+    @MochitestCommand
+    def run_mochitest_browser(self, test_file, **kwargs):
+        return self.run_mochitest(test_file, 'browser', **kwargs)
 
     @Command('mochitest-metro', help='Run a mochitest with metro browser chrome.')
-    @CommandArgument('--debugger', '-d', metavar='DEBUGGER',
-        help=debugger_help)
-    @CommandArgument('test_file', default=None, nargs='?', metavar='TEST',
-        help=generic_help)
-    def run_mochitest_metro(self, test_file, debugger=None):
-        return self.run_mochitest(test_file, 'metro', debugger=debugger)
+    @MochitestCommand
+    def run_mochitest_metro(self, test_file, **kwargs):
+        return self.run_mochitest(test_file, 'metro', **kwargs)
 
     @Command('mochitest-a11y', help='Run an a11y mochitest.')
-    @CommandArgument('--debugger', '-d', metavar='DEBUGGER',
-        help=debugger_help)
-    @CommandArgument('test_file', default=None, nargs='?', metavar='TEST',
-        help=generic_help)
-    def run_mochitest_a11y(self, test_file, debugger=None):
-        return self.run_mochitest(test_file, 'a11y', debugger=debugger)
+    @MochitestCommand
+    def run_mochitest_a11y(self, test_file, **kwargs):
+        return self.run_mochitest(test_file, 'a11y', **kwargs)
 
-    def run_mochitest(self, test_file, flavor, debugger=None):
+    def run_mochitest(self, test_file, flavor, **kwargs):
         self._ensure_state_subdir_exists('.')
 
         mochitest = self._spawn(MochitestRunner)
         return mochitest.run_mochitest_test(test_file=test_file, suite=flavor,
-            debugger=debugger)
+            **kwargs)