|
| 1 | +from unittest.mock import patch, MagicMock |
| 2 | +from utilities_common.util_base import UtilHelper |
| 3 | + |
| 4 | + |
| 5 | +@patch("pkgutil.iter_modules") |
| 6 | +@patch("utilities_common.util_base.log.log_error") |
| 7 | +@patch("utilities_common.util_base.log.log_warning") |
| 8 | +def test_load_plugins_exceptions_logs(mock_log_warning, mock_log_error, mock_iter_modules): |
| 9 | + NON_EXISTENT_MODULE_NAME = "non-existent-module" |
| 10 | + FAILED_TO_IMPORT_MESSAGE = f"failed to import plugin {NON_EXISTENT_MODULE_NAME}" |
| 11 | + COMMON_EXCEPTION_MESSAGE = "Common exception" |
| 12 | + |
| 13 | + mock_iter_modules.return_value = [(None, NON_EXISTENT_MODULE_NAME, False)] |
| 14 | + plugins_namespace = MagicMock() |
| 15 | + plugins_namespace.__path__ = "some_path" |
| 16 | + plugins_namespace.__name__ = "some_name" |
| 17 | + |
| 18 | + # Assetion for ModuleNotFoundError |
| 19 | + list(UtilHelper().load_plugins(plugins_namespace)) |
| 20 | + mock_log_warning.assert_called_once_with( |
| 21 | + f"{FAILED_TO_IMPORT_MESSAGE}: No module named '{NON_EXISTENT_MODULE_NAME}'", |
| 22 | + also_print_to_console=True |
| 23 | + ) |
| 24 | + |
| 25 | + # Assertion for Exception |
| 26 | + with patch("importlib.import_module", side_effect=Exception(COMMON_EXCEPTION_MESSAGE)): |
| 27 | + list(UtilHelper().load_plugins(plugins_namespace)) |
| 28 | + mock_log_error.assert_called_once_with( |
| 29 | + f"{FAILED_TO_IMPORT_MESSAGE}: {COMMON_EXCEPTION_MESSAGE}", |
| 30 | + also_print_to_console=True |
| 31 | + ) |
0 commit comments