summaryrefslogtreecommitdiff
path: root/pylint/test/unittest_reporting.py
blob: 14b05f0fcb9e5c666f4c592576a06a7900a420ab (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
# Copyright (c) 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2014 Calin Don <calin.don@gmail.com>
# Copyright (c) 2014 Google, Inc.
# Copyright (c) 2014 Arun Persaud <arun@nubati.net>
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2016-2017 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING

import warnings

from io import StringIO

import pylint.config
from pylint.lint import PluginRegistry, PyLinter
from pylint import checkers
from pylint.reporters.text import TextReporter, ParseableTextReporter
import pytest


@pytest.fixture(scope="module")
def reporter(reporter):
    return TextReporter


@pytest.fixture(scope="module")
def disable(disable):
    return ["I"]


def test_template_option(linter):
    output = StringIO()
    linter.reporter.set_output(output)
    linter.set_option("msg-template", "{msg_id}:{line:03d}")
    linter.open()
    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 (
        output.getvalue() == "************* Module 0123\n" "C0301:001\n" "C0301:002\n"
    )


def test_parseable_output_deprecated():
    with warnings.catch_warnings(record=True) as cm:
        warnings.simplefilter("always")
        ParseableTextReporter()

    assert len(cm) == 1
    assert isinstance(cm[0].message, DeprecationWarning)


def test_parseable_output_regression():
    output = StringIO()
    with warnings.catch_warnings(record=True):
        global_config = pylint.config.Configuration()
        linter = PyLinter(global_config)
        registry = PluginRegistry(linter, register_options=global_config.add_options)
        linter.set_reporter(ParseableTextReporter())

    checkers.initialize(registry)
    linter.config.persistent = 0
    linter.reporter.set_output(output)
    linter.config.output_format = "parseable"
    linter.open()
    linter.set_current_module("0123")
    linter.add_message("line-too-long", line=1, args=(1, 2))
    assert (
        output.getvalue() == "************* Module 0123\n"
        "0123:1: [C0301(line-too-long), ] "
        "Line too long (1/2)\n"
    )


def test_display_results_is_renamed():
    class CustomReporter(TextReporter):
        def _display(self, layout):
            return None

    reporter = CustomReporter()
    with pytest.raises(AttributeError):
        reporter.display_results