aboutsummaryrefslogtreecommitdiff
path: root/tests/testutils/test_output_line.py
blob: cd8bc7e9bd4f68b23d3c52d0e7a01be275840477 (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
# 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

# pylint: disable=redefined-outer-name

from typing import Callable, List, Optional

import pytest

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


@pytest.fixture()
def message() -> Callable:
    def inner(confidence: Confidence = HIGH) -> Message:
        return Message(
            symbol="missing-docstring",
            msg_id="C0123",
            location=[
                "abspath",
                "path",
                "module",
                "obj",
                "line",
                "column",
            ],
            msg="msg",
            confidence=confidence,
        )

    return inner


def test_output_line() -> None:
    output_line = OutputLine(
        symbol="missing-docstring",
        lineno=0,
        column="0",
        object="",
        msg="Missing docstring's bad.",
        confidence=HIGH,
    )
    assert output_line.symbol == "missing-docstring"


def test_output_line_from_message(message: Callable) -> None:
    output_line = OutputLine.from_msg(message())
    assert output_line.symbol == "missing-docstring"
    assert output_line.msg == "msg"


@pytest.mark.parametrize("confidence", [HIGH, INFERENCE])
def test_output_line_to_csv(confidence: Confidence, message: Callable) -> None:
    output_line = OutputLine.from_msg(message(confidence))
    csv = output_line.to_csv()
    expected_column = "column" if PY38_PLUS else ""
    assert csv == (
        "missing-docstring",
        "line",
        expected_column,
        "obj",
        "msg",
        confidence.name,
    )


def test_output_line_from_csv_error() -> None:
    with pytest.raises(
        MalformedOutputLineException,
        match="msg-symbolic-name:42:27:MyClass.my_function:The message",
    ):
        OutputLine.from_csv("'missing-docstring', 'line', 'column', 'obj', 'msg'")
    with pytest.raises(
        MalformedOutputLineException, match="symbol='missing-docstring' ?"
    ):
        csv = ("missing-docstring", "line", "column", "obj", "msg")
        OutputLine.from_csv(csv)


@pytest.mark.parametrize(
    "confidence,expected_confidence", [[None, "HIGH"], ["INFERENCE", "INFERENCE"]]
)
def test_output_line_from_csv(
    confidence: Optional[str], expected_confidence: Confidence
) -> None:
    if confidence:
        proper_csv: List[str] = [
            "missing-docstring",
            "1",
            "2",
            "obj",
            "msg",
            confidence,
        ]
    else:
        proper_csv = ["missing-docstring", "1", "2", "obj", "msg"]
    output_line = OutputLine.from_csv(proper_csv)
    expected_column = "2" if PY38_PLUS else ""
    assert output_line == OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=expected_column,
        object="obj",
        msg="msg",
        confidence=expected_confidence,
    )