Skip to content

Commit 595c2b2

Browse files
committed
lyrics: isolate test configuration
Create 'helpers.ConfigMixin' which sets up testing configuration. This is helpful for tests (e.g. test_lyrics.py) that only need the configuration and do not require temp dir. (#5102) Refactor lyrics tests to fix the issue global beets config issue. Additionally, add 'integration_test' mark that can be used to mark tests that should only run once a week.
1 parent c662a92 commit 595c2b2

File tree

3 files changed

+147
-205
lines changed

3 files changed

+147
-205
lines changed

beets/test/helper.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
import beets
5353
import beets.plugins
54-
from beets import autotag, config, importer, logging, util
54+
from beets import autotag, importer, logging, util
5555
from beets.autotag.hooks import AlbumInfo, TrackInfo
5656
from beets.importer import ImportSession
5757
from beets.library import Album, Item, Library
@@ -156,12 +156,27 @@ def check_reflink_support(path: str) -> bool:
156156
return reflink.supported_at(path)
157157

158158

159+
class ConfigMixin:
160+
@cached_property
161+
def config(self) -> beets.IncludeLazyConfig:
162+
"""Base beets configuration for tests."""
163+
config = beets.config
164+
config.sources = []
165+
config.read(user=False, defaults=True)
166+
167+
config["plugins"] = []
168+
config["verbose"] = 1
169+
config["ui"]["color"] = False
170+
config["threaded"] = False
171+
return config
172+
173+
159174
NEEDS_REFLINK = unittest.skipUnless(
160175
check_reflink_support(gettempdir()), "no reflink support for libdir"
161176
)
162177

163178

164-
class TestHelper(_common.Assertions):
179+
class TestHelper(_common.Assertions, ConfigMixin):
165180
"""Helper mixin for high-level cli and plugin tests.
166181
167182
This mixin provides methods to isolate beets' global state provide
@@ -187,8 +202,6 @@ def setup_beets(self):
187202
- ``libdir`` Path to a subfolder of ``temp_dir``, containing the
188203
library's media files. Same as ``config['directory']``.
189204
190-
- ``config`` The global configuration used by beets.
191-
192205
- ``lib`` Library instance created with the settings from
193206
``config``.
194207
@@ -205,15 +218,6 @@ def setup_beets(self):
205218
)
206219
self.env_patcher.start()
207220

208-
self.config = beets.config
209-
self.config.sources = []
210-
self.config.read(user=False, defaults=True)
211-
212-
self.config["plugins"] = []
213-
self.config["verbose"] = 1
214-
self.config["ui"]["color"] = False
215-
self.config["threaded"] = False
216-
217221
self.libdir = os.path.join(self.temp_dir, b"libdir")
218222
os.mkdir(syspath(self.libdir))
219223
self.config["directory"] = os.fsdecode(self.libdir)
@@ -232,8 +236,6 @@ def teardown_beets(self):
232236
self.io.restore()
233237
self.lib._close()
234238
self.remove_temp_dir()
235-
beets.config.clear()
236-
beets.config._materialized = False
237239

238240
# Library fixtures methods
239241

@@ -462,7 +464,7 @@ def setUp(self):
462464
self.i = _common.item(self.lib)
463465

464466

465-
class PluginMixin:
467+
class PluginMixin(ConfigMixin):
466468
plugin: ClassVar[str]
467469
preload_plugin: ClassVar[bool] = True
468470

@@ -483,7 +485,7 @@ def load_plugins(self, *plugins: str) -> None:
483485
"""
484486
# FIXME this should eventually be handled by a plugin manager
485487
plugins = (self.plugin,) if hasattr(self, "plugin") else plugins
486-
beets.config["plugins"] = plugins
488+
self.config["plugins"] = plugins
487489
beets.plugins.load_plugins(plugins)
488490
beets.plugins.find_plugins()
489491

@@ -504,7 +506,7 @@ def unload_plugins(self) -> None:
504506
# FIXME this should eventually be handled by a plugin manager
505507
for plugin_class in beets.plugins._instances:
506508
plugin_class.listeners = None
507-
beets.config["plugins"] = []
509+
self.config["plugins"] = []
508510
beets.plugins._classes = set()
509511
beets.plugins._instances = {}
510512
Item._types = getattr(Item, "_original_types", {})
@@ -515,10 +517,10 @@ def unload_plugins(self) -> None:
515517
@contextmanager
516518
def configure_plugin(self, config: list[Any] | dict[str, Any]):
517519
if isinstance(config, list):
518-
beets.config[self.plugin] = config
520+
self.config[self.plugin] = config
519521
else:
520522
for key, value in config.items():
521-
beets.config[self.plugin][key] = value
523+
self.config[self.plugin][key] = value
522524
self.load_plugins(self.plugin)
523525

524526
yield
@@ -638,7 +640,7 @@ def _get_import_session(self, import_dir: bytes) -> ImportSession:
638640
def setup_importer(
639641
self, import_dir: bytes | None = None, **kwargs
640642
) -> ImportSession:
641-
config["import"].set_args({**self.default_import_config, **kwargs})
643+
self.config["import"].set_args({**self.default_import_config, **kwargs})
642644
self.importer = self._get_import_session(import_dir or self.import_dir)
643645
return self.importer
644646

docs/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Other changes:
8888
calculate the bpm. Previously this import was being done immediately, so
8989
every ``beet`` invocation was being delayed by a couple of seconds.
9090
:bug:`5185`
91+
* :doc:`plugins/lyrics`: Rewrite lyrics tests using pytest to provide isolated
92+
configuration for each test case.
93+
:bug:`5133`
9194

9295
2.0.0 (May 30, 2024)
9396
--------------------

0 commit comments

Comments
 (0)