summaryrefslogtreecommitdiff
path: root/tests/message/unittest_message_definition_store.py
blob: a8a0043facd9822ce56f4c96b1eae7c3b201944c (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

from contextlib import redirect_stdout
from io import StringIO

import pytest
from pytest import CaptureFixture

from pylint.checkers import BaseChecker
from pylint.exceptions import InvalidMessageError, UnknownMessageError
from pylint.lint.pylinter import PyLinter
from pylint.message import MessageDefinition
from pylint.message.message_definition_store import MessageDefinitionStore
from pylint.typing import MessageDefinitionTuple


@pytest.mark.parametrize(
    "messages,expected",
    [
        (
            {
                "W1234": ("message one", "msg-symbol-one", "msg description"),
                "W4321": ("message two", "msg-symbol-two", "msg description"),
            },
            r"Inconsistent checker part in message id 'W4321' (expected 'x12xx' because we"
            r" already had ['W1234']).",
        ),
        (
            {
                "W1233": (
                    "message two",
                    "msg-symbol-two",
                    "msg description",
                    {"old_names": [("W1234", "old-symbol")]},
                ),
                "W1234": ("message one", "msg-symbol-one", "msg description"),
            },
            "Message id 'W1234' cannot have both 'msg-symbol-one' and 'old-symbol' as symbolic name.",
        ),
        (
            {
                "W1234": ("message one", "msg-symbol-one", "msg description"),
                "W1235": (
                    "message two",
                    "msg-symbol-two",
                    "msg description",
                    {"old_names": [("W1234", "old-symbol")]},
                ),
            },
            "Message id 'W1234' cannot have both 'msg-symbol-one' and 'old-symbol' as symbolic name.",
        ),
        (
            {
                "W1234": (
                    "message one",
                    "msg-symbol-one",
                    "msg description",
                    {"old_names": [("W1201", "old-symbol-one")]},
                ),
                "W1235": (
                    "message two",
                    "msg-symbol-two",
                    "msg description",
                    {"old_names": [("W1201", "old-symbol-two")]},
                ),
            },
            "Message id 'W1201' cannot have both 'old-symbol-one' and 'old-symbol-two' as symbolic name.",
        ),
        (
            {
                "W1234": ("message one", "msg-symbol", "msg description"),
                "W1235": ("message two", "msg-symbol", "msg description"),
            },
            "Message symbol 'msg-symbol' cannot be used for 'W1234' and 'W1235' at the same time. "
            "If you're creating an 'old_names' use 'old-msg-symbol' as the old symbol.",
        ),
        (
            {
                "W1233": (
                    "message two",
                    "msg-symbol-two",
                    "msg description",
                    {"old_names": [("W1230", "msg-symbol-one")]},
                ),
                "W1234": ("message one", "msg-symbol-one", "msg description"),
            },
            "Message symbol 'msg-symbol-one' cannot be used for 'W1230' and 'W1234' at the same time."
            " If you're creating an 'old_names' use 'old-msg-symbol-one' as the old symbol.",
        ),
        (
            {
                "W1234": ("message one", "msg-symbol-one", "msg description"),
                "W1235": (
                    "message two",
                    "msg-symbol-two",
                    "msg description",
                    {"old_names": [("W1230", "msg-symbol-one")]},
                ),
            },
            "Message symbol 'msg-symbol-one' cannot be used for 'W1230' and 'W1234' at the same time. "
            "If you're creating an 'old_names' use 'old-msg-symbol-one' as the old symbol.",
        ),
        (
            {
                "W1234": (
                    "message one",
                    "msg-symbol-one",
                    "msg description",
                    {"old_names": [("W1230", "old-symbol-one")]},
                ),
                "W1235": (
                    "message two",
                    "msg-symbol-two",
                    "msg description",
                    {"old_names": [("W1231", "old-symbol-one")]},
                ),
            },
            "Message symbol 'old-symbol-one' cannot be used for 'W1230' and 'W1231' at the same time. "
            "If you're creating an 'old_names' use 'old-old-symbol-one' as the old symbol.",
        ),
    ],
)
def test_register_error(
    empty_store: MessageDefinitionStore,
    messages: dict[str, MessageDefinitionTuple],
    expected: str,
) -> None:
    class Checker(BaseChecker):
        def __init__(self) -> None:
            super().__init__(PyLinter())

        name = "checker"
        msgs = messages

    with pytest.raises(InvalidMessageError) as cm:
        empty_store.register_messages_from_checker(Checker())
    assert str(cm.value) == expected


def test_register_error_new_id_duplicate_of_new(
    empty_store: MessageDefinitionStore,
) -> None:
    class CheckerOne(BaseChecker):
        def __init__(self) -> None:
            super().__init__(PyLinter())

        name = "checker_one"
        msgs = {"W1234": ("message one", "msg-symbol-one", "msg description.")}

    class CheckerTwo(BaseChecker):
        def __init__(self) -> None:
            super().__init__(PyLinter())

        name = "checker_two"
        msgs = {"W1234": ("message two", "msg-symbol-two", "another msg description.")}

    empty_store.register_messages_from_checker(CheckerOne())
    test_register_error(
        empty_store,
        CheckerTwo.msgs,
        "Message id 'W1234' cannot have both 'msg-symbol-one' and 'msg-symbol-two' as symbolic name.",
    )


def test_format_help(capsys: CaptureFixture, store: MessageDefinitionStore) -> None:
    store.help_message([])
    captured = capsys.readouterr()
    assert captured.out == ""
    store.help_message(["W1234", "E1234", "C1234"])
    captured = capsys.readouterr()
    assert (
        captured.out
        == """:msg-symbol (W1234): *message*
  msg description. This message belongs to the achecker checker.

:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call*
  Used when a function call passes the same keyword argument multiple times.
  This message belongs to the achecker checker. It can't be emitted when using
  Python >= 2.6.

No such message id or symbol 'C1234'.

"""
    )


def test_get_msg_display_string(store: MessageDefinitionStore) -> None:
    assert store.get_msg_display_string("W1234") == "'msg-symbol'"
    assert store.get_msg_display_string("E1234") == "'duplicate-keyword-arg'"


def test_check_message_id(store: MessageDefinitionStore) -> None:
    w1234 = store.get_message_definitions("W1234")[0]
    w0001 = store.get_message_definitions("W0001")[0]
    e1234 = store.get_message_definitions("E1234")[0]
    old_symbol = store.get_message_definitions("old-symbol")[0]
    assert isinstance(w1234, MessageDefinition)
    assert isinstance(e1234, MessageDefinition)
    assert w1234 == w0001
    assert w1234 == old_symbol
    with pytest.raises(UnknownMessageError):
        store.get_message_definitions("YB12")


class TestMessageDefinitionStore:
    @staticmethod
    def _compare_messages(
        desc: str, msg: MessageDefinition, checkerref: bool = False
    ) -> None:
        assert desc == msg.format_help(checkerref=checkerref)

    def test_message_help(self, store: MessageDefinitionStore) -> None:
        message_definition = store.get_message_definitions("W1234")[0]
        self._compare_messages(
            """:msg-symbol (W1234): *message*
  msg description. This message belongs to the achecker checker.""",
            message_definition,
            checkerref=True,
        )
        self._compare_messages(
            """:msg-symbol (W1234): *message*
  msg description.""",
            message_definition,
            checkerref=False,
        )

    def test_message_help_minmax(self, store: MessageDefinitionStore) -> None:
        # build the message manually to be python version independent
        message_definition = store.get_message_definitions("E1234")[0]
        self._compare_messages(
            """:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call*
  Used when a function call passes the same keyword argument multiple times.
  This message belongs to the achecker checker. It can't be emitted when using
  Python >= 2.6.""",
            message_definition,
            checkerref=True,
        )
        self._compare_messages(
            """:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call*
  Used when a function call passes the same keyword argument multiple times.
  This message can't be emitted when using Python >= 2.6.""",
            message_definition,
            checkerref=False,
        )


def test_list_messages(store: MessageDefinitionStore) -> None:
    output = StringIO()
    with redirect_stdout(output):
        store.list_messages()
    # cursory examination of the output: we're mostly testing it completes
    assert ":msg-symbol (W1234): *message*" in output.getvalue()


def test_renamed_message_register(store: MessageDefinitionStore) -> None:
    assert store.get_message_definitions("W0001")[0].symbol == "msg-symbol"
    assert store.get_message_definitions("old-symbol")[0].symbol == "msg-symbol"


def test_multiple_child_of_old_name(store: MessageDefinitionStore) -> None:
    """We can define multiple name with the same old name."""

    class FamillyChecker(BaseChecker):
        def __init__(self) -> None:
            super().__init__(PyLinter())

        name = "famillychecker"
        msgs = {
            "W1235": (
                "Child 1",
                "child-one",
                "Child one description.",
                {"old_names": [("C1234", "mother")]},
            ),
            "W1236": (
                "Child 2",
                "child-two",
                "Child two description",
                {"old_names": [("C1234", "mother")]},
            ),
        }

    store.register_messages_from_checker(FamillyChecker())
    mother = store.get_message_definitions("C1234")
    child = store.get_message_definitions("W1235")
    other_child = store.get_message_definitions("W1236")
    assert len(mother) == 2
    assert len(child) == 1
    assert len(other_child) == 1
    assert child[0] in mother
    assert other_child[0] in mother