Bug 1339178 - Add python/mach_commands.py and config/mozunit.py to flake8 linter, r=davehunt

MozReview-Commit-ID: Gcsz6z8MeOi

--HG--
extra : rebase_source : 121c9007d336cecbd068075dc54d66cc06e8334e
This commit is contained in:
Andrew Halberstadt 2017-08-29 14:41:38 -04:00
parent da6a85e6d9
commit 74afe80557
3 changed files with 38 additions and 31 deletions

View File

@ -2,25 +2,25 @@
# 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 unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
import unittest
import inspect
from StringIO import StringIO
import os
import sys
import unittest
from StringIO import StringIO
from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
'''Helper to make python unit tests report the way that the Mozilla
unit test infrastructure expects tests to report.
Usage:
import unittest
import mozunit
if __name__ == '__main__':
mozunit.main()
'''
class _MozTestResult(_TestResult):
def __init__(self, stream, descriptions):
_TestResult.__init__(self)
@ -68,7 +68,7 @@ class _MozTestResult(_TestResult):
def addFailure(self, test, err):
_TestResult.addFailure(self, test, err)
self.printFail(test,err)
self.printFail(test, err)
self.stream.writeln("FAIL: {0}".format(self.getDescription(test)))
self.stream.writeln(self.failures[-1][1])
@ -87,13 +87,15 @@ class _MozTestResult(_TestResult):
class MozTestRunner(_TestRunner):
def _makeResult(self):
return _MozTestResult(self.stream, self.descriptions)
def run(self, test):
result = self._makeResult()
test(result)
return result
class MockedFile(StringIO):
def __init__(self, context, filename, content = ''):
def __init__(self, context, filename, content=''):
self.context = context
self.name = filename
StringIO.__init__(self, content)
@ -108,6 +110,7 @@ class MockedFile(StringIO):
def __exit__(self, type, value, traceback):
self.close()
def normcase(path):
'''
Normalize the case of `path`.
@ -119,6 +122,7 @@ def normcase(path):
return path.lower()
return path
class MockedOpen(object):
'''
Context manager diverting the open builtin such that opening files
@ -138,12 +142,12 @@ class MockedOpen(object):
f.write('foo')
self.assertRaises(Exception,f.open('foo', 'r'))
'''
def __init__(self, files = {}):
def __init__(self, files={}):
self.files = {}
for name, content in files.iteritems():
self.files[normcase(os.path.abspath(name))] = content
def __call__(self, name, mode = 'r'):
def __call__(self, name, mode='r'):
absname = normcase(os.path.abspath(name))
if 'w' in mode:
file = MockedFile(self, absname)
@ -203,5 +207,6 @@ class MockedOpen(object):
return self._orig_path_exists(p)
def main(*args, **kwargs):
unittest.main(testRunner=MozTestRunner(), *args, **kwargs)

View File

@ -34,7 +34,7 @@ from mach.decorators import (
@CommandProvider
class MachCommands(MachCommandBase):
@Command('python', category='devenv',
description='Run Python.')
description='Run Python.')
@CommandArgument('args', nargs=argparse.REMAINDER)
def python(self, args):
# Avoid logging the command
@ -43,33 +43,33 @@ class MachCommands(MachCommandBase):
self._activate_virtualenv()
return self.run_process([self.virtualenv_manager.python_path] + args,
pass_thru=True, # Allow user to run Python interactively.
ensure_exit_code=False, # Don't throw on non-zero exit code.
# Note: subprocess requires native strings in os.environ on Windows
append_env={b'PYTHONDONTWRITEBYTECODE': str('1')})
pass_thru=True, # Allow user to run Python interactively.
ensure_exit_code=False, # Don't throw on non-zero exit code.
# Note: subprocess requires native strings in os.environ on Windows
append_env={b'PYTHONDONTWRITEBYTECODE': str('1')})
@Command('python-test', category='testing',
description='Run Python unit tests with an appropriate test runner.')
description='Run Python unit tests with an appropriate test runner.')
@CommandArgument('--verbose',
default=False,
action='store_true',
help='Verbose output.')
default=False,
action='store_true',
help='Verbose output.')
@CommandArgument('--stop',
default=False,
action='store_true',
help='Stop running tests after the first error or failure.')
default=False,
action='store_true',
help='Stop running tests after the first error or failure.')
@CommandArgument('-j', '--jobs',
default=1,
type=int,
help='Number of concurrent jobs to run. Default is 1.')
default=1,
type=int,
help='Number of concurrent jobs to run. Default is 1.')
@CommandArgument('--subsuite',
default=None,
help=('Python subsuite to run. If not specified, all subsuites are run. '
'Use the string `default` to only run tests without a subsuite.'))
default=None,
help=('Python subsuite to run. If not specified, all subsuites are run. '
'Use the string `default` to only run tests without a subsuite.'))
@CommandArgument('tests', nargs='*',
metavar='TEST',
help=('Tests to run. Each test can be a single file or a directory. '
'Default test resolution relies on PYTHON_UNITTEST_MANIFESTS.'))
metavar='TEST',
help=('Tests to run. Each test can be a single file or a directory. '
'Default test resolution relies on PYTHON_UNITTEST_MANIFESTS.'))
def python_test(self, *args, **kwargs):
try:
tempdir = os.environ[b'PYTHON_TEST_TMP'] = str(tempfile.mkdtemp(suffix='-python-test'))
@ -99,8 +99,8 @@ class MachCommands(MachCommandBase):
files += glob.glob(mozpath.join(root, 'unit*.py'))
else:
self.log(logging.WARN, 'python-test',
{'test': t},
'TEST-UNEXPECTED-FAIL | Invalid test: {test}')
{'test': t},
'TEST-UNEXPECTED-FAIL | Invalid test: {test}')
if stop:
break
return files

View File

@ -2,7 +2,9 @@
flake8:
description: Python linter
include:
- config/mozunit.py
- layout/tools/reftest
- python/mach_commands.py
- python/mozlint
- security/manager
- taskcluster