2012-09-26 09:43:53 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
2012-10-10 11:08:09 -07:00
|
|
|
# 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/.
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2012-10-10 11:08:09 -07:00
|
|
|
# Integrates the xpcshell test runner with mach.
|
2012-09-26 09:43:53 -07:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-03-06 20:41:22 -08:00
|
|
|
import mozpack.path
|
2012-10-10 11:08:09 -07:00
|
|
|
import os
|
2012-11-14 10:05:18 -08:00
|
|
|
import sys
|
2012-09-26 09:43:53 -07:00
|
|
|
|
|
|
|
from StringIO import StringIO
|
|
|
|
|
2012-11-06 16:57:41 -08:00
|
|
|
from mozbuild.base import (
|
|
|
|
MachCommandBase,
|
|
|
|
MozbuildObject,
|
|
|
|
)
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2012-11-06 16:57:41 -08:00
|
|
|
from mach.decorators import (
|
2012-10-10 11:08:09 -07:00
|
|
|
CommandArgument,
|
|
|
|
CommandProvider,
|
|
|
|
Command,
|
|
|
|
)
|
|
|
|
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2012-11-14 10:05:18 -08:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
unicode_type = unicode
|
|
|
|
else:
|
|
|
|
unicode_type = str
|
|
|
|
|
|
|
|
|
2012-11-28 12:19:32 -08:00
|
|
|
# This should probably be consolidated with similar classes in other test
|
|
|
|
# runners.
|
|
|
|
class InvalidTestPathError(Exception):
|
|
|
|
"""Exception raised when the test path is not valid."""
|
|
|
|
|
|
|
|
|
2012-09-26 09:43:53 -07:00
|
|
|
class XPCShellRunner(MozbuildObject):
|
|
|
|
"""Run xpcshell tests."""
|
2012-10-31 10:29:26 -07:00
|
|
|
def run_suite(self, **kwargs):
|
|
|
|
manifest = os.path.join(self.topobjdir, '_tests', 'xpcshell',
|
|
|
|
'xpcshell.ini')
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2012-12-05 15:42:11 -08:00
|
|
|
return self._run_xpcshell_harness(manifest=manifest, **kwargs)
|
2012-10-31 10:29:26 -07:00
|
|
|
|
|
|
|
def run_test(self, test_file, debug=False, interactive=False,
|
|
|
|
keep_going=False, shuffle=False):
|
2012-09-26 09:43:53 -07:00
|
|
|
"""Runs an individual xpcshell test."""
|
2012-10-31 10:29:26 -07:00
|
|
|
|
2012-09-26 09:43:53 -07:00
|
|
|
if test_file == 'all':
|
2012-10-31 10:29:26 -07:00
|
|
|
self.run_suite(debug=debug, interactive=interactive,
|
|
|
|
keep_going=keep_going, shuffle=shuffle)
|
2012-09-26 09:43:53 -07:00
|
|
|
return
|
|
|
|
|
2013-03-14 11:09:00 -07:00
|
|
|
path_arg = self._wrap_path_argument(test_file)
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2013-03-14 11:09:00 -07:00
|
|
|
test_obj_dir = os.path.join(self.topobjdir, '_tests', 'xpcshell',
|
|
|
|
path_arg.relpath())
|
|
|
|
if os.path.isfile(test_obj_dir):
|
|
|
|
test_obj_dir = mozpack.path.dirname(test_obj_dir)
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2013-03-06 20:41:22 -08:00
|
|
|
xpcshell_ini_file = mozpack.path.join(test_obj_dir, 'xpcshell.ini')
|
2012-11-28 12:19:32 -08:00
|
|
|
if not os.path.exists(xpcshell_ini_file):
|
|
|
|
raise InvalidTestPathError('An xpcshell.ini could not be found '
|
|
|
|
'for the passed test path. Please select a path whose '
|
|
|
|
'directory contains an xpcshell.ini file. It is possible you '
|
|
|
|
'received this error because the tree is not built or tests '
|
|
|
|
'are not enabled.')
|
|
|
|
|
2012-09-26 09:43:53 -07:00
|
|
|
args = {
|
|
|
|
'debug': debug,
|
2012-10-31 10:29:26 -07:00
|
|
|
'interactive': interactive,
|
|
|
|
'keep_going': keep_going,
|
|
|
|
'shuffle': shuffle,
|
2013-03-06 20:41:22 -08:00
|
|
|
'test_dirs': [test_obj_dir],
|
2012-09-26 09:43:53 -07:00
|
|
|
}
|
|
|
|
|
2013-03-14 11:09:00 -07:00
|
|
|
if os.path.isfile(path_arg.srcdir_path()):
|
|
|
|
args['test_path'] = mozpack.path.basename(path_arg.srcdir_path())
|
2012-09-26 09:43:53 -07:00
|
|
|
|
2012-12-05 15:42:11 -08:00
|
|
|
return self._run_xpcshell_harness(**args)
|
2012-09-26 09:43:53 -07:00
|
|
|
|
|
|
|
def _run_xpcshell_harness(self, test_dirs=None, manifest=None,
|
2012-10-31 10:29:26 -07:00
|
|
|
test_path=None, debug=False, shuffle=False, interactive=False,
|
|
|
|
keep_going=False):
|
2012-09-26 09:43:53 -07:00
|
|
|
|
|
|
|
# Obtain a reference to the xpcshell test runner.
|
|
|
|
import runxpcshelltests
|
|
|
|
|
|
|
|
dummy_log = StringIO()
|
|
|
|
xpcshell = runxpcshelltests.XPCShellTests(log=dummy_log)
|
|
|
|
self.log_manager.enable_unstructured()
|
|
|
|
|
|
|
|
tests_dir = os.path.join(self.topobjdir, '_tests', 'xpcshell')
|
|
|
|
modules_dir = os.path.join(self.topobjdir, '_tests', 'modules')
|
|
|
|
|
|
|
|
args = {
|
|
|
|
'xpcshell': os.path.join(self.bindir, 'xpcshell'),
|
|
|
|
'mozInfo': os.path.join(self.topobjdir, 'mozinfo.json'),
|
|
|
|
'symbolsPath': os.path.join(self.distdir, 'crashreporter-symbols'),
|
2012-10-31 10:29:26 -07:00
|
|
|
'interactive': interactive,
|
|
|
|
'keepGoing': keep_going,
|
2012-09-26 09:43:53 -07:00
|
|
|
'logfiles': False,
|
2012-10-31 10:29:26 -07:00
|
|
|
'shuffle': shuffle,
|
2012-09-26 09:43:53 -07:00
|
|
|
'testsRootDir': tests_dir,
|
|
|
|
'testingModulesDir': modules_dir,
|
|
|
|
'profileName': 'firefox',
|
|
|
|
'verbose': test_path is not None,
|
2012-10-31 10:29:26 -07:00
|
|
|
'xunitFilename': os.path.join(self.statedir, 'xpchsell.xunit.xml'),
|
|
|
|
'xunitName': 'xpcshell',
|
2013-02-19 09:13:38 -08:00
|
|
|
'pluginsPath': os.path.join(self.distdir, 'plugins'),
|
2012-09-26 09:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if manifest is not None:
|
|
|
|
args['manifest'] = manifest
|
|
|
|
elif test_dirs is not None:
|
|
|
|
if isinstance(test_dirs, list):
|
|
|
|
args['testdirs'] = test_dirs
|
|
|
|
else:
|
|
|
|
args['testdirs'] = [test_dirs]
|
|
|
|
else:
|
|
|
|
raise Exception('One of test_dirs or manifest must be provided.')
|
|
|
|
|
|
|
|
if test_path is not None:
|
|
|
|
args['testPath'] = test_path
|
|
|
|
|
2012-11-14 10:05:18 -08:00
|
|
|
# Python through 2.7.2 has issues with unicode in some of the
|
|
|
|
# arguments. Work around that.
|
|
|
|
filtered_args = {}
|
|
|
|
for k, v in args.items():
|
|
|
|
if isinstance(v, unicode_type):
|
|
|
|
v = v.encode('utf-8')
|
|
|
|
|
|
|
|
if isinstance(k, unicode_type):
|
|
|
|
k = k.encode('utf-8')
|
|
|
|
|
|
|
|
filtered_args[k] = v
|
|
|
|
|
2012-12-05 15:42:11 -08:00
|
|
|
result = xpcshell.runTests(**filtered_args)
|
2012-09-26 09:43:53 -07:00
|
|
|
|
|
|
|
self.log_manager.disable_unstructured()
|
2012-10-10 11:08:09 -07:00
|
|
|
|
2012-12-05 15:42:11 -08:00
|
|
|
return int(not result)
|
|
|
|
|
2012-10-10 11:08:09 -07:00
|
|
|
|
|
|
|
@CommandProvider
|
2012-11-06 16:57:41 -08:00
|
|
|
class MachCommands(MachCommandBase):
|
2012-10-10 11:08:09 -07:00
|
|
|
@Command('xpcshell-test', help='Run an xpcshell test.')
|
|
|
|
@CommandArgument('test_file', default='all', nargs='?', metavar='TEST',
|
|
|
|
help='Test to run. Can be specified as a single JS file, a directory, '
|
|
|
|
'or omitted. If omitted, the entire test suite is executed.')
|
|
|
|
@CommandArgument('--debug', '-d', action='store_true',
|
|
|
|
help='Run test in a debugger.')
|
2012-10-31 10:29:26 -07:00
|
|
|
@CommandArgument('--interactive', '-i', action='store_true',
|
|
|
|
help='Open an xpcshell prompt before running tests.')
|
|
|
|
@CommandArgument('--keep-going', '-k', action='store_true',
|
|
|
|
help='Continue running tests after a SIGINT is received.')
|
|
|
|
@CommandArgument('--shuffle', '-s', action='store_true',
|
|
|
|
help='Randomize the execution order of tests.')
|
2012-10-10 11:08:09 -07:00
|
|
|
def run_xpcshell_test(self, **params):
|
2012-11-02 10:32:40 -07:00
|
|
|
# We should probably have a utility function to ensure the tree is
|
|
|
|
# ready to run tests. Until then, we just create the state dir (in
|
|
|
|
# case the tree wasn't built with mach).
|
|
|
|
self._ensure_state_subdir_exists('.')
|
|
|
|
|
2012-10-10 11:08:09 -07:00
|
|
|
xpcshell = self._spawn(XPCShellRunner)
|
2012-11-28 12:19:32 -08:00
|
|
|
|
|
|
|
try:
|
|
|
|
return xpcshell.run_test(**params)
|
|
|
|
except InvalidTestPathError as e:
|
|
|
|
print(e.message)
|
|
|
|
return 1
|