summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2020-03-17 22:12:56 -0700
committerAnthony Sottile <asottile@umich.edu>2020-03-17 22:12:56 -0700
commit28797a57d882283c1c987b96655cc1e53aef060e (patch)
tree6e2418de570b88f619dcfd34933026ed2bce0a1d /tests
parent8f9b4931b9a28896fb43edccb23016a7540f5b82 (diff)
downloadflake8-28797a57d882283c1c987b96655cc1e53aef060e.tar.gz
Allow noqa to apply to lines due to continuationnoqa_continuation
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/test_checker.py9
-rw-r--r--tests/integration/test_main.py19
-rw-r--r--tests/unit/test_file_processor.py50
3 files changed, 68 insertions, 10 deletions
diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py
index 93cc239..096b350 100644
--- a/tests/integration/test_checker.py
+++ b/tests/integration/test_checker.py
@@ -15,7 +15,7 @@ EXPECTED_RESULT_PHYSICAL_LINE = (
0,
1,
'Expected Message',
- PHYSICAL_LINE,
+ None,
)
@@ -153,11 +153,10 @@ def test_line_check_results(plugin_target, len_results):
"""Test the FileChecker class handling results from line checks."""
file_checker = mock_file_checker_with_plugin(plugin_target)
- # Results will be store in an internal array
+ # Results will be stored in an internal array
file_checker.run_physical_checks(PHYSICAL_LINE)
- assert file_checker.results == [
- EXPECTED_RESULT_PHYSICAL_LINE
- ] * len_results
+ expected = [EXPECTED_RESULT_PHYSICAL_LINE] * len_results
+ assert file_checker.results == expected
PLACEHOLDER_CODE = 'some_line = "of" * code'
diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py
index db307ba..b67992b 100644
--- a/tests/integration/test_main.py
+++ b/tests/integration/test_main.py
@@ -182,6 +182,25 @@ t.py:1:15: E711 comparison to None should be 'if cond is None:'
'''
+def test_specific_noqa_on_line_with_continuation(tmpdir, capsys):
+ """See https://gitlab.com/pycqa/flake8/issues/375."""
+ t_py_src = '''\
+from os \\
+ import path # noqa: F401
+
+x = """
+ trailing whitespace: \n
+""" # noqa: W291
+'''
+
+ with tmpdir.as_cwd():
+ tmpdir.join('t.py').write(t_py_src)
+ _call_main(['t.py'], retv=0)
+
+ out, err = capsys.readouterr()
+ assert out == err == ''
+
+
def test_obtaining_args_from_sys_argv_when_not_explicity_provided(capsys):
"""Test that arguments are obtained from 'sys.argv'."""
with mock.patch('sys.argv', ['flake8', '--help']):
diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py
index e5367a8..044d11b 100644
--- a/tests/unit/test_file_processor.py
+++ b/tests/unit/test_file_processor.py
@@ -122,16 +122,56 @@ def test_read_lines_ignores_empty_display_name(
assert file_processor.filename == 'stdin'
-def test_line_for(default_options):
+def test_noqa_line_for(default_options):
"""Verify we grab the correct line from the cached lines."""
file_processor = processor.FileProcessor('-', default_options, lines=[
- 'Line 1',
- 'Line 2',
- 'Line 3',
+ 'Line 1\n',
+ 'Line 2\n',
+ 'Line 3\n',
])
for i in range(1, 4):
- assert file_processor.line_for(i) == 'Line {0}'.format(i)
+ assert file_processor.noqa_line_for(i) == 'Line {0}\n'.format(i)
+
+
+def test_noqa_line_for_continuation(default_options):
+ """Verify that the correct "line" is retrieved for continuation."""
+ src = '''\
+from foo \\
+ import bar # 2
+
+x = """
+hello
+world
+""" # 7
+'''
+ lines = src.splitlines(True)
+ file_processor = processor.FileProcessor('-', default_options, lines=lines)
+
+ assert file_processor.noqa_line_for(0) is None
+
+ l_1_2 = 'from foo \\\n import bar # 2\n'
+ assert file_processor.noqa_line_for(1) == l_1_2
+ assert file_processor.noqa_line_for(2) == l_1_2
+
+ assert file_processor.noqa_line_for(3) == '\n'
+
+ l_4_7 = 'x = """\nhello\nworld\n""" # 7\n'
+ for i in (4, 5, 6, 7):
+ assert file_processor.noqa_line_for(i) == l_4_7
+
+ assert file_processor.noqa_line_for(8) is None
+
+
+def test_noqa_line_for_no_eol_at_end_of_file(default_options):
+ """Verify that we properly handle noqa line at the end of the file."""
+ src = 'from foo \\\nimport bar' # no end of file newline
+ lines = src.splitlines(True)
+ file_processor = processor.FileProcessor('-', default_options, lines=lines)
+
+ l_1_2 = 'from foo \\\nimport bar'
+ assert file_processor.noqa_line_for(1) == l_1_2
+ assert file_processor.noqa_line_for(2) == l_1_2
def test_next_line(default_options):