summaryrefslogtreecommitdiff
path: root/pylint/testutils/decorator.py
blob: 87fbd7afa00785f0e33f2b71554241b698a81e82 (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
# 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

import functools
import optparse  # pylint: disable=deprecated-module

from pylint.lint import PyLinter
from pylint.testutils.checker_test_case import CheckerTestCase


def set_config(**kwargs):
    """Decorator for setting config values on a checker.

    Passing the args and kwargs back to the test function itself
    allows this decorator to be used on parametrized test cases.
    """

    def _wrapper(fun):
        @functools.wraps(fun)
        def _forward(self, *args, **test_function_kwargs):
            try:
                for key, value in kwargs.items():
                    self.checker.set_option(key.replace("_", "-"), value)
            except optparse.OptionError:
                # Check if option is one of the base options of the PyLinter class
                for key, value in kwargs.items():
                    try:
                        self.checker.set_option(
                            key.replace("_", "-"),
                            value,
                            optdict=dict(PyLinter.make_options())[
                                key.replace("_", "-")
                            ],
                        )
                    except KeyError:
                        # pylint: disable-next=fixme
                        # TODO: Find good way to double load checkers in unittests
                        # When options are used by multiple checkers we need to load both of them
                        # to be able to get an optdict
                        self.checker.set_option(
                            key.replace("_", "-"),
                            value,
                            optdict={},
                        )
            if isinstance(self, CheckerTestCase):
                # reopen checker in case, it may be interested in configuration change
                self.checker.open()
            fun(self, *args, **test_function_kwargs)

        return _forward

    return _wrapper


def set_config_directly(**kwargs):
    """Decorator for setting config values on a checker without validation.

    Some options should be declared in two different checkers. This is
    impossible without duplicating the option key. For example:
    "no-docstring-rgx" in DocstringParameterChecker & DocStringChecker
    This decorator allows to directly set such options.

    Passing the args and kwargs back to the test function itself
    allows this decorator to be used on parametrized test cases.
    """

    def _wrapper(fun):
        @functools.wraps(fun)
        def _forward(self, *args, **test_function_kwargs):
            for key, value in kwargs.items():
                setattr(self.checker.config, key, value)
            if isinstance(self, CheckerTestCase):
                # reopen checker in case, it may be interested in configuration change
                self.checker.open()
            fun(self, *args, **test_function_kwargs)

        return _forward

    return _wrapper