mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-04 20:26:53 +00:00
Handle SyntaxErrors after new-lines specially
In some cases, when we handle SyntaxErrors we need to ensure that the column number is correct for a 1-indexed report. In some cases, we also need to account for the fact that the SyntaxError has happened "after" a new-line. To extract and alter the row and column numbers, we've moved the logic to a private static method on the FileChecker object to avoid an overly complex method. Closes #237
This commit is contained in:
parent
c17043ff3f
commit
7998734fe6
3 changed files with 61 additions and 8 deletions
1
tests/fixtures/example-code/invalid-syntax.py
vendored
Normal file
1
tests/fixtures/example-code/invalid-syntax.py
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
foo(
|
||||
25
tests/unit/test_file_checker.py
Normal file
25
tests/unit/test_file_checker.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
"""Unit tests for the FileChecker class."""
|
||||
import mock
|
||||
|
||||
from flake8 import checker
|
||||
|
||||
|
||||
@mock.patch('flake8.processor.FileProcessor')
|
||||
def test_run_ast_checks_handles_SyntaxErrors(FileProcessor):
|
||||
"""Stress our SyntaxError handling.
|
||||
|
||||
Related to: https://gitlab.com/pycqa/flake8/issues/237
|
||||
"""
|
||||
processor = mock.Mock(lines=[])
|
||||
FileProcessor.return_value = processor
|
||||
processor.build_ast.side_effect = SyntaxError('Failed to build ast',
|
||||
('', 1, 5, 'foo(\n'))
|
||||
file_checker = checker.FileChecker(__file__, checks={}, options=object())
|
||||
|
||||
with mock.patch.object(file_checker, 'report') as report:
|
||||
file_checker.run_ast_checks()
|
||||
|
||||
report.assert_called_once_with(
|
||||
'E999', 1, 3,
|
||||
'SyntaxError: Failed to build ast',
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue