summaryrefslogtreecommitdiff
path: root/pylint/testutils/reporter_for_tests.py
blob: 480f1ed2d29d6b075354c05d6f00ba1200e2fec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# 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 pylint import interfaces
from pylint.message import Message
from pylint.reporters import BaseReporter

if TYPE_CHECKING:
    from pylint.reporters.ureports.nodes import Section


class GenericTestReporter(BaseReporter):
    """reporter storing plain text messages"""

    __implements__ = interfaces.IReporter

    def __init__(
        self,
    ):  # pylint: disable=super-init-not-called # See https://github.com/PyCQA/pylint/issues/4941
        self.reset()

    def reset(self):
        self.message_ids: Dict = {}
        self.out = StringIO()
        self.path_strip_prefix: str = getcwd() + sep
        self.messages: List[str] = []

    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()
        for msg in self.messages:
            print(msg, file=self.out)
        result = self.out.getvalue()
        self.reset()
        return result

    # pylint: disable=unused-argument
    def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
        pass

    # pylint: enable=unused-argument

    def display_reports(self, layout: "Section") -> None:
        """ignore layouts"""

    def _display(self, layout: "Section") -> None:
        pass


class MinimalTestReporter(BaseReporter):
    def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
        self.messages = []

    def _display(self, layout: "Section") -> None:
        pass


class FunctionalTestReporter(BaseReporter):
    def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
        self.messages = []

    def display_reports(self, layout: "Section") -> None:
        """Ignore layouts and don't call self._display()."""

    def _display(self, layout: "Section") -> None:
        pass