|
| 1 | +from typing import Iterable |
| 2 | + |
| 3 | +import pytest |
| 4 | +from confuse import AttrDict |
| 5 | + |
| 6 | +from beets import metadata_plugins |
| 7 | +from beets.test.helper import PluginMixin |
| 8 | + |
| 9 | + |
| 10 | +class ErrorMetadataMockPlugin(metadata_plugins.MetadataSourcePlugin): |
| 11 | + """A metadata source plugin that raises errors in all its methods.""" |
| 12 | + |
| 13 | + def candidates(self, *args, **kwargs): |
| 14 | + raise ValueError("Mocked error") |
| 15 | + |
| 16 | + def item_candidates(self, *args, **kwargs): |
| 17 | + raise ValueError("Mocked error") |
| 18 | + |
| 19 | + def album_for_id(self, *args, **kwargs): |
| 20 | + raise ValueError("Mocked error") |
| 21 | + |
| 22 | + def track_for_id(self, *args, **kwargs): |
| 23 | + raise ValueError("Mocked error") |
| 24 | + |
| 25 | + def track_distance(self, *args, **kwargs): |
| 26 | + raise ValueError("Mocked error") |
| 27 | + |
| 28 | + def album_distance(self, *args, **kwargs): |
| 29 | + raise ValueError("Mocked error") |
| 30 | + |
| 31 | + |
| 32 | +class TestMetadataPluginsException(PluginMixin): |
| 33 | + """Check that errors during the metadata plugins do not crash beets. |
| 34 | + They should be logged as errors instead. |
| 35 | + """ |
| 36 | + |
| 37 | + @pytest.fixture(autouse=True) |
| 38 | + def setup(self): |
| 39 | + self.register_plugin(ErrorMetadataMockPlugin) |
| 40 | + yield |
| 41 | + self.unload_plugins() |
| 42 | + |
| 43 | + @pytest.mark.parametrize( |
| 44 | + "method_name,args", |
| 45 | + [ |
| 46 | + ("candidates", ()), |
| 47 | + ("item_candidates", ()), |
| 48 | + ("album_for_id", ("some_id",)), |
| 49 | + ("track_for_id", ("some_id",)), |
| 50 | + ("track_distance", (None, AttrDict({"data_source": "mock"}))), |
| 51 | + ("album_distance", (None, AttrDict({"data_source": "mock"}), None)), |
| 52 | + ], |
| 53 | + ) |
| 54 | + def test_error_handling_candidates( |
| 55 | + self, |
| 56 | + caplog, |
| 57 | + method_name, |
| 58 | + args, |
| 59 | + ): |
| 60 | + with caplog.at_level("ERROR"): |
| 61 | + # Call the method to trigger the error |
| 62 | + ret = getattr(metadata_plugins, method_name)(*args) |
| 63 | + if isinstance(ret, Iterable): |
| 64 | + list(ret) |
| 65 | + |
| 66 | + # Check that an error was logged |
| 67 | + assert len(caplog.records) == 1 |
| 68 | + logs = [record.getMessage() for record in caplog.records] |
| 69 | + assert logs == ["Error in 'ErrorMetadataMockPlugin': Mocked error"] |
| 70 | + caplog.clear() |
0 commit comments