mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-04 20:26:53 +00:00
Rename style_guide.Error to style_guide.Violation
Move all Violation related methods from the StyleGuide to our Violation class.
This commit is contained in:
parent
65107a5624
commit
92c367dee4
9 changed files with 185 additions and 136 deletions
|
|
@ -51,7 +51,7 @@ def test_show_source_returns_nothing_when_not_showing_source():
|
|||
"""Ensure we return nothing when users want nothing."""
|
||||
formatter = base.BaseFormatter(options(show_source=False))
|
||||
assert formatter.show_source(
|
||||
style_guide.Error('A000', 'file.py', 1, 1, 'error text', 'line')
|
||||
style_guide.Violation('A000', 'file.py', 1, 1, 'error text', 'line')
|
||||
) is ''
|
||||
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ def test_show_source_returns_nothing_when_there_is_source():
|
|||
"""Ensure we return nothing when there is no line."""
|
||||
formatter = base.BaseFormatter(options(show_source=True))
|
||||
assert formatter.show_source(
|
||||
style_guide.Error('A000', 'file.py', 1, 1, 'error text', None)
|
||||
style_guide.Violation('A000', 'file.py', 1, 1, 'error text', None)
|
||||
) is ''
|
||||
|
||||
|
||||
|
|
@ -71,7 +71,7 @@ def test_show_source_returns_nothing_when_there_is_source():
|
|||
def test_show_source_updates_physical_line_appropriately(line, column):
|
||||
"""Ensure the error column is appropriately indicated."""
|
||||
formatter = base.BaseFormatter(options(show_source=True))
|
||||
error = style_guide.Error('A000', 'file.py', 1, column, 'error', line)
|
||||
error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line)
|
||||
output = formatter.show_source(error)
|
||||
_, pointer = output.rsplit('\n', 1)
|
||||
assert pointer.count(' ') == (column - 1)
|
||||
|
|
@ -149,7 +149,7 @@ def test_handle_formats_the_error():
|
|||
"""Verify that a formatter will call format from handle."""
|
||||
formatter = FormatFormatter(options(show_source=False))
|
||||
filemock = formatter.output_fd = mock.Mock()
|
||||
error = style_guide.Error(
|
||||
error = style_guide.Violation(
|
||||
code='A001',
|
||||
filename='example.py',
|
||||
line_number=1,
|
||||
|
|
|
|||
|
|
@ -17,14 +17,15 @@ def test_caches_filenames_already_printed():
|
|||
formatter = default.FilenameOnly(options())
|
||||
assert formatter.filenames_already_printed == set()
|
||||
|
||||
formatter.format(style_guide.Error('code', 'file.py', 1, 1, 'text', 'l'))
|
||||
formatter.format(
|
||||
style_guide.Violation('code', 'file.py', 1, 1, 'text', 'l'))
|
||||
assert formatter.filenames_already_printed == {'file.py'}
|
||||
|
||||
|
||||
def test_only_returns_a_string_once_from_format():
|
||||
"""Verify format ignores the second error with the same filename."""
|
||||
formatter = default.FilenameOnly(options())
|
||||
error = style_guide.Error('code', 'file.py', 1, 1, 'text', '1')
|
||||
error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')
|
||||
|
||||
assert formatter.format(error) == 'file.py'
|
||||
assert formatter.format(error) is None
|
||||
|
|
@ -33,6 +34,6 @@ def test_only_returns_a_string_once_from_format():
|
|||
def test_show_source_returns_nothing():
|
||||
"""Verify show_source returns nothing."""
|
||||
formatter = default.FilenameOnly(options())
|
||||
error = style_guide.Error('code', 'file.py', 1, 1, 'text', '1')
|
||||
error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')
|
||||
|
||||
assert formatter.show_source(error) is None
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ def options(**kwargs):
|
|||
def test_format_returns_nothing():
|
||||
"""Verify Nothing.format returns None."""
|
||||
formatter = default.Nothing(options())
|
||||
error = style_guide.Error('code', 'file.py', 1, 1, 'text', '1')
|
||||
error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')
|
||||
|
||||
assert formatter.format(error) is None
|
||||
|
||||
|
|
@ -23,6 +23,6 @@ def test_format_returns_nothing():
|
|||
def test_show_source_returns_nothing():
|
||||
"""Verify Nothing.show_source returns None."""
|
||||
formatter = default.Nothing(options())
|
||||
error = style_guide.Error('code', 'file.py', 1, 1, 'text', '1')
|
||||
error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')
|
||||
|
||||
assert formatter.show_source(error) is None
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ DEFAULT_TEXT = 'Default text'
|
|||
|
||||
def make_error(**kwargs):
|
||||
"""Create errors with a bunch of default values."""
|
||||
return style_guide.Error(
|
||||
return style_guide.Violation(
|
||||
code=kwargs.pop('code', DEFAULT_ERROR_CODE),
|
||||
filename=kwargs.pop('filename', DEFAULT_FILENAME),
|
||||
line_number=kwargs.pop('line_number', 1),
|
||||
|
|
|
|||
|
|
@ -19,46 +19,6 @@ def create_options(**kwargs):
|
|||
return optparse.Values(kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('error_code,physical_line,expected_result', [
|
||||
('E111', 'a = 1', False),
|
||||
('E121', 'a = 1 # noqa: E111', False),
|
||||
('E121', 'a = 1 # noqa: E111,W123,F821', False),
|
||||
('E111', 'a = 1 # noqa: E111,W123,F821', True),
|
||||
('W123', 'a = 1 # noqa: E111,W123,F821', True),
|
||||
('E111', 'a = 1 # noqa: E11,W123,F821', True),
|
||||
('E111', 'a = 1 # noqa, analysis:ignore', True),
|
||||
('E111', 'a = 1 # noqa analysis:ignore', True),
|
||||
('E111', 'a = 1 # noqa - We do not care', True),
|
||||
('E111', 'a = 1 # noqa: We do not care', True),
|
||||
])
|
||||
def test_is_inline_ignored(error_code, physical_line, expected_result):
|
||||
"""Verify that we detect inline usage of ``# noqa``."""
|
||||
guide = style_guide.StyleGuide(create_options(select=['E', 'W', 'F']),
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
error = style_guide.Error(error_code, 'filename.py', 1, 1, 'error text',
|
||||
None)
|
||||
# We want `None` to be passed as the physical line so we actually use our
|
||||
# monkey-patched linecache.getline value.
|
||||
|
||||
with mock.patch('linecache.getline', return_value=physical_line):
|
||||
assert guide.is_inline_ignored(error) is expected_result
|
||||
|
||||
|
||||
def test_disable_is_inline_ignored():
|
||||
"""Verify that is_inline_ignored exits immediately if disabling NoQA."""
|
||||
guide = style_guide.StyleGuide(create_options(disable_noqa=True),
|
||||
listener_trie=None,
|
||||
formatter=None)
|
||||
error = style_guide.Error('E121', 'filename.py', 1, 1, 'error text',
|
||||
'line')
|
||||
|
||||
with mock.patch('linecache.getline') as getline:
|
||||
assert guide.is_inline_ignored(error) is False
|
||||
|
||||
assert getline.called is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize('select_list,ignore_list,error_code', [
|
||||
(['E111', 'E121'], [], 'E111'),
|
||||
(['E111', 'E121'], [], 'E121'),
|
||||
|
|
@ -76,8 +36,8 @@ def test_handle_error_notifies_listeners(select_list, ignore_list, error_code):
|
|||
|
||||
with mock.patch('linecache.getline', return_value=''):
|
||||
guide.handle_error(error_code, 'stdin', 1, 0, 'error found')
|
||||
error = style_guide.Error(error_code, 'stdin', 1, 1, 'error found',
|
||||
None)
|
||||
error = style_guide.Violation(
|
||||
error_code, 'stdin', 1, 1, 'error found', None)
|
||||
listener_trie.notify.assert_called_once_with(error_code, error)
|
||||
formatter.handle.assert_called_once_with(error)
|
||||
|
||||
|
|
|
|||
55
tests/unit/test_violation.py
Normal file
55
tests/unit/test_violation.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"""Tests for the flake8.style_guide.Violation class."""
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from flake8 import style_guide
|
||||
|
||||
|
||||
@pytest.mark.parametrize('error_code,physical_line,expected_result', [
|
||||
('E111', 'a = 1', False),
|
||||
('E121', 'a = 1 # noqa: E111', False),
|
||||
('E121', 'a = 1 # noqa: E111,W123,F821', False),
|
||||
('E111', 'a = 1 # noqa: E111,W123,F821', True),
|
||||
('W123', 'a = 1 # noqa: E111,W123,F821', True),
|
||||
('E111', 'a = 1 # noqa: E11,W123,F821', True),
|
||||
('E111', 'a = 1 # noqa, analysis:ignore', True),
|
||||
('E111', 'a = 1 # noqa analysis:ignore', True),
|
||||
('E111', 'a = 1 # noqa - We do not care', True),
|
||||
('E111', 'a = 1 # noqa: We do not care', True),
|
||||
])
|
||||
def test_is_inline_ignored(error_code, physical_line, expected_result):
|
||||
"""Verify that we detect inline usage of ``# noqa``."""
|
||||
error = style_guide.Violation(
|
||||
error_code, 'filename.py', 1, 1, 'error text', None)
|
||||
# We want `None` to be passed as the physical line so we actually use our
|
||||
# monkey-patched linecache.getline value.
|
||||
|
||||
with mock.patch('linecache.getline', return_value=physical_line):
|
||||
assert error.is_inline_ignored(False) is expected_result
|
||||
|
||||
|
||||
def test_disable_is_inline_ignored():
|
||||
"""Verify that is_inline_ignored exits immediately if disabling NoQA."""
|
||||
error = style_guide.Violation(
|
||||
'E121', 'filename.py', 1, 1, 'error text', 'line')
|
||||
|
||||
with mock.patch('linecache.getline') as getline:
|
||||
assert error.is_inline_ignored(True) is False
|
||||
|
||||
assert getline.called is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize('violation_file,violation_line,diff,expected', [
|
||||
('file.py', 10, {}, True),
|
||||
('file.py', 1, {'file.py': range(1, 2)}, True),
|
||||
('file.py', 10, {'file.py': range(1, 2)}, False),
|
||||
('file.py', 1, {'other.py': range(1, 2)}, False),
|
||||
('file.py', 10, {'other.py': range(1, 2)}, False),
|
||||
])
|
||||
def test_violation_is_in_diff(violation_file, violation_line, diff, expected):
|
||||
"""Verify that we find violations within a diff."""
|
||||
violation = style_guide.Violation(
|
||||
'E001', violation_file, violation_line, 1, 'warning', 'line',
|
||||
)
|
||||
|
||||
assert violation.is_in(diff) is expected
|
||||
Loading…
Add table
Add a link
Reference in a new issue