mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-06 13:06:53 +00:00
Add handling and decision making for select and ignore
This commit is contained in:
parent
0645ec3ef7
commit
eafc91ae6a
4 changed files with 249 additions and 6 deletions
128
tests/unit/test_style_guide.py
Normal file
128
tests/unit/test_style_guide.py
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
"""Tests for the flake8.style_guide.StyleGuide class."""
|
||||
import optparse
|
||||
|
||||
from flake8 import style_guide
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def create_options(**kwargs):
|
||||
"""Create and return an instance of optparse.Values."""
|
||||
kwargs.setdefault('select', [])
|
||||
kwargs.setdefault('ignore', [])
|
||||
return optparse.Values(kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ignore_list,error_code', [
|
||||
(['E111', 'E121'], 'E111'),
|
||||
(['E111', 'E121'], 'E121'),
|
||||
(['E11', 'E12'], 'E121'),
|
||||
(['E2', 'E12'], 'E121'),
|
||||
(['E2', 'E12'], 'E211'),
|
||||
])
|
||||
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)
|
||||
|
||||
assert guide.is_user_ignored(error_code) is style_guide.Ignored.Explicitly
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ignore_list,error_code', [
|
||||
(['E111', 'E121'], 'E112'),
|
||||
(['E111', 'E121'], 'E122'),
|
||||
(['E11', 'E12'], 'W121'),
|
||||
(['E2', 'E12'], 'E112'),
|
||||
(['E2', 'E12'], 'E111'),
|
||||
])
|
||||
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)
|
||||
|
||||
assert guide.is_user_ignored(error_code) is style_guide.Selected.Implicitly
|
||||
|
||||
|
||||
@pytest.mark.parametrize('select_list,error_code', [
|
||||
(['E111', 'E121'], 'E111'),
|
||||
(['E111', 'E121'], 'E121'),
|
||||
(['E11', 'E12'], 'E121'),
|
||||
(['E2', 'E12'], 'E121'),
|
||||
(['E2', 'E12'], 'E211'),
|
||||
])
|
||||
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)
|
||||
|
||||
assert (guide.is_user_selected(error_code) is
|
||||
style_guide.Selected.Explicitly)
|
||||
|
||||
|
||||
def test_is_user_selected_implicitly_selects_errors():
|
||||
"""Verify we detect users implicitly selecting an error."""
|
||||
select_list = []
|
||||
error_code = 'E121'
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
|
||||
assert (guide.is_user_selected(error_code) is
|
||||
style_guide.Selected.Implicitly)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('select_list,error_code', [
|
||||
(['E111', 'E121'], 'E112'),
|
||||
(['E111', 'E121'], 'E122'),
|
||||
(['E11', 'E12'], 'E132'),
|
||||
(['E2', 'E12'], 'E321'),
|
||||
(['E2', 'E12'], 'E410'),
|
||||
])
|
||||
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)
|
||||
|
||||
assert guide.is_user_selected(error_code) is style_guide.Ignored.Implicitly
|
||||
|
||||
|
||||
@pytest.mark.parametrize('select_list,ignore_list,error_code,expected', [
|
||||
(['E111', 'E121'], [], 'E111', style_guide.Decision.Selected),
|
||||
(['E111', 'E121'], [], 'E112', style_guide.Decision.Ignored),
|
||||
(['E111', 'E121'], [], 'E121', style_guide.Decision.Selected),
|
||||
(['E111', 'E121'], [], 'E122', style_guide.Decision.Ignored),
|
||||
(['E11', 'E12'], [], 'E132', style_guide.Decision.Ignored),
|
||||
(['E2', 'E12'], [], 'E321', style_guide.Decision.Ignored),
|
||||
(['E2', 'E12'], [], 'E410', style_guide.Decision.Ignored),
|
||||
(['E11', 'E121'], ['E1'], 'E112', style_guide.Decision.Selected),
|
||||
(['E111', 'E121'], ['E2'], 'E122', style_guide.Decision.Ignored),
|
||||
(['E11', 'E12'], ['E13'], 'E132', style_guide.Decision.Ignored),
|
||||
(['E1', 'E3'], ['E32'], 'E321', style_guide.Decision.Ignored),
|
||||
([], ['E2', 'E12'], 'E410', style_guide.Decision.Selected),
|
||||
(['E4'], ['E2', 'E12', 'E41'], 'E410', style_guide.Decision.Ignored),
|
||||
(['E41'], ['E2', 'E12', 'E4'], 'E410', style_guide.Decision.Selected),
|
||||
])
|
||||
def test_should_report_error(select_list, ignore_list, error_code, expected):
|
||||
"""Verify we decide when to report an error."""
|
||||
guide = style_guide.StyleGuide(create_options(select=select_list,
|
||||
ignore=ignore_list),
|
||||
arguments=[],
|
||||
checker_plugins=None,
|
||||
listening_plugins=None,
|
||||
formatting_plugins=None)
|
||||
|
||||
assert guide.should_report_error(error_code) is expected
|
||||
Loading…
Add table
Add a link
Reference in a new issue