diff --git a/conman/nav_tree/tests/test_utils.py b/conman/nav_tree/tests/test_utils.py index 9d072c7..c0d65d4 100644 --- a/conman/nav_tree/tests/test_utils.py +++ b/conman/nav_tree/tests/test_utils.py @@ -41,15 +41,22 @@ def test_split_path_with_dots(self): class TestImportFromDottedPath(TestCase): + def assert_error_message(self, exception): + """Check the exception's message is correct.""" + message = 'An import path with two or more components is required.' + self.assertEqual(exception.args[0], message) + def test_empty(self): """An empty path cannot be imported.""" - with self.assertRaises(ValueError): + with self.assertRaises(ValueError) as cm: utils.import_from_dotted_path('') + self.assert_error_message(cm.exception) def test_too_short(self): """A path with only one component cannot be imported.""" - with self.assertRaises(ValueError): + with self.assertRaises(ValueError) as cm: utils.import_from_dotted_path('antigravity') + self.assert_error_message(cm.exception) def test_import_module(self): """A module can be imported by dotted path.""" diff --git a/conman/nav_tree/utils.py b/conman/nav_tree/utils.py index 455f27f..d21026b 100644 --- a/conman/nav_tree/utils.py +++ b/conman/nav_tree/utils.py @@ -20,6 +20,11 @@ def import_from_dotted_path(path): The path must have at least one dot. """ - module_path, attr = path.rsplit('.', 1) + try: + module_path, attr = path.rsplit('.', 1) + except ValueError: + message = 'An import path with two or more components is required.' + raise ValueError(message) from None + module = importlib.import_module(module_path) return getattr(module, attr)