short circuit on ast error before tokenization error

This commit is contained in:
Anthony Sottile 2021-04-18 10:04:13 -07:00
parent 645cd71f57
commit 456e98486e
6 changed files with 47 additions and 94 deletions

View file

@ -1,6 +1,7 @@
"""Integration tests for the main entrypoint of flake8."""
import json
import os
import sys
from unittest import mock
import pytest
@ -186,8 +187,15 @@ def test_tokenization_error_but_not_syntax_error(tmpdir, capsys):
tmpdir.join("t.py").write("b'foo' \\\n")
_call_main(["t.py"], retv=1)
if hasattr(sys, "pypy_version_info"): # pragma: no cover (pypy)
expected = "t.py:2:1: E999 SyntaxError: end of file (EOF) in multi-line statement\n" # noqa: E501
elif sys.version_info < (3, 8): # pragma: no cover (<cp38)
expected = "t.py:2:1: E902 TokenError: EOF in multi-line statement\n"
else: # pragma: no cover (cp38+)
expected = "t.py:1:8: E999 SyntaxError: unexpected EOF while parsing\n"
out, err = capsys.readouterr()
assert out == "t.py:1:1: E902 TokenError: EOF in multi-line statement\n"
assert out == expected
assert err == ""
@ -197,8 +205,12 @@ def test_tokenization_error_is_a_syntax_error(tmpdir, capsys):
tmpdir.join("t.py").write("if True:\n pass\n pass\n")
_call_main(["t.py"], retv=1)
if hasattr(sys, "pypy_version_info"): # pragma: no cover (pypy)
expected = "t.py:3:2: E999 IndentationError: unindent does not match any outer indentation level\n" # noqa: E501
else: # pragma: no cover (cpython)
expected = "t.py:3:5: E999 IndentationError: unindent does not match any outer indentation level\n" # noqa: E501
out, err = capsys.readouterr()
expected = "t.py:1:1: E902 IndentationError: unindent does not match any outer indentation level\n" # noqa: E501
assert out == expected
assert err == ""

View file

@ -13,7 +13,6 @@ from flake8 import exceptions
plugin_name="plugin_name",
exception=ValueError("boom!"),
),
exceptions.InvalidSyntax(exception=ValueError("Unexpected token: $")),
exceptions.PluginRequestedUnknownParameters(
plugin={"plugin_name": "plugin_name"},
exception=ValueError("boom!"),

View file

@ -7,30 +7,6 @@ import flake8
from flake8 import checker
@mock.patch("flake8.processor.FileProcessor")
def test_run_ast_checks_handles_SyntaxErrors(FileProcessor): # noqa: N802,N803
"""Stress our SyntaxError handling.
Related to: https://github.com/pycqa/flake8/issues/169
"""
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",
)
@mock.patch("flake8.checker.FileChecker._make_processor", return_value=None)
def test_repr(*args):
"""Verify we generate a correct repr."""