|
| 1 | +import typing as t |
| 2 | +from importlib import metadata |
| 3 | +from importlib.metadata import EntryPoint |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from plux.runtime.metadata import EntryPointsResolver, build_entry_point_index |
| 9 | +from plux.runtime.resolve import MetadataPluginFinder |
| 10 | + |
| 11 | + |
| 12 | +class DummyEntryPointsResolver(EntryPointsResolver): |
| 13 | + entry_points: t.Dict[str, t.List[metadata.EntryPoint]] |
| 14 | + |
| 15 | + def __init__(self, entry_points: t.List[metadata.EntryPoint]): |
| 16 | + self.entry_points = build_entry_point_index(entry_points) |
| 17 | + |
| 18 | + def get_entry_points(self) -> t.Dict[str, t.List[metadata.EntryPoint]]: |
| 19 | + return self.entry_points |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def dummy_entry_point_resolver(): |
| 24 | + return DummyEntryPointsResolver( |
| 25 | + [ |
| 26 | + EntryPoint( |
| 27 | + group="namespace_1", |
| 28 | + name="plugin_1", |
| 29 | + value="tests.plugins.sample_plugins:plugin_spec_1", |
| 30 | + ), |
| 31 | + EntryPoint( |
| 32 | + group="namespace_1", |
| 33 | + name="plugin_2", |
| 34 | + value="tests.plugins.sample_plugins:plugin_spec_2", |
| 35 | + ), |
| 36 | + EntryPoint( |
| 37 | + group="namespace_2", |
| 38 | + name="cannot-be-loaded", |
| 39 | + value="tests.plugins.invalid_module:CannotBeLoadedPlugin", |
| 40 | + ), |
| 41 | + EntryPoint( |
| 42 | + group="namespace_2", |
| 43 | + name="simple", |
| 44 | + value="tests.plugins.sample_plugins:SimplePlugin", |
| 45 | + ), |
| 46 | + ] |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_resolve_error(dummy_entry_point_resolver): |
| 51 | + mock = MagicMock() |
| 52 | + finder = MetadataPluginFinder( |
| 53 | + "namespace_2", |
| 54 | + on_resolve_exception_callback=mock, |
| 55 | + entry_points_resolver=dummy_entry_point_resolver, |
| 56 | + ) |
| 57 | + plugins = finder.find_plugins() |
| 58 | + assert len(plugins) == 1 |
| 59 | + assert plugins[0].name == "simple" |
| 60 | + assert mock.call_count == 1 |
| 61 | + assert mock.call_args[0][0] == "namespace_2" |
| 62 | + assert mock.call_args[0][1] == EntryPoint( |
| 63 | + "cannot-be-loaded", |
| 64 | + "tests.plugins.invalid_module:CannotBeLoadedPlugin", |
| 65 | + "namespace_2", |
| 66 | + ) |
| 67 | + assert str(mock.call_args[0][2]) == "this is an expected exception" |
0 commit comments