Skip to content

Commit

Permalink
[functional expected output] Makes auto-update works even if the curr…
Browse files Browse the repository at this point in the history
…ent output is wrong

Previously if an exception was raised during parsing we re-raised an explanation.
Most of the time I just want to regenerate the output from the current pylint's
output. It's what the exception suggested but it's impossible to do as the error
is also raised when updating, so you had to empty the file.
  • Loading branch information
Pierre-Sassoulas committed Jun 13, 2022
1 parent 003b472 commit b3e1688
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 47 deletions.
49 changes: 9 additions & 40 deletions pylint/testutils/output_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,6 @@ class MessageTest(NamedTuple):
"""


class MalformedOutputLineException(Exception):
def __init__(
self,
row: Sequence[str] | str,
exception: Exception,
) -> None:
example = "msg-symbolic-name:42:27:MyClass.my_function:The message"
other_example = "msg-symbolic-name:7:42::The message"
expected = [
"symbol",
"line",
"column",
"end_line",
"end_column",
"MyClass.myFunction, (or '')",
"Message",
"confidence",
]
reconstructed_row = ""
i = 0
try:
for i, column in enumerate(row):
reconstructed_row += f"\t{expected[i]}='{column}' ?\n"
for missing in expected[i + 1 :]:
reconstructed_row += f"\t{missing}= Nothing provided !\n"
except IndexError:
pass
raw = ":".join(row)
msg = f"""\
{exception}
Expected '{example}' or '{other_example}' but we got '{raw}':
{reconstructed_row}
Try updating it with: 'python tests/test_functional.py {UPDATE_OPTION}'"""
super().__init__(msg)


class OutputLine(NamedTuple):
symbol: str
lineno: int
Expand Down Expand Up @@ -127,6 +89,7 @@ def from_csv(
"""
if isinstance(row, str):
row = row.split(",")
# noinspection PyBroadException
try:
column = cls._get_column(row[2])
if len(row) == 5:
Expand Down Expand Up @@ -170,8 +133,14 @@ def from_csv(
row[7],
)
raise IndexError
except Exception as e:
raise MalformedOutputLineException(row, e) from e
except Exception: # pylint: disable=broad-except
warnings.warn(
"Expected 'msg-symbolic-name:42:27:MyClass.my_function:The message:"
f"CONFIDENCE' but we got '{':'.join(row)}'. Try updating the expected"
f" output with:\npython tests/test_functional.py {UPDATE_OPTION}",
UserWarning,
)
return cls("", 0, 0, None, None, "", "", "")

def to_csv(self) -> tuple[str, str, str, str, str, str, str, str]:
"""Convert an OutputLine to a tuple of string to be written by a
Expand Down
14 changes: 7 additions & 7 deletions tests/testutils/test_output_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pylint.constants import PY38_PLUS
from pylint.interfaces import HIGH, INFERENCE, Confidence
from pylint.message import Message
from pylint.testutils.output_line import MalformedOutputLineException, OutputLine
from pylint.testutils.output_line import OutputLine
from pylint.typing import MessageLocationTuple


Expand Down Expand Up @@ -127,20 +127,20 @@ def test_output_line_to_csv(confidence: Confidence, message: Callable) -> None:
def test_output_line_from_csv_error() -> None:
"""Test that errors are correctly raised for incorrect OutputLine's."""
# Test a csv-string which does not have a number for line and column
with pytest.raises(
MalformedOutputLineException,
with pytest.warns(
UserWarning,
match="msg-symbolic-name:42:27:MyClass.my_function:The message",
):
OutputLine.from_csv("'missing-docstring', 'line', 'column', 'obj', 'msg'", True)
# Test a tuple which does not have a number for line and column
with pytest.raises(
MalformedOutputLineException, match="symbol='missing-docstring' ?"
with pytest.warns(
UserWarning, match="we got 'missing-docstring:line:column:obj:msg'"
):
csv = ("missing-docstring", "line", "column", "obj", "msg")
OutputLine.from_csv(csv, True)
# Test a csv-string that is too long
with pytest.raises(
MalformedOutputLineException,
with pytest.warns(
UserWarning,
match="msg-symbolic-name:42:27:MyClass.my_function:The message",
):
OutputLine.from_csv(
Expand Down

0 comments on commit b3e1688

Please sign in to comment.