summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2021-09-29 10:21:52 +0200
committerDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2021-09-29 14:17:31 +0200
commit7c06fe8641293745417e3524577e668cc80710f5 (patch)
tree3af8f0225902397e9130bce911a46de916e8acfa
parent20304a6cfb0dedec8ca7878822b8db1fd3cc71cc (diff)
downloadpylint-git-7c06fe8641293745417e3524577e668cc80710f5.tar.gz
Make test decorator use ``checker.set_option()``
This makes unittests with options mimick the normal option setting behaviour. It also adds ``set_config_directly()`` for those cases where this is not possible.
-rw-r--r--pylint/testutils/decorator.py38
-rw-r--r--tests/extensions/test_check_docs.py5
2 files changed, 41 insertions, 2 deletions
diff --git a/pylint/testutils/decorator.py b/pylint/testutils/decorator.py
index 44d186d1e..d635c7f3c 100644
--- a/pylint/testutils/decorator.py
+++ b/pylint/testutils/decorator.py
@@ -2,7 +2,9 @@
# 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
@@ -16,6 +18,42 @@ def set_config(**kwargs):
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():
+ self.checker.set_option(
+ key.replace("_", "-"),
+ value,
+ optdict=dict(PyLinter.make_options()),
+ )
+ 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):
diff --git a/tests/extensions/test_check_docs.py b/tests/extensions/test_check_docs.py
index 6676c13b4..65d077aed 100644
--- a/tests/extensions/test_check_docs.py
+++ b/tests/extensions/test_check_docs.py
@@ -31,6 +31,7 @@ from astroid import nodes
from pylint.extensions.docparams import DocstringParameterChecker
from pylint.testutils import CheckerTestCase, MessageTest, set_config
+from pylint.testutils.decorator import set_config_directly
class TestParamDocChecker(CheckerTestCase):
@@ -2324,7 +2325,7 @@ class TestParamDocChecker(CheckerTestCase):
):
self.checker.visit_functiondef(node)
- @set_config(no_docstring_rgx=r"^_(?!_).*$")
+ @set_config_directly(no_docstring_rgx=r"^_(?!_).*$")
def test_skip_no_docstring_rgx(self) -> None:
"""Example of a function that matches the default 'no-docstring-rgx' config option
@@ -2342,7 +2343,7 @@ class TestParamDocChecker(CheckerTestCase):
with self.assertNoMessages():
self.checker.visit_functiondef(node)
- @set_config(docstring_min_length=3)
+ @set_config_directly(docstring_min_length=3)
def test_skip_docstring_min_length(self) -> None:
"""Example of a function that is less than 'docstring-min-length' config option