diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-09-29 10:21:52 +0200 |
---|---|---|
committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-09-29 14:17:31 +0200 |
commit | 7c06fe8641293745417e3524577e668cc80710f5 (patch) | |
tree | 3af8f0225902397e9130bce911a46de916e8acfa /pylint/testutils | |
parent | 20304a6cfb0dedec8ca7878822b8db1fd3cc71cc (diff) | |
download | pylint-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.
Diffstat (limited to 'pylint/testutils')
-rw-r--r-- | pylint/testutils/decorator.py | 38 |
1 files changed, 38 insertions, 0 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): |