From 9a875006615c601343c9026acc88d2cae062161b 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 (#7938) --- .../importing_plugin/importing_plugin.py | 32 +++++++++++++++++++ .../regrtest_data/settings_project/models.py | 0 .../settings_project/settings.py | 0 tests/test_self.py | 17 ++++++++++ 4 files changed, 49 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 00000000000..227b82ed33d --- /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 00000000000..e69de29bb2d diff --git a/tests/regrtest_data/settings_project/settings.py b/tests/regrtest_data/settings_project/settings.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/test_self.py b/tests/test_self.py index 02e1a17e83d..586e048c8af 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -869,6 +869,23 @@ 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.""" + + project_path = join(HERE, "regrtest_data", "settings_project") + plugin_path = join(HERE, "regrtest_data", "importing_plugin") + # Enable --load-plugins=importing_plugin + sys.path.append(plugin_path) + with _test_cwd(): + os.chdir(project_path) + Run( + ["--load-plugins=importing_plugin", "models.py"], + exit=False, + ) + sys.path.remove(plugin_path) + @pytest.mark.parametrize( "args", [