From 7c06fe8641293745417e3524577e668cc80710f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 29 Sep 2021 10:21:52 +0200 Subject: 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. --- pylint/testutils/decorator.py | 38 +++++++++++++++++++++++++++++++++++++ tests/extensions/test_check_docs.py | 5 +++-- 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 @@ -13,6 +15,42 @@ def set_config(**kwargs): 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(): + 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): 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 -- cgit v1.2.1