summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorIan Cordasco <graffatcolmingov@gmail.com>2017-06-04 07:57:28 -0500
committerIan Cordasco <graffatcolmingov@gmail.com>2017-06-04 07:57:28 -0500
commit92c367dee4e5908745822902adfda0bd915b1380 (patch)
tree4f069b953d063c4f129bb276329d06d308a8a930 /tests/unit
parent65107a5624eb6eb4b1ca78e081b6c66879b77655 (diff)
downloadflake8-92c367dee4e5908745822902adfda0bd915b1380.tar.gz
Rename style_guide.Error to style_guide.Violation
Move all Violation related methods from the StyleGuide to our Violation class.
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/test_base_formatter.py8
-rw-r--r--tests/unit/test_filenameonly_formatter.py7
-rw-r--r--tests/unit/test_nothing_formatter.py4
-rw-r--r--tests/unit/test_statistics.py2
-rw-r--r--tests/unit/test_style_guide.py44
-rw-r--r--tests/unit/test_violation.py55
6 files changed, 68 insertions, 52 deletions
diff --git a/tests/unit/test_base_formatter.py b/tests/unit/test_base_formatter.py
index bb54358..87b5fee 100644
--- a/tests/unit/test_base_formatter.py
+++ b/tests/unit/test_base_formatter.py
@@ -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,
diff --git a/tests/unit/test_filenameonly_formatter.py b/tests/unit/test_filenameonly_formatter.py
index caa0ae3..b423ee3 100644
--- a/tests/unit/test_filenameonly_formatter.py
+++ b/tests/unit/test_filenameonly_formatter.py
@@ -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
diff --git a/tests/unit/test_nothing_formatter.py b/tests/unit/test_nothing_formatter.py
index ce8087f..a1fd683 100644
--- a/tests/unit/test_nothing_formatter.py
+++ b/tests/unit/test_nothing_formatter.py
@@ -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
diff --git a/tests/unit/test_statistics.py b/tests/unit/test_statistics.py
index f95c638..31d6276 100644
--- a/tests/unit/test_statistics.py
+++ b/tests/unit/test_statistics.py
@@ -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),
diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py
index b3641c6..84c47ff 100644
--- a/tests/unit/test_style_guide.py
+++ b/tests/unit/test_style_guide.py
@@ -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)
diff --git a/tests/unit/test_violation.py b/tests/unit/test_violation.py
new file mode 100644
index 0000000..fa75278
--- /dev/null
+++ b/tests/unit/test_violation.py
@@ -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