зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1302172 - [mozlint] Convert unittest tests to use pytest instead, r=maja_zf
MozReview-Commit-ID: D4bN62QbkKm --HG-- extra : rebase_source : 22ffe05ca78095fc469520b9f85d93585251cd1c
This commit is contained in:
Родитель
b03af07d9c
Коммит
98a0fd6336
|
@ -0,0 +1,42 @@
|
|||
# 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/.
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from mozlint import LintRoller
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def lint(request):
|
||||
lintargs = getattr(request.module, 'lintargs', {})
|
||||
return LintRoller(root=here, **lintargs)
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def filedir():
|
||||
return os.path.join(here, 'files')
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def files(filedir, request):
|
||||
suffix_filter = getattr(request.module, 'files', [''])
|
||||
return [os.path.join(filedir, p) for p in os.listdir(filedir)
|
||||
if any(p.endswith(suffix) for suffix in suffix_filter)]
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def lintdir():
|
||||
return os.path.join(here, 'linters')
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def linters(lintdir, request):
|
||||
suffix_filter = getattr(request.module, 'linters', ['.lint'])
|
||||
return [os.path.join(lintdir, p) for p in os.listdir(lintdir)
|
||||
if any(p.endswith(suffix) for suffix in suffix_filter)]
|
|
@ -5,56 +5,50 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from unittest import TestCase
|
||||
|
||||
from mozunit import main
|
||||
import pytest
|
||||
|
||||
from mozlint import ResultContainer
|
||||
from mozlint import formatters
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
@pytest.fixture
|
||||
def results(scope='module'):
|
||||
containers = (
|
||||
ResultContainer(
|
||||
linter='foo',
|
||||
path='a/b/c.txt',
|
||||
message="oh no foo",
|
||||
lineno=1,
|
||||
),
|
||||
ResultContainer(
|
||||
linter='bar',
|
||||
path='d/e/f.txt',
|
||||
message="oh no bar",
|
||||
hint="try baz instead",
|
||||
level='warning',
|
||||
lineno=4,
|
||||
column=2,
|
||||
rule="bar-not-allowed",
|
||||
),
|
||||
ResultContainer(
|
||||
linter='baz',
|
||||
path='a/b/c.txt',
|
||||
message="oh no baz",
|
||||
lineno=4,
|
||||
source="if baz:",
|
||||
),
|
||||
)
|
||||
results = defaultdict(list)
|
||||
for c in containers:
|
||||
results[c.path].append(c)
|
||||
return results
|
||||
|
||||
|
||||
class TestFormatters(TestCase):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
TestCase.__init__(self, *args, **kwargs)
|
||||
|
||||
containers = (
|
||||
ResultContainer(
|
||||
linter='foo',
|
||||
path='a/b/c.txt',
|
||||
message="oh no foo",
|
||||
lineno=1,
|
||||
),
|
||||
ResultContainer(
|
||||
linter='bar',
|
||||
path='d/e/f.txt',
|
||||
message="oh no bar",
|
||||
hint="try baz instead",
|
||||
level='warning',
|
||||
lineno=4,
|
||||
column=2,
|
||||
rule="bar-not-allowed",
|
||||
),
|
||||
ResultContainer(
|
||||
linter='baz',
|
||||
path='a/b/c.txt',
|
||||
message="oh no baz",
|
||||
lineno=4,
|
||||
source="if baz:",
|
||||
),
|
||||
)
|
||||
|
||||
self.results = defaultdict(list)
|
||||
for c in containers:
|
||||
self.results[c.path].append(c)
|
||||
|
||||
def test_stylish_formatter(self):
|
||||
expected = """
|
||||
def test_stylish_formatter(results):
|
||||
expected = """
|
||||
a/b/c.txt
|
||||
1 error oh no foo (foo)
|
||||
4 error oh no baz (baz)
|
||||
|
@ -65,30 +59,32 @@ d/e/f.txt
|
|||
\u2716 3 problems (2 errors, 1 warning)
|
||||
""".strip()
|
||||
|
||||
fmt = formatters.get('stylish', disable_colors=True)
|
||||
self.assertEqual(expected, fmt(self.results))
|
||||
fmt = formatters.get('stylish', disable_colors=True)
|
||||
assert expected == fmt(results)
|
||||
|
||||
def test_treeherder_formatter(self):
|
||||
expected = """
|
||||
|
||||
def test_treeherder_formatter(results):
|
||||
expected = """
|
||||
TEST-UNEXPECTED-ERROR | a/b/c.txt:1 | oh no foo (foo)
|
||||
TEST-UNEXPECTED-ERROR | a/b/c.txt:4 | oh no baz (baz)
|
||||
TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
|
||||
""".strip()
|
||||
|
||||
fmt = formatters.get('treeherder')
|
||||
self.assertEqual(expected, fmt(self.results))
|
||||
fmt = formatters.get('treeherder')
|
||||
assert expected == fmt(results)
|
||||
|
||||
def test_json_formatter(self):
|
||||
fmt = formatters.get('json')
|
||||
formatted = json.loads(fmt(self.results))
|
||||
|
||||
self.assertEqual(set(formatted.keys()), set(self.results.keys()))
|
||||
def test_json_formatter(results):
|
||||
fmt = formatters.get('json')
|
||||
formatted = json.loads(fmt(results))
|
||||
|
||||
slots = ResultContainer.__slots__
|
||||
for errors in formatted.values():
|
||||
for err in errors:
|
||||
self.assertTrue(all(s in err for s in slots))
|
||||
assert set(formatted.keys()) == set(results.keys())
|
||||
|
||||
slots = ResultContainer.__slots__
|
||||
for errors in formatted.values():
|
||||
for err in errors:
|
||||
assert all(s in err for s in slots)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
sys.exit(pytest.main(['--verbose', __file__]))
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
import os
|
||||
from unittest import TestCase
|
||||
import sys
|
||||
|
||||
from mozunit import main
|
||||
import pytest
|
||||
|
||||
from mozlint.parser import Parser
|
||||
from mozlint.errors import (
|
||||
|
@ -14,55 +14,42 @@ from mozlint.errors import (
|
|||
)
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
@pytest.fixture(scope='module')
|
||||
def parse(lintdir):
|
||||
parser = Parser()
|
||||
|
||||
def _parse(name):
|
||||
path = os.path.join(lintdir, name)
|
||||
return parser(path)
|
||||
return _parse
|
||||
|
||||
|
||||
class TestParser(TestCase):
|
||||
def test_parse_valid_linter(parse):
|
||||
lintobj = parse('string.lint')
|
||||
assert isinstance(lintobj, dict)
|
||||
assert 'name' in lintobj
|
||||
assert 'description' in lintobj
|
||||
assert 'type' in lintobj
|
||||
assert 'payload' in lintobj
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
TestCase.__init__(self, *args, **kwargs)
|
||||
|
||||
self._lintdir = os.path.join(here, 'linters')
|
||||
self._parse = Parser()
|
||||
@pytest.mark.parametrize('linter', [
|
||||
'invalid_type.lint',
|
||||
'invalid_extension.lnt',
|
||||
'invalid_include.lint',
|
||||
'invalid_exclude.lint',
|
||||
'missing_attrs.lint',
|
||||
'missing_definition.lint',
|
||||
])
|
||||
def test_parse_invalid_linter(parse, linter):
|
||||
with pytest.raises(LinterParseError):
|
||||
parse(linter)
|
||||
|
||||
def parse(self, name):
|
||||
return self._parse(os.path.join(self._lintdir, name))
|
||||
|
||||
def test_parse_valid_linter(self):
|
||||
linter = self.parse('string.lint')
|
||||
self.assertIsInstance(linter, dict)
|
||||
self.assertIn('name', linter)
|
||||
self.assertIn('description', linter)
|
||||
self.assertIn('type', linter)
|
||||
self.assertIn('payload', linter)
|
||||
|
||||
def test_parse_invalid_type(self):
|
||||
with self.assertRaises(LinterParseError):
|
||||
self.parse('invalid_type.lint')
|
||||
|
||||
def test_parse_invalid_extension(self):
|
||||
with self.assertRaises(LinterParseError):
|
||||
self.parse('invalid_extension.lnt')
|
||||
|
||||
def test_parse_invalid_include_exclude(self):
|
||||
with self.assertRaises(LinterParseError):
|
||||
self.parse('invalid_include.lint')
|
||||
|
||||
with self.assertRaises(LinterParseError):
|
||||
self.parse('invalid_exclude.lint')
|
||||
|
||||
def test_parse_missing_attributes(self):
|
||||
with self.assertRaises(LinterParseError):
|
||||
self.parse('missing_attrs.lint')
|
||||
|
||||
def test_parse_missing_definition(self):
|
||||
with self.assertRaises(LinterParseError):
|
||||
self.parse('missing_definition.lint')
|
||||
|
||||
def test_parse_non_existent_linter(self):
|
||||
with self.assertRaises(LinterNotFound):
|
||||
self.parse('missing_file.lint')
|
||||
def test_parse_non_existent_linter(parse):
|
||||
with pytest.raises(LinterNotFound):
|
||||
parse('missing_file.lint')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
sys.exit(pytest.main(['--verbose', __file__]))
|
||||
|
|
|
@ -4,77 +4,67 @@
|
|||
|
||||
import os
|
||||
import sys
|
||||
from unittest import TestCase
|
||||
|
||||
from mozunit import main
|
||||
import pytest
|
||||
|
||||
from mozlint import LintRoller, ResultContainer
|
||||
from mozlint import ResultContainer
|
||||
from mozlint.errors import LintersNotConfigured, LintException
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
class TestLintRoller(TestCase):
|
||||
linters = ('string.lint', 'regex.lint', 'external.lint')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
TestCase.__init__(self, *args, **kwargs)
|
||||
|
||||
self.filedir = os.path.join(here, 'files')
|
||||
self.files = [os.path.join(self.filedir, f) for f in os.listdir(self.filedir)]
|
||||
self.lintdir = os.path.join(here, 'linters')
|
||||
def test_roll_no_linters_configured(lint, files):
|
||||
with pytest.raises(LintersNotConfigured):
|
||||
lint.roll(files)
|
||||
|
||||
names = ('string.lint', 'regex.lint', 'external.lint')
|
||||
self.linters = [os.path.join(self.lintdir, n) for n in names]
|
||||
|
||||
def setUp(self):
|
||||
TestCase.setUp(self)
|
||||
self.lint = LintRoller(root=here)
|
||||
def test_roll_successful(lint, linters, files):
|
||||
lint.read(linters)
|
||||
|
||||
def test_roll_no_linters_configured(self):
|
||||
with self.assertRaises(LintersNotConfigured):
|
||||
self.lint.roll(self.files)
|
||||
result = lint.roll(files)
|
||||
assert len(result) == 1
|
||||
|
||||
def test_roll_successful(self):
|
||||
self.lint.read(self.linters)
|
||||
path = result.keys()[0]
|
||||
assert os.path.basename(path) == 'foobar.js'
|
||||
|
||||
result = self.lint.roll(self.files)
|
||||
self.assertEqual(len(result), 1)
|
||||
errors = result[path]
|
||||
assert isinstance(errors, list)
|
||||
assert len(errors) == 6
|
||||
|
||||
path = result.keys()[0]
|
||||
self.assertEqual(os.path.basename(path), 'foobar.js')
|
||||
container = errors[0]
|
||||
assert isinstance(container, ResultContainer)
|
||||
assert container.rule == 'no-foobar'
|
||||
|
||||
errors = result[path]
|
||||
self.assertIsInstance(errors, list)
|
||||
self.assertEqual(len(errors), 6)
|
||||
|
||||
container = errors[0]
|
||||
self.assertIsInstance(container, ResultContainer)
|
||||
self.assertEqual(container.rule, 'no-foobar')
|
||||
def test_roll_catch_exception(lint, lintdir, files):
|
||||
lint.read(os.path.join(lintdir, 'raises.lint'))
|
||||
|
||||
def test_roll_catch_exception(self):
|
||||
self.lint.read(os.path.join(self.lintdir, 'raises.lint'))
|
||||
# suppress printed traceback from test output
|
||||
old_stderr = sys.stderr
|
||||
sys.stderr = open(os.devnull, 'w')
|
||||
with pytest.raises(LintException):
|
||||
lint.roll(files)
|
||||
sys.stderr = old_stderr
|
||||
|
||||
# suppress printed traceback from test output
|
||||
old_stderr = sys.stderr
|
||||
sys.stderr = open(os.devnull, 'w')
|
||||
with self.assertRaises(LintException):
|
||||
self.lint.roll(self.files)
|
||||
sys.stderr = old_stderr
|
||||
|
||||
def test_roll_with_excluded_path(self):
|
||||
self.lint.lintargs.update({'exclude': ['**/foobar.js']})
|
||||
def test_roll_with_excluded_path(lint, linters, files):
|
||||
lint.lintargs.update({'exclude': ['**/foobar.js']})
|
||||
|
||||
self.lint.read(self.linters)
|
||||
result = self.lint.roll(self.files)
|
||||
lint.read(linters)
|
||||
result = lint.roll(files)
|
||||
|
||||
self.assertEqual(len(result), 0)
|
||||
assert len(result) == 0
|
||||
|
||||
def test_roll_with_invalid_extension(self):
|
||||
self.lint.read(os.path.join(self.lintdir, 'external.lint'))
|
||||
result = self.lint.roll(os.path.join(self.filedir, 'foobar.py'))
|
||||
self.assertEqual(len(result), 0)
|
||||
|
||||
def test_roll_with_invalid_extension(lint, lintdir, filedir):
|
||||
lint.read(os.path.join(lintdir, 'external.lint'))
|
||||
result = lint.roll(os.path.join(filedir, 'foobar.py'))
|
||||
assert len(result) == 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
sys.exit(pytest.main(['--verbose', __file__]))
|
||||
|
|
|
@ -3,76 +3,48 @@
|
|||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
import os
|
||||
from unittest import TestCase
|
||||
import sys
|
||||
|
||||
from mozunit import main
|
||||
import pytest
|
||||
|
||||
from mozlint import LintRoller
|
||||
from mozlint.result import ResultContainer
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
@pytest.fixture
|
||||
def path(filedir):
|
||||
def _path(name):
|
||||
return os.path.join(filedir, name)
|
||||
return _path
|
||||
|
||||
|
||||
class TestLinterTypes(TestCase):
|
||||
@pytest.fixture(params=['string.lint', 'regex.lint', 'external.lint'])
|
||||
def linter(lintdir, request):
|
||||
return os.path.join(lintdir, request.param)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
TestCase.__init__(self, *args, **kwargs)
|
||||
|
||||
self.lintdir = os.path.join(here, 'linters')
|
||||
self.filedir = os.path.join(here, 'files')
|
||||
self.files = [os.path.join(self.filedir, f) for f in os.listdir(self.filedir)]
|
||||
def test_linter_types(lint, linter, files, path):
|
||||
lint.read(linter)
|
||||
result = lint.roll(files)
|
||||
assert isinstance(result, dict)
|
||||
assert path('foobar.js') in result
|
||||
assert path('no_foobar.js') not in result
|
||||
|
||||
def setUp(self):
|
||||
TestCase.setUp(self)
|
||||
self.lint = LintRoller(root=here)
|
||||
result = result[path('foobar.js')][0]
|
||||
assert isinstance(result, ResultContainer)
|
||||
|
||||
def path(self, name):
|
||||
return os.path.join(self.filedir, name)
|
||||
name = os.path.basename(linter).split('.')[0]
|
||||
assert result.linter.lower().startswith(name)
|
||||
|
||||
def test_string_linter(self):
|
||||
self.lint.read(os.path.join(self.lintdir, 'string.lint'))
|
||||
result = self.lint.roll(self.files)
|
||||
self.assertIsInstance(result, dict)
|
||||
|
||||
self.assertIn(self.path('foobar.js'), result.keys())
|
||||
self.assertNotIn(self.path('no_foobar.js'), result.keys())
|
||||
def test_no_filter(lint, lintdir, files):
|
||||
lint.read(os.path.join(lintdir, 'explicit_path.lint'))
|
||||
result = lint.roll(files)
|
||||
assert len(result) == 0
|
||||
|
||||
result = result[self.path('foobar.js')][0]
|
||||
self.assertIsInstance(result, ResultContainer)
|
||||
self.assertEqual(result.linter, 'StringLinter')
|
||||
|
||||
def test_regex_linter(self):
|
||||
self.lint.read(os.path.join(self.lintdir, 'regex.lint'))
|
||||
result = self.lint.roll(self.files)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertIn(self.path('foobar.js'), result.keys())
|
||||
self.assertNotIn(self.path('no_foobar.js'), result.keys())
|
||||
|
||||
result = result[self.path('foobar.js')][0]
|
||||
self.assertIsInstance(result, ResultContainer)
|
||||
self.assertEqual(result.linter, 'RegexLinter')
|
||||
|
||||
def test_external_linter(self):
|
||||
self.lint.read(os.path.join(self.lintdir, 'external.lint'))
|
||||
result = self.lint.roll(self.files)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertIn(self.path('foobar.js'), result.keys())
|
||||
self.assertNotIn(self.path('no_foobar.js'), result.keys())
|
||||
|
||||
result = result[self.path('foobar.js')][0]
|
||||
self.assertIsInstance(result, ResultContainer)
|
||||
self.assertEqual(result.linter, 'ExternalLinter')
|
||||
|
||||
def test_no_filter(self):
|
||||
self.lint.read(os.path.join(self.lintdir, 'explicit_path.lint'))
|
||||
result = self.lint.roll(self.files)
|
||||
self.assertEqual(len(result), 0)
|
||||
|
||||
self.lint.lintargs['use_filters'] = False
|
||||
result = self.lint.roll(self.files)
|
||||
self.assertEqual(len(result), 2)
|
||||
lint.lintargs['use_filters'] = False
|
||||
result = lint.roll(files)
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
sys.exit(pytest.main(['--verbose', __file__]))
|
||||
|
|
Загрузка…
Ссылка в новой задаче