51
51
52
52
import beets
53
53
import beets .plugins
54
- from beets import autotag , config , importer , logging , util
54
+ from beets import autotag , importer , logging , util
55
55
from beets .autotag .hooks import AlbumInfo , TrackInfo
56
56
from beets .importer import ImportSession
57
57
from beets .library import Album , Item , Library
@@ -156,12 +156,27 @@ def check_reflink_support(path: str) -> bool:
156
156
return reflink .supported_at (path )
157
157
158
158
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
+
159
174
NEEDS_REFLINK = unittest .skipUnless (
160
175
check_reflink_support (gettempdir ()), "no reflink support for libdir"
161
176
)
162
177
163
178
164
- class TestHelper (_common .Assertions ):
179
+ class TestHelper (_common .Assertions , ConfigMixin ):
165
180
"""Helper mixin for high-level cli and plugin tests.
166
181
167
182
This mixin provides methods to isolate beets' global state provide
@@ -187,8 +202,6 @@ def setup_beets(self):
187
202
- ``libdir`` Path to a subfolder of ``temp_dir``, containing the
188
203
library's media files. Same as ``config['directory']``.
189
204
190
- - ``config`` The global configuration used by beets.
191
-
192
205
- ``lib`` Library instance created with the settings from
193
206
``config``.
194
207
@@ -205,15 +218,6 @@ def setup_beets(self):
205
218
)
206
219
self .env_patcher .start ()
207
220
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
-
217
221
self .libdir = os .path .join (self .temp_dir , b"libdir" )
218
222
os .mkdir (syspath (self .libdir ))
219
223
self .config ["directory" ] = os .fsdecode (self .libdir )
@@ -232,8 +236,6 @@ def teardown_beets(self):
232
236
self .io .restore ()
233
237
self .lib ._close ()
234
238
self .remove_temp_dir ()
235
- beets .config .clear ()
236
- beets .config ._materialized = False
237
239
238
240
# Library fixtures methods
239
241
@@ -462,7 +464,7 @@ def setUp(self):
462
464
self .i = _common .item (self .lib )
463
465
464
466
465
- class PluginMixin :
467
+ class PluginMixin ( ConfigMixin ) :
466
468
plugin : ClassVar [str ]
467
469
preload_plugin : ClassVar [bool ] = True
468
470
@@ -483,7 +485,7 @@ def load_plugins(self, *plugins: str) -> None:
483
485
"""
484
486
# FIXME this should eventually be handled by a plugin manager
485
487
plugins = (self .plugin ,) if hasattr (self , "plugin" ) else plugins
486
- beets .config ["plugins" ] = plugins
488
+ self .config ["plugins" ] = plugins
487
489
beets .plugins .load_plugins (plugins )
488
490
beets .plugins .find_plugins ()
489
491
@@ -504,7 +506,7 @@ def unload_plugins(self) -> None:
504
506
# FIXME this should eventually be handled by a plugin manager
505
507
for plugin_class in beets .plugins ._instances :
506
508
plugin_class .listeners = None
507
- beets .config ["plugins" ] = []
509
+ self .config ["plugins" ] = []
508
510
beets .plugins ._classes = set ()
509
511
beets .plugins ._instances = {}
510
512
Item ._types = getattr (Item , "_original_types" , {})
@@ -515,10 +517,10 @@ def unload_plugins(self) -> None:
515
517
@contextmanager
516
518
def configure_plugin (self , config : list [Any ] | dict [str , Any ]):
517
519
if isinstance (config , list ):
518
- beets .config [self .plugin ] = config
520
+ self .config [self .plugin ] = config
519
521
else :
520
522
for key , value in config .items ():
521
- beets .config [self .plugin ][key ] = value
523
+ self .config [self .plugin ][key ] = value
522
524
self .load_plugins (self .plugin )
523
525
524
526
yield
@@ -638,7 +640,7 @@ def _get_import_session(self, import_dir: bytes) -> ImportSession:
638
640
def setup_importer (
639
641
self , import_dir : bytes | None = None , ** kwargs
640
642
) -> ImportSession :
641
- config ["import" ].set_args ({** self .default_import_config , ** kwargs })
643
+ self . config ["import" ].set_args ({** self .default_import_config , ** kwargs })
642
644
self .importer = self ._get_import_session (import_dir or self .import_dir )
643
645
return self .importer
644
646
0 commit comments