Add tests for the expected behaviour of per-file-ignores

These show that there were some subtle bugs in how we were matching
things and that there are bugs that need to be fixed.
This commit is contained in:
Ian Stapleton Cordasco 2018-10-26 15:01:06 -05:00
parent f2776107db
commit 1433a008b3
No known key found for this signature in database
GPG key ID: C9D7A2604B4FCB2A

View file

@ -5,6 +5,7 @@ import mock
import pytest
from flake8 import style_guide
from flake8 import utils
from flake8.formatting import base
from flake8.plugins import notifier
@ -17,6 +18,7 @@ def create_options(**kwargs):
kwargs.setdefault('extend_ignore', [])
kwargs.setdefault('disable_noqa', False)
kwargs.setdefault('enable_extensions', [])
kwargs.setdefault('per_file_ignores', [])
return optparse.Values(kwargs)
@ -81,3 +83,101 @@ def test_handle_error_does_not_notify_listeners(select_list, ignore_list,
guide.handle_error(error_code, 'stdin', 1, 1, 'error found')
assert listener_trie.notify.called is False
assert formatter.handle.called is False
def test_style_guide_manager():
"""Verify how the StyleGuideManager creates a default style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options()
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
assert guide.default_style_guide.options is options
assert len(guide.style_guides) == 1
PER_FILE_IGNORES_UNPARSED = [
"first_file.py:W9",
"second_file.py:F4,F9",
"third_file.py:E3",
"sub_dir/*:F4",
]
@pytest.mark.parametrize('style_guide_file,filename,expected', [
("first_file.py", "first_file.py", True),
("first_file.py", "second_file.py", False),
("sub_dir/*.py", "first_file.py", False),
("sub_dir/*.py", "sub_dir/file.py", True),
("sub_dir/*.py", "other_dir/file.py", False),
])
def test_style_guide_applies_to(style_guide_file, filename, expected):
"""Verify that we match a file to its style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options()
guide = style_guide.StyleGuide(options,
listener_trie=listener_trie,
formatter=formatter,
filename=style_guide_file)
assert guide.applies_to(filename) is expected
def test_style_guide_manager_pre_file_ignores_parsing():
"""Verify how the StyleGuideManager creates a default style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED)
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
assert len(guide.style_guides) == 5
assert list(map(utils.normalize_path,
["first_file.py", "second_file.py", "third_file.py",
"sub_dir/*"])
) == [g.filename for g in guide.style_guides[1:]]
@pytest.mark.parametrize('ignores,violation,filename,handle_error_return', [
(['E1', 'E2'], 'F401', 'first_file.py', 1),
(['E1', 'E2'], 'E121', 'first_file.py', 0),
(['E1', 'E2'], 'F401', 'second_file.py', 0),
(['E1', 'E2'], 'F401', 'third_file.py', 1),
(['E1', 'E2'], 'E311', 'third_file.py', 0),
(['E1', 'E2'], 'F401', 'sub_dir/file.py', 0),
])
def test_style_guide_manager_pre_file_ignores(ignores, violation, filename,
handle_error_return):
"""Verify how the StyleGuideManager creates a default style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options(ignore=ignores,
select=['E', 'F', 'W'],
per_file_ignores=PER_FILE_IGNORES_UNPARSED)
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
assert (guide.handle_error(violation, filename, 1, 1, "Fake text")
== handle_error_return)
@pytest.mark.parametrize('filename,expected', [
('first_file.py', utils.normalize_path('first_file.py')),
('second_file.py', utils.normalize_path('second_file.py')),
('third_file.py', utils.normalize_path('third_file.py')),
('fourth_file.py', None),
('sub_dir/__init__.py', utils.normalize_path('sub_dir/*')),
('other_dir/__init__.py', None),
])
def test_style_guide_manager_style_guide_for(filename, expected):
"""Verify the style guide selection function."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED)
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
file_guide = guide.style_guide_for(filename)
assert file_guide.filename == expected