From e1b5e98f12123dbc9d20925fdbc0f528ed3bf185 Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Thu, 15 Dec 2022 13:22:24 +0300 Subject: [PATCH] Add test for ModuleNotFoundError fix Refs #7938 --- .../importing_plugin/importing_plugin.py | 32 +++++++++++++++++++ .../regrtest_data/settings_project/models.py | 0 .../settings_project/settings.py | 0 tests/test_self.py | 13 ++++++++ 4 files changed, 45 insertions(+) create mode 100644 tests/regrtest_data/importing_plugin/importing_plugin.py create mode 100644 tests/regrtest_data/settings_project/models.py create mode 100644 tests/regrtest_data/settings_project/settings.py diff --git a/tests/regrtest_data/importing_plugin/importing_plugin.py b/tests/regrtest_data/importing_plugin/importing_plugin.py new file mode 100644 index 0000000000..227b82ed33 --- /dev/null +++ b/tests/regrtest_data/importing_plugin/importing_plugin.py @@ -0,0 +1,32 @@ +from importlib import import_module + +from pylint.checkers import BaseChecker +from pylint.lint.pylinter import PyLinter + + +class ImportingChecker(BaseChecker): + options = ( + ( + "settings-module", + { + "default": "settings", + "type": "string", + "metavar": "" + }, + ), + ) + + msgs = { + "E9999": ( + "Importing checker error message", + "importing-checker-error", + "Importing checker error message", + ), + } + + def open(self) -> None: + import_module(self.linter.config.settings_module) + + +def register(linter: "PyLinter") -> None: + linter.register_checker(ImportingChecker(linter)) diff --git a/tests/regrtest_data/settings_project/models.py b/tests/regrtest_data/settings_project/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regrtest_data/settings_project/settings.py b/tests/regrtest_data/settings_project/settings.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/test_self.py b/tests/test_self.py index 02e1a17e83..61b417f0c0 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -869,6 +869,19 @@ def test_modify_sys_path() -> None: modify_sys_path() assert sys.path == paths[1:] + @staticmethod + def test_plugin_that_imports_from_open() -> None: + """Test that a plugin that imports a source file from a checker open() + function (ala pylint_django) does not raise an exception.""" + with _test_sys_path(): + # Enable --load-plugins=importing_plugin + sys.path.append(join(HERE, "regrtest_data", "importing_plugin")) + with _test_cwd(join(HERE, "regrtest_data", "settings_project")): + Run( + ["--load-plugins=importing_plugin", "models.py"], + exit=False, + ) + @pytest.mark.parametrize( "args", [