summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2021-10-02 23:13:23 +0200
committerDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2021-10-06 21:51:34 +0200
commit6ad9d6ce38988c55fa8d2c0cccfe03df8a08de54 (patch)
treed9e12040fc2ff8b8d0ffd941d25e03f44cf572cf
parent7bc962ba81966b61b5b626cbfce36dd9279226c7 (diff)
downloadpylint-git-6ad9d6ce38988c55fa8d2c0cccfe03df8a08de54.tar.gz
Refactor test reporter
-rw-r--r--pylint/testutils/reporter_for_tests.py49
-rw-r--r--tests/lint/unittest_lint.py54
2 files changed, 71 insertions, 32 deletions
diff --git a/pylint/testutils/reporter_for_tests.py b/pylint/testutils/reporter_for_tests.py
index 480f1ed2d..264ca9294 100644
--- a/pylint/testutils/reporter_for_tests.py
+++ b/pylint/testutils/reporter_for_tests.py
@@ -2,8 +2,8 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
from io import StringIO
-from os import getcwd, linesep, sep
-from typing import TYPE_CHECKING, Dict, List, Optional
+from os import getcwd, sep
+from typing import TYPE_CHECKING, List, Optional
from pylint import interfaces
from pylint.message import Message
@@ -17,38 +17,35 @@ class GenericTestReporter(BaseReporter):
"""reporter storing plain text messages"""
__implements__ = interfaces.IReporter
+ out: StringIO
- def __init__(
+ def __init__( # pylint: disable=super-init-not-called # See https://github.com/PyCQA/pylint/issues/4941
self,
- ): # pylint: disable=super-init-not-called # See https://github.com/PyCQA/pylint/issues/4941
+ ) -> None:
+ self.path_strip_prefix: str = getcwd() + sep
self.reset()
- def reset(self):
- self.message_ids: Dict = {}
+ def reset(self) -> None:
self.out = StringIO()
- self.path_strip_prefix: str = getcwd() + sep
- self.messages: List[str] = []
+ self.messages: List[Message] = []
def handle_message(self, msg: Message) -> None:
- """manage message of different type and in the context of path"""
- obj = msg.obj
- line = msg.line
- msg_id = msg.msg_id
- str_message: str = msg.msg
- self.message_ids[msg_id] = 1
- if obj:
- obj = f":{obj}"
- sigle = msg_id[0]
- if linesep != "\n":
- # 2to3 writes os.linesep instead of using
- # the previously used line separators
- str_message = str_message.replace("\r\n", "\n")
- self.messages.append(f"{sigle}:{line:>3}{obj}: {str_message}")
-
- def finalize(self):
- self.messages.sort()
+ """Append messages to the list of messages of the reporter"""
+ self.messages.append(msg)
+
+ def finalize(self) -> str:
+ """Format and print messages in the context of the path"""
+ messages: List[str] = []
for msg in self.messages:
- print(msg, file=self.out)
+ obj = ""
+ if msg.obj:
+ obj = f":{msg.obj}"
+ messages.append(f"{msg.msg_id[0]}:{msg.line:>3}{obj}: {msg.msg}")
+
+ messages.sort()
+ for message in messages:
+ print(message, file=self.out)
+
result = self.out.getvalue()
self.reset()
return result
diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py
index 5cb88f043..277799676 100644
--- a/tests/lint/unittest_lint.py
+++ b/tests/lint/unittest_lint.py
@@ -63,7 +63,9 @@ from pylint.constants import (
)
from pylint.exceptions import InvalidMessageError
from pylint.lint import ArgumentPreprocessingError, PyLinter, Run, preprocess_options
+from pylint.message import Message
from pylint.reporters import text
+from pylint.typing import MessageLocationTuple
from pylint.utils import FileState, tokenize_module
if os.name == "java":
@@ -487,10 +489,31 @@ def test_addmessage(linter: PyLinter) -> None:
linter.set_current_module("0123")
linter.add_message("C0301", line=1, args=(1, 2))
linter.add_message("line-too-long", line=2, args=(3, 4))
- assert [
- "C: 1: Line too long (1/2)",
- "C: 2: Line too long (3/4)",
- ] == linter.reporter.messages
+ assert len(linter.reporter.messages) == 2
+ assert linter.reporter.messages[0] == Message(
+ msg_id="C0301",
+ symbol="line-too-long",
+ msg="Line too long (1/2)",
+ confidence=interfaces.Confidence(
+ name="UNDEFINED",
+ description="Warning without any associated confidence level.",
+ ),
+ location=MessageLocationTuple(
+ abspath="0123", path="0123", module="0123", obj="", line=1, column=0
+ ),
+ )
+ assert linter.reporter.messages[1] == Message(
+ msg_id="C0301",
+ symbol="line-too-long",
+ msg="Line too long (3/4)",
+ confidence=interfaces.Confidence(
+ name="UNDEFINED",
+ description="Warning without any associated confidence level.",
+ ),
+ location=MessageLocationTuple(
+ abspath="0123", path="0123", module="0123", obj="", line=2, column=0
+ ),
+ )
def test_addmessage_invalid(linter: PyLinter) -> None:
@@ -574,7 +597,26 @@ def test_init_hooks_called_before_load_plugins() -> None:
def test_analyze_explicit_script(linter: PyLinter) -> None:
linter.set_reporter(testutils.GenericTestReporter())
linter.check([os.path.join(DATA_DIR, "ascript")])
- assert ["C: 2: Line too long (175/100)"] == linter.reporter.messages
+ assert len(linter.reporter.messages) == 1
+ assert linter.reporter.messages[0] == Message(
+ msg_id="C0301",
+ symbol="line-too-long",
+ msg="Line too long (175/100)",
+ confidence=interfaces.Confidence(
+ name="UNDEFINED",
+ description="Warning without any associated confidence level.",
+ ),
+ location=MessageLocationTuple(
+ abspath=os.path.join(abspath(dirname(__file__)), "ascript").replace(
+ f"lint{os.path.sep}ascript", f"data{os.path.sep}ascript"
+ ),
+ path=f"tests{os.path.sep}data{os.path.sep}ascript",
+ module="data.ascript",
+ obj="",
+ line=2,
+ column=0,
+ ),
+ )
def test_full_documentation(linter: PyLinter) -> None:
@@ -774,7 +816,7 @@ def test_custom_should_analyze_file() -> None:
messages = reporter.messages
assert len(messages) == 1
- assert "invalid syntax" in messages[0]
+ assert "invalid syntax" in messages[0].msg
# we do the check with jobs=1 as well, so that we are sure that the duplicates