mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-03 19:56:54 +00:00
Update StyleGuide tests and add new tests for handle_error
This commit is contained in:
parent
99b96e95ce
commit
a9a939cbbc
2 changed files with 67 additions and 20 deletions
|
|
@ -139,9 +139,9 @@ class StyleGuide(object):
|
|||
# type: (str, str, int, int, str) -> NoneType
|
||||
"""Handle an error reported by a check."""
|
||||
error = Error(code, filename, line_number, column_number, text)
|
||||
if self.should_report_error(error):
|
||||
if self.should_report_error(error.code) is Decision.Selected:
|
||||
self.formatter.handle(error)
|
||||
self.notifier.notify(error.code, error)
|
||||
self.listener.notify(error.code, error)
|
||||
|
||||
# Should separate style guide logic from code that runs checks
|
||||
# StyleGuide should manage select/ignore logic as well as include/exclude
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
"""Tests for the flake8.style_guide.StyleGuide class."""
|
||||
import optparse
|
||||
|
||||
from flake8.formatting import base
|
||||
from flake8.plugins import notifier
|
||||
from flake8 import style_guide
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
|
||||
|
|
@ -24,9 +27,8 @@ def test_is_user_ignored_ignores_errors(ignore_list, error_code):
|
|||
"""Verify we detect users explicitly ignoring an error."""
|
||||
guide = style_guide.StyleGuide(create_options(ignore=ignore_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
|
||||
assert guide.is_user_ignored(error_code) is style_guide.Ignored.Explicitly
|
||||
|
||||
|
|
@ -42,9 +44,8 @@ def test_is_user_ignored_implicitly_selects_errors(ignore_list, error_code):
|
|||
"""Verify we detect users does not explicitly ignore an error."""
|
||||
guide = style_guide.StyleGuide(create_options(ignore=ignore_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
|
||||
assert guide.is_user_ignored(error_code) is style_guide.Selected.Implicitly
|
||||
|
||||
|
|
@ -60,9 +61,8 @@ def test_is_user_selected_selects_errors(select_list, error_code):
|
|||
"""Verify we detect users explicitly selecting an error."""
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
|
||||
assert (guide.is_user_selected(error_code) is
|
||||
style_guide.Selected.Explicitly)
|
||||
|
|
@ -74,9 +74,8 @@ def test_is_user_selected_implicitly_selects_errors():
|
|||
error_code = 'E121'
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
|
||||
assert (guide.is_user_selected(error_code) is
|
||||
style_guide.Selected.Implicitly)
|
||||
|
|
@ -93,9 +92,8 @@ def test_is_user_selected_excludes_errors(select_list, error_code):
|
|||
"""Verify we detect users implicitly excludes an error."""
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
|
||||
assert guide.is_user_selected(error_code) is style_guide.Ignored.Implicitly
|
||||
|
||||
|
|
@ -121,8 +119,57 @@ def test_should_report_error(select_list, ignore_list, error_code, expected):
|
|||
guide = style_guide.StyleGuide(create_options(select=select_list,
|
||||
ignore=ignore_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
|
||||
assert guide.should_report_error(error_code) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('select_list,ignore_list,error_code', [
|
||||
(['E111', 'E121'], [], 'E111'),
|
||||
(['E111', 'E121'], [], 'E121'),
|
||||
(['E11', 'E121'], ['E1'], 'E112'),
|
||||
([], ['E2', 'E12'], 'E410'),
|
||||
(['E41'], ['E2', 'E12', 'E4'], 'E410'),
|
||||
])
|
||||
def test_handle_error_notifies_listeners(select_list, ignore_list, error_code):
|
||||
"""Verify that error codes notify the listener trie appropriately."""
|
||||
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
|
||||
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list,
|
||||
ignore=ignore_list),
|
||||
arguments=[],
|
||||
listener_trie=listener_trie,
|
||||
formatter=formatter)
|
||||
|
||||
guide.handle_error(error_code, 'stdin', 1, 1, 'error found')
|
||||
error = style_guide.Error(error_code, 'stdin', 1, 1, 'error found')
|
||||
listener_trie.notify.assert_called_once_with(error_code, error)
|
||||
formatter.handle.assert_called_once_with(error)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('select_list,ignore_list,error_code', [
|
||||
(['E111', 'E121'], [], 'E122'),
|
||||
(['E11', 'E12'], [], 'E132'),
|
||||
(['E2', 'E12'], [], 'E321'),
|
||||
(['E2', 'E12'], [], 'E410'),
|
||||
(['E111', 'E121'], ['E2'], 'E122'),
|
||||
(['E11', 'E12'], ['E13'], 'E132'),
|
||||
(['E1', 'E3'], ['E32'], 'E321'),
|
||||
(['E4'], ['E2', 'E12', 'E41'], 'E410'),
|
||||
(['E111', 'E121'], [], 'E112'),
|
||||
])
|
||||
def test_handle_error_does_not_notify_listeners(select_list, ignore_list,
|
||||
error_code):
|
||||
"""Verify that error codes notify the listener trie appropriately."""
|
||||
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
|
||||
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list,
|
||||
ignore=ignore_list),
|
||||
arguments=[],
|
||||
listener_trie=listener_trie,
|
||||
formatter=formatter)
|
||||
|
||||
guide.handle_error(error_code, 'stdin', 1, 1, 'error found')
|
||||
assert listener_trie.notify.called is False
|
||||
assert formatter.handle.called is False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue