Skip to content

Commit

Permalink
bug 1255479 - make mach python-tests use TestResolver for discovery…
Browse files Browse the repository at this point in the history
…, make `mach test` work for python tests. r=nalexander

MozReview-Commit-ID: CK2Vh6gdnb0
  • Loading branch information
luser committed Mar 10, 2016
1 parent 62dcadf commit 3283e68
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 77 deletions.
63 changes: 34 additions & 29 deletions build/compare-mozconfig/compare-mozconfigs-wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

from __future__ import unicode_literals

import logging
import mozunit
import subprocess
import sys
import unittest

from os import path
from buildconfig import substs

import logging
log = logging.getLogger(__name__)

def determine_platform():
Expand All @@ -25,39 +28,41 @@ def determine_platform():
cpu_type = substs['TARGET_CPU']
return platform_mapping.get(os_type, {}).get(cpu_type, None)

def main():
""" A wrapper script that calls compare-mozconfig.py
based on the platform that the machine is building for"""
platform = determine_platform()

if platform is not None:
python_exe = substs['PYTHON']
topsrcdir = substs['top_srcdir']
class TestCompareMozconfigs(unittest.TestCase):
def test_compare_mozconfigs(self):
""" A wrapper script that calls compare-mozconfig.py
based on the platform that the machine is building for"""
platform = determine_platform()

if platform is not None:
python_exe = substs['PYTHON']
topsrcdir = substs['top_srcdir']

# construct paths and args for compare-mozconfig
browser_dir = path.join(topsrcdir, 'browser')
script_path = path.join(topsrcdir, 'build/compare-mozconfig/compare-mozconfigs.py')
whitelist_path = path.join(browser_dir, 'config/mozconfigs/whitelist')
beta_mozconfig_path = path.join(browser_dir, 'config/mozconfigs', platform, 'beta')
release_mozconfig_path = path.join(browser_dir, 'config/mozconfigs', platform, 'release')
nightly_mozconfig_path = path.join(browser_dir, 'config/mozconfigs', platform, 'nightly')
# construct paths and args for compare-mozconfig
browser_dir = path.join(topsrcdir, 'browser')
script_path = path.join(topsrcdir, 'build/compare-mozconfig/compare-mozconfigs.py')
whitelist_path = path.join(browser_dir, 'config/mozconfigs/whitelist')
beta_mozconfig_path = path.join(browser_dir, 'config/mozconfigs', platform, 'beta')
release_mozconfig_path = path.join(browser_dir, 'config/mozconfigs', platform, 'release')
nightly_mozconfig_path = path.join(browser_dir, 'config/mozconfigs', platform, 'nightly')

log.info("Comparing beta against nightly mozconfigs")
ret_code = subprocess.call([python_exe, script_path, '--whitelist',
whitelist_path, '--no-download',
platform + ',' + beta_mozconfig_path +
',' + nightly_mozconfig_path])
log.info("Comparing beta against nightly mozconfigs")
ret_code = subprocess.call([python_exe, script_path, '--whitelist',
whitelist_path, '--no-download',
platform + ',' + beta_mozconfig_path +
',' + nightly_mozconfig_path])

if ret_code > 0:
return ret_code
if ret_code > 0:
return ret_code

log.info("Comparing release against nightly mozconfigs")
ret_code = subprocess.call([python_exe, script_path, '--whitelist',
whitelist_path, '--no-download',
platform + ',' + release_mozconfig_path +
',' + nightly_mozconfig_path])
log.info("Comparing release against nightly mozconfigs")
ret_code = subprocess.call([python_exe, script_path, '--whitelist',
whitelist_path, '--no-download',
platform + ',' + release_mozconfig_path +
',' + nightly_mozconfig_path])

return ret_code
self.assertEqual(0, ret_code)

if __name__ == '__main__':
sys.exit(main())
mozunit.main()
54 changes: 22 additions & 32 deletions python/mach_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,38 @@ def python(self, args):
default=False,
action='store_true',
help='Stop running tests after the first error or failure.')
@CommandArgument('tests', nargs='+',
@CommandArgument('tests', nargs='*',
metavar='TEST',
help='Tests to run. Each test can be a single file or a directory.')
def python_test(self, tests, verbose=False, stop=False):
def python_test(self,
tests=[],
test_objects=None,
subsuite=None,
verbose=False,
stop=False):
self._activate_virtualenv()
import glob

# Python's unittest, and in particular discover, has problems with
# clashing namespaces when importing multiple test modules. What follows
# is a simple way to keep environments separate, at the price of
# launching Python multiple times. This also runs tests via mozunit,
# which produces output in the format Mozilla infrastructure expects.
return_code = 0
files = []
# We search for files in both the current directory (for people running
# from topsrcdir or cd'd into their test directory) and topsrcdir (to
# support people running mach from the objdir). The |break|s in the
# loop below ensure that we don't run tests twice if we're running mach
# from topsrcdir
search_dirs = ['.', self.topsrcdir]
last_search_dir = search_dirs[-1]
for t in tests:
for d in search_dirs:
test = mozpath.join(d, t)
if test.endswith('.py') and os.path.isfile(test):
files.append(test)
break
elif os.path.isfile(test + '.py'):
files.append(test + '.py')
break
elif os.path.isdir(test):
files += glob.glob(mozpath.join(test, 'test*.py'))
files += glob.glob(mozpath.join(test, 'unit*.py'))
break
elif d == last_search_dir:
self.log(logging.WARN, 'python-test',
{'test': t},
'TEST-UNEXPECTED-FAIL | Invalid test: {test}')
if stop:
return 1

for f in files:
if test_objects is None:
# If we're not being called from `mach test`, do our own
# test resolution.
from mozbuild.testing import TestResolver
resolver = self._spawn(TestResolver)
if tests:
# If we were given test paths, try to find tests matching them.
test_objects = resolver.resolve_tests(paths=tests,
flavor='python')
else:
# Otherwise just run all Python tests.
test_objects = resolver.resolve_tests(flavor='python')

for test in test_objects:
f = test['path']
file_displayed_test = [] # Used as a boolean.

def _line_handler(line):
Expand Down
11 changes: 0 additions & 11 deletions python/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,24 @@ SPHINX_PYTHON_PACKAGE_DIRS += [
SPHINX_TREES['mach'] = 'mach/docs'

PYTHON_UNIT_TESTS += [
'mach/mach/test/__init__.py',
'mach/mach/test/common.py',
'mach/mach/test/test_conditions.py',
'mach/mach/test/test_config.py',
'mach/mach/test/test_entry_point.py',
'mach/mach/test/test_error_output.py',
'mach/mach/test/test_logger.py',
'mozbuild/dumbmake/test/__init__.py',
'mozbuild/dumbmake/test/test_dumbmake.py',
'mozbuild/mozbuild/test/__init__.py',
'mozbuild/mozbuild/test/action/test_buildlist.py',
'mozbuild/mozbuild/test/action/test_generate_browsersearch.py',
'mozbuild/mozbuild/test/backend/__init__.py',
'mozbuild/mozbuild/test/backend/common.py',
'mozbuild/mozbuild/test/backend/test_android_eclipse.py',
'mozbuild/mozbuild/test/backend/test_build.py',
'mozbuild/mozbuild/test/backend/test_configenvironment.py',
'mozbuild/mozbuild/test/backend/test_recursivemake.py',
'mozbuild/mozbuild/test/backend/test_visualstudio.py',
'mozbuild/mozbuild/test/common.py',
'mozbuild/mozbuild/test/compilation/__init__.py',
'mozbuild/mozbuild/test/compilation/test_warnings.py',
'mozbuild/mozbuild/test/configure/test_configure.py',
'mozbuild/mozbuild/test/configure/test_options.py',
'mozbuild/mozbuild/test/controller/__init__.py',
'mozbuild/mozbuild/test/controller/test_ccachestats.py',
'mozbuild/mozbuild/test/controller/test_clobber.py',
'mozbuild/mozbuild/test/frontend/__init__.py',
'mozbuild/mozbuild/test/frontend/test_context.py',
'mozbuild/mozbuild/test/frontend/test_emitter.py',
'mozbuild/mozbuild/test/frontend/test_namespaces.py',
Expand All @@ -66,7 +56,6 @@ PYTHON_UNIT_TESTS += [
'mozbuild/mozbuild/test/test_pythonutil.py',
'mozbuild/mozbuild/test/test_testing.py',
'mozbuild/mozbuild/test/test_util.py',
'mozbuild/mozpack/test/__init__.py',
'mozbuild/mozpack/test/test_chrome_flags.py',
'mozbuild/mozpack/test/test_chrome_manifest.py',
'mozbuild/mozpack/test/test_copier.py',
Expand Down
8 changes: 8 additions & 0 deletions testing/mach_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
'mach_command': 'luciddream',
'kwargs': {'test_paths': None},
},
'python': {
'mach_command': 'python-test',
'kwargs': {'tests': None},
},
'reftest': {
'aliases': ('RR', 'rr', 'Rr'),
'mach_command': 'reftest',
Expand Down Expand Up @@ -146,6 +150,10 @@
'mach_command': 'mochitest',
'kwargs': {'flavor': 'mochitest', 'test_paths': []},
},
'python': {
'mach_command': 'python-test',
'kwargs': {},
},
'reftest': {
'mach_command': 'reftest',
'kwargs': {'tests': []}
Expand Down
13 changes: 9 additions & 4 deletions testing/xpcshell/selftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
# http://creativecommons.org/publicdomain/zero/1.0/
#

from __future__ import with_statement
import sys, os, unittest, tempfile, shutil, re, pprint
import mozinfo
import mozunit
import os
import pprint
import re
import shutil
import sys
import tempfile
import unittest

from StringIO import StringIO

from mozlog import structured
from mozbuild.base import MozbuildObject
os.environ.pop('MOZ_OBJDIR', None)
Expand Down Expand Up @@ -1341,4 +1346,4 @@ def testChildMozinfo(self):
self.assertNotInLog(TEST_FAIL_STRING)

if __name__ == "__main__":
unittest.main(verbosity=3)
mozunit.main()
3 changes: 2 additions & 1 deletion toolkit/crashreporter/tools/unit-symbolstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import concurrent.futures
import mock
import mozunit
import os
import platform
import shutil
Expand Down Expand Up @@ -533,5 +534,5 @@ def testSymbolstore(self):
# that our mocking/module-patching works.
symbolstore.Dumper.GlobalInit(concurrent.futures.ThreadPoolExecutor)

unittest.main()
mozunit.main()

0 comments on commit 3283e68

Please sign in to comment.