From c0fe1c5cfec2f2998bb3b86aae6297d83c5d7739 Mon Sep 17 00:00:00 2001 From: Akhil Kamat Date: Fri, 20 Sep 2024 14:44:25 -0400 Subject: [PATCH] Fix two incorrect/broken tests in `tests/checkers/unittest_imports.py` (#9911) --- tests/checkers/unittest_imports.py | 42 +++++++++++++++------------ tests/regrtest_data/dummy_wildcard.py | 0 2 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 tests/regrtest_data/dummy_wildcard.py diff --git a/tests/checkers/unittest_imports.py b/tests/checkers/unittest_imports.py index 2a5547d388..92477ac2ea 100644 --- a/tests/checkers/unittest_imports.py +++ b/tests/checkers/unittest_imports.py @@ -11,6 +11,7 @@ from pylint.checkers import imports from pylint.interfaces import UNDEFINED +from pylint.lint import augmented_sys_path, discover_package_path from pylint.testutils import CheckerTestCase, MessageTest from pylint.testutils._run import _Run as Run @@ -92,28 +93,33 @@ def test_relative_beyond_top_level_four(capsys: CaptureFixture[str]) -> None: assert errors == "" def test_wildcard_import_init(self) -> None: - module = astroid.MANAGER.ast_from_module_name("init_wildcard", REGR_DATA) - import_from = module.body[0] + context_file = os.path.join(REGR_DATA, "dummy_wildcard.py") - with self.assertNoMessages(): - self.checker.visit_importfrom(import_from) + with augmented_sys_path([discover_package_path(context_file, [])]): + module = astroid.MANAGER.ast_from_module_name("init_wildcard", context_file) + import_from = module.body[0] + + with self.assertNoMessages(): + self.checker.visit_importfrom(import_from) def test_wildcard_import_non_init(self) -> None: - module = astroid.MANAGER.ast_from_module_name("wildcard", REGR_DATA) - import_from = module.body[0] + context_file = os.path.join(REGR_DATA, "dummy_wildcard.py") - msg = MessageTest( - msg_id="wildcard-import", - node=import_from, - args="empty", - confidence=UNDEFINED, - line=1, - col_offset=0, - end_line=1, - end_col_offset=19, - ) - with self.assertAddsMessages(msg): - self.checker.visit_importfrom(import_from) + with augmented_sys_path([discover_package_path(context_file, [])]): + module = astroid.MANAGER.ast_from_module_name("wildcard", context_file) + import_from = module.body[0] + msg = MessageTest( + msg_id="wildcard-import", + node=import_from, + args="empty", + confidence=UNDEFINED, + line=1, + col_offset=0, + end_line=1, + end_col_offset=19, + ) + with self.assertAddsMessages(msg): + self.checker.visit_importfrom(import_from) @staticmethod def test_preferred_module(capsys: CaptureFixture[str]) -> None: diff --git a/tests/regrtest_data/dummy_wildcard.py b/tests/regrtest_data/dummy_wildcard.py new file mode 100644 index 0000000000..e69de29bb2