summaryrefslogtreecommitdiff
path: root/tests/test_pragma_parser.py
blob: b686c0042fe76b8196175be85da1f6d8a8848f70 (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
# 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

import pytest

from pylint.utils.pragma_parser import (
    OPTION_PO,
    InvalidPragmaError,
    UnRecognizedOptionError,
    parse_pragma,
)


def test_simple_pragma() -> None:
    comment = "#pylint: disable = missing-docstring"
    match = OPTION_PO.search(comment)
    assert match
    for pragma_repr in parse_pragma(match.group(2)):
        assert pragma_repr.action == "disable"
        assert pragma_repr.messages == ["missing-docstring"]


def test_disable_checker_with_number_in_name() -> None:
    comment = "#pylint: disable = j3-custom-checker"
    match = OPTION_PO.search(comment)
    assert match
    for pragma_repr in parse_pragma(match.group(2)):
        assert pragma_repr.action == "disable"
        assert pragma_repr.messages == ["j3-custom-checker"]


def test_simple_pragma_no_messages() -> None:
    comment = "#pylint: skip-file"
    match = OPTION_PO.search(comment)
    assert match
    for pragma_repr in parse_pragma(match.group(2)):
        assert pragma_repr.action == "skip-file"
        assert not pragma_repr.messages


def test_simple_pragma_multiple_messages() -> None:
    comment = "#pylint: disable = missing-docstring, invalid-name"
    match = OPTION_PO.search(comment)
    assert match
    for pragma_repr in parse_pragma(match.group(2)):
        assert pragma_repr.action == "disable"
        assert pragma_repr.messages == ["missing-docstring", "invalid-name"]


def test_multiple_pragma_multiple_messages() -> None:
    comment = "#pylint: disable = missing-docstring, invalid-name, enable = R0202, no-staticmethod-decorator"
    match = OPTION_PO.search(comment)
    assert match
    res = list(parse_pragma(match.group(2)))
    assert res[0].action == "disable"
    assert res[0].messages == ["missing-docstring", "invalid-name"]
    assert res[1].action == "enable"
    assert res[1].messages == ["R0202", "no-staticmethod-decorator"]


def test_missing_assignment() -> None:
    comment = "#pylint: disable missing-docstring"
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(InvalidPragmaError):
        list(parse_pragma(match.group(2)))


def test_missing_keyword() -> None:
    comment = "#pylint: = missing-docstring"
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(InvalidPragmaError):
        list(parse_pragma(match.group(2)))


def test_unsupported_assignment() -> None:
    comment = "#pylint: disable-all = missing-docstring"
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(UnRecognizedOptionError):
        list(parse_pragma(match.group(2)))


def test_unknown_keyword_with_messages() -> None:
    comment = "#pylint: unknown-keyword = missing-docstring"
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(UnRecognizedOptionError):
        list(parse_pragma(match.group(2)))


def test_unknown_keyword_with_missing_messages() -> None:
    comment = "#pylint: unknown-keyword = "
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(UnRecognizedOptionError):
        list(parse_pragma(match.group(2)))


def test_unknown_keyword_without_messages() -> None:
    comment = "#pylint: unknown-keyword"
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(UnRecognizedOptionError):
        list(parse_pragma(match.group(2)))


def test_missing_message() -> None:
    comment = "#pylint: disable = "
    match = OPTION_PO.search(comment)
    assert match
    with pytest.raises(InvalidPragmaError):
        list(parse_pragma(match.group(2)))


def test_parse_message_with_dash() -> None:
    comment = "#pylint: disable = raw_input-builtin"
    match = OPTION_PO.search(comment)
    assert match
    res = list(parse_pragma(match.group(2)))
    assert res[0].action == "disable"
    assert res[0].messages == ["raw_input-builtin"]