|
4 | 4 |
|
5 | 5 | from typing import Any |
6 | 6 | from unittest import TestCase |
| 7 | +from unittest.mock import Mock, patch |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 | import xarray as xr |
10 | 11 |
|
11 | | -from xrlint.config import Config, ConfigObject, get_entry_point_plugins |
| 12 | +from xrlint.config import ( |
| 13 | + Config, |
| 14 | + ConfigObject, |
| 15 | + get_entry_point_plugins, |
| 16 | + plugins_from_entry_points, |
| 17 | +) |
12 | 18 | from xrlint.constants import CORE_PLUGIN_NAME |
13 | 19 | from xrlint.plugin import Plugin, new_plugin |
14 | 20 | from xrlint.processor import ProcessorOp, define_processor |
@@ -50,6 +56,43 @@ def test_get_rule(self): |
50 | 56 | with pytest.raises(ValueError, match="unknown rule 'foo'"): |
51 | 57 | config_obj.get_rule("foo") |
52 | 58 |
|
| 59 | + def test_plugins_from_entry_points_load_failure(self): |
| 60 | + """Test that plugins_from_entry_points raises ValueError when entry point fails to load.""" |
| 61 | + # Create a mock entry point that raises an exception when loaded |
| 62 | + mock_entry_point = Mock() |
| 63 | + mock_entry_point.name = "failing-plugin" |
| 64 | + mock_entry_point.load.side_effect = ImportError("Module not found") |
| 65 | + |
| 66 | + # Mock the entry_points function to return our failing entry point |
| 67 | + with patch("importlib.metadata.entry_points") as mock_entry_points: |
| 68 | + mock_entry_points.return_value = [mock_entry_point] |
| 69 | + |
| 70 | + # Verify it raises the expected ValueError with the proper message |
| 71 | + with pytest.raises( |
| 72 | + ValueError, |
| 73 | + match=r"failed to load xrlint plugin from entry point 'failing-plugin'", |
| 74 | + ): |
| 75 | + plugins_from_entry_points() |
| 76 | + |
| 77 | + def test_plugins_from_entry_points_export_failure(self): |
| 78 | + """Test when the plugin module loads but export_plugin() fails.""" |
| 79 | + # Create a mock module that loads successfully but export_plugin() fails |
| 80 | + mock_module = Mock() |
| 81 | + mock_module.export_plugin.side_effect = AttributeError("missing attribute") |
| 82 | + |
| 83 | + mock_entry_point = Mock() |
| 84 | + mock_entry_point.name = "bad-export-plugin" |
| 85 | + mock_entry_point.load.return_value = mock_module |
| 86 | + |
| 87 | + with patch("importlib.metadata.entry_points") as mock_entry_points: |
| 88 | + mock_entry_points.return_value = [mock_entry_point] |
| 89 | + |
| 90 | + with pytest.raises( |
| 91 | + ValueError, |
| 92 | + match=r"failed to load xrlint plugin from entry point 'bad-export-plugin'", |
| 93 | + ): |
| 94 | + plugins_from_entry_points() |
| 95 | + |
53 | 96 | def test_get_processor_op(self): |
54 | 97 | class MyProc(ProcessorOp): |
55 | 98 | def preprocess( |
|
0 commit comments