diff --git a/beets/test/helper.py b/beets/test/helper.py index aa883ade10..e2bf98dee4 100644 --- a/beets/test/helper.py +++ b/beets/test/helper.py @@ -221,41 +221,6 @@ def teardown_beets(self): beets.config.clear() beets.config._materialized = False - def load_plugins(self, *plugins): - """Load and initialize plugins by names. - - Similar setting a list of plugins in the configuration. Make - sure you call ``unload_plugins()`` afterwards. - """ - # FIXME this should eventually be handled by a plugin manager - plugins = (self.plugin,) if hasattr(self, "plugin") else plugins - beets.config["plugins"] = plugins - beets.plugins.load_plugins(plugins) - beets.plugins.find_plugins() - - # Take a backup of the original _types and _queries to restore - # when unloading. - Item._original_types = dict(Item._types) - Album._original_types = dict(Album._types) - Item._types.update(beets.plugins.types(Item)) - Album._types.update(beets.plugins.types(Album)) - - Item._original_queries = dict(Item._queries) - Album._original_queries = dict(Album._queries) - Item._queries.update(beets.plugins.named_queries(Item)) - Album._queries.update(beets.plugins.named_queries(Album)) - - def unload_plugins(self): - """Unload all plugins and remove them from the configuration.""" - # FIXME this should eventually be handled by a plugin manager - beets.config["plugins"] = [] - beets.plugins._classes = set() - beets.plugins._instances = {} - Item._types = getattr(Item, "_original_types", {}) - Album._types = getattr(Album, "_original_types", {}) - Item._queries = getattr(Item, "_original_queries", {}) - Album._queries = getattr(Album, "_original_queries", {}) - # Library fixtures methods def create_item(self, **values): @@ -485,15 +450,54 @@ def setUp(self): class PluginMixin: plugin: ClassVar[str] + preload_plugin: ClassVar[bool] = True def setUp(self): super().setUp() - self.load_plugins() + if self.preload_plugin: + self.load_plugins() def tearDown(self): super().tearDown() self.unload_plugins() + def load_plugins(self, *plugins: str) -> None: + """Load and initialize plugins by names. + + Similar setting a list of plugins in the configuration. Make + sure you call ``unload_plugins()`` afterwards. + """ + # FIXME this should eventually be handled by a plugin manager + plugins = (self.plugin,) if hasattr(self, "plugin") else plugins + beets.config["plugins"] = plugins + beets.plugins.load_plugins(plugins) + beets.plugins.find_plugins() + + # Take a backup of the original _types and _queries to restore + # when unloading. + Item._original_types = dict(Item._types) + Album._original_types = dict(Album._types) + Item._types.update(beets.plugins.types(Item)) + Album._types.update(beets.plugins.types(Album)) + + Item._original_queries = dict(Item._queries) + Album._original_queries = dict(Album._queries) + Item._queries.update(beets.plugins.named_queries(Item)) + Album._queries.update(beets.plugins.named_queries(Album)) + + def unload_plugins(self) -> None: + """Unload all plugins and remove them from the configuration.""" + # FIXME this should eventually be handled by a plugin manager + for plugin_class in beets.plugins._instances: + plugin_class.listeners = None + beets.config["plugins"] = [] + beets.plugins._classes = set() + beets.plugins._instances = {} + Item._types = getattr(Item, "_original_types", {}) + Album._types = getattr(Album, "_original_types", {}) + Item._queries = getattr(Item, "_original_queries", {}) + Album._queries = getattr(Album, "_original_queries", {}) + class PluginTestCase(PluginMixin, BeetsTestCase): pass diff --git a/test/plugins/test_advancedrewrite.py b/test/plugins/test_advancedrewrite.py index 782b779e82..55f090d74d 100644 --- a/test/plugins/test_advancedrewrite.py +++ b/test/plugins/test_advancedrewrite.py @@ -16,16 +16,15 @@ """ -from beets.test.helper import BeetsTestCase +from beets.test.helper import BeetsTestCase, PluginMixin from beets.ui import UserError PLUGIN_NAME = "advancedrewrite" -class AdvancedRewritePluginTest(BeetsTestCase): - def tearDown(self): - self.unload_plugins() - super().tearDown() +class AdvancedRewritePluginTest(PluginMixin, BeetsTestCase): + plugin = "advancedrewrite" + preload_plugin = False def test_simple_rewrite_example(self): self.config[PLUGIN_NAME] = [ diff --git a/test/plugins/test_edit.py b/test/plugins/test_edit.py index ba068e90dd..e15caaa57d 100644 --- a/test/plugins/test_edit.py +++ b/test/plugins/test_edit.py @@ -26,7 +26,6 @@ TerminalImportMixin, control_stdin, ) -from beetsplug.edit import EditPlugin class ModifyFileMocker: @@ -135,10 +134,6 @@ def setUp(self): {f: item[f] for f in item._fields} for item in self.album.items() ] - def tearDown(self): - EditPlugin.listeners = None - super().tearDown() - def assertCounts( # noqa self, mock_write, @@ -338,7 +333,6 @@ def setUp(self): self.matcher.matching = AutotagStub.GOOD def tearDown(self): - EditPlugin.listeners = None super().tearDown() self.matcher.restore() diff --git a/test/plugins/test_filefilter.py b/test/plugins/test_filefilter.py index 37058878ea..194cafff63 100644 --- a/test/plugins/test_filefilter.py +++ b/test/plugins/test_filefilter.py @@ -15,21 +15,18 @@ """Tests for the `filefilter` plugin. """ from beets import config -from beets.test.helper import ImportTestCase +from beets.test.helper import ImportTestCase, PluginMixin from beets.util import bytestring_path -from beetsplug.filefilter import FileFilterPlugin -class FileFilterPluginMixin(ImportTestCase): +class FileFilterPluginMixin(PluginMixin, ImportTestCase): + plugin = "filefilter" + preload_plugin = False + def setUp(self): super().setUp() self.prepare_tracks_for_import() - def tearDown(self): - self.unload_plugins() - FileFilterPlugin.listeners = None - super().tearDown() - def prepare_tracks_for_import(self): self.album_track, self.other_album_track, self.single_track = ( bytestring_path(self.prepare_album_for_import(1, album_path=p)[0]) diff --git a/test/plugins/test_hook.py b/test/plugins/test_hook.py index 8d44423181..f935039adc 100644 --- a/test/plugins/test_hook.py +++ b/test/plugins/test_hook.py @@ -19,7 +19,7 @@ import unittest from beets import config, plugins -from beets.test.helper import BeetsTestCase, capture_log +from beets.test.helper import BeetsTestCase, PluginMixin, capture_log def get_temporary_path(): @@ -29,13 +29,11 @@ def get_temporary_path(): return os.path.join(temporary_directory, temporary_name) -class HookTest(BeetsTestCase): +class HookTest(PluginMixin, BeetsTestCase): + plugin = "hook" + preload_plugin = False TEST_HOOK_COUNT = 5 - def tearDown(self): - self.unload_plugins() - super().tearDown() - def _add_hook(self, event, command): hook = {"event": event, "command": command} diff --git a/test/plugins/test_playlist.py b/test/plugins/test_playlist.py index 8a8ef85a77..53f4d8a4e4 100644 --- a/test/plugins/test_playlist.py +++ b/test/plugins/test_playlist.py @@ -18,10 +18,13 @@ import beets from beets.test import _common -from beets.test.helper import BeetsTestCase +from beets.test.helper import PluginTestCase -class PlaylistTestCase(BeetsTestCase): +class PlaylistTestCase(PluginTestCase): + plugin = "playlist" + preload_plugin = False + def setUp(self): super().setUp() @@ -77,15 +80,11 @@ def setUp(self): self.config["playlist"]["playlist_dir"] = self.playlist_dir self.setup_test() - self.load_plugins("playlist") + self.load_plugins() def setup_test(self): raise NotImplementedError - def tearDown(self): - self.unload_plugins() - super().tearDown() - class PlaylistQueryTest: def test_name_query_with_absolute_paths_in_playlist(self): diff --git a/test/plugins/test_replaygain.py b/test/plugins/test_replaygain.py index 0e7b663f5b..348725a6f2 100644 --- a/test/plugins/test_replaygain.py +++ b/test/plugins/test_replaygain.py @@ -19,7 +19,12 @@ from mediafile import MediaFile from beets import config -from beets.test.helper import AsIsImporterMixin, ImportTestCase, has_program +from beets.test.helper import ( + AsIsImporterMixin, + ImportTestCase, + PluginMixin, + has_program, +) from beetsplug.replaygain import ( FatalGstreamerPluginReplayGainError, GStreamerBackend, @@ -52,8 +57,11 @@ def reset_replaygain(item): item.store() -class ReplayGainTestCase(ImportTestCase): +class ReplayGainTestCase(PluginMixin, ImportTestCase): db_on_disk = True + plugin = "replaygain" + preload_plugin = False + backend: ClassVar[str] def setUp(self): @@ -63,14 +71,7 @@ def setUp(self): super().setUp() self.config["replaygain"]["backend"] = self.backend - try: - self.load_plugins("replaygain") - except Exception: - self.tearDown() - - def tearDown(self): - self.unload_plugins() - super().tearDown() + self.load_plugins() class ThreadedImportMixin: diff --git a/test/plugins/test_zero.py b/test/plugins/test_zero.py index 8b6427ae16..cc67d0095a 100644 --- a/test/plugins/test_zero.py +++ b/test/plugins/test_zero.py @@ -3,12 +3,15 @@ from mediafile import MediaFile from beets.library import Item -from beets.test.helper import BeetsTestCase, control_stdin +from beets.test.helper import BeetsTestCase, PluginMixin, control_stdin from beets.util import syspath from beetsplug.zero import ZeroPlugin -class ZeroPluginTest(BeetsTestCase): +class ZeroPluginTest(PluginMixin, BeetsTestCase): + plugin = "zero" + preload_plugin = False + def setUp(self): super().setUp() self.config["zero"] = { @@ -17,11 +20,6 @@ def setUp(self): "update_database": False, } - def tearDown(self): - ZeroPlugin.listeners = None - super().tearDown() - self.unload_plugins() - def test_no_patterns(self): self.config["zero"]["fields"] = ["comments", "month"] diff --git a/test/test_logging.py b/test/test_logging.py index 0cd5b68180..30d79f57e0 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -82,13 +82,6 @@ def setUp(self): beetsplug.dummy = self.DummyModule super().setUp() - def tearDown(self): - super().tearDown() - del beetsplug.dummy - sys.modules.pop("beetsplug.dummy") - self.DummyModule.DummyPlugin.listeners = None - self.DummyModule.DummyPlugin._raw_listeners = None - def test_command_level0(self): self.config["verbose"] = 0 with helper.capture_log() as logs: