@@ -163,15 +163,49 @@ def config(self) -> beets.IncludeLazyConfig:
163
163
)
164
164
165
165
166
- class TestHelper (_common .Assertions , ConfigMixin ):
166
+ class IOMixin :
167
+ @cached_property
168
+ def io (self ) -> _common .DummyIO :
169
+ return _common .DummyIO ()
170
+
171
+ def setUp (self ):
172
+ super ().setUp ()
173
+ self .io .install ()
174
+
175
+ def tearDown (self ):
176
+ super ().tearDown ()
177
+ self .io .restore ()
178
+
179
+
180
+ class TestHelper (ConfigMixin ):
167
181
"""Helper mixin for high-level cli and plugin tests.
168
182
169
183
This mixin provides methods to isolate beets' global state provide
170
184
fixtures.
171
185
"""
172
186
187
+ resource_path = Path (os .fsdecode (_common .RSRC )) / "full.mp3"
188
+
173
189
db_on_disk : ClassVar [bool ] = False
174
190
191
+ @cached_property
192
+ def temp_dir_path (self ) -> Path :
193
+ return Path (self .create_temp_dir ())
194
+
195
+ @cached_property
196
+ def temp_dir (self ) -> bytes :
197
+ return util .bytestring_path (self .temp_dir_path )
198
+
199
+ @cached_property
200
+ def lib_path (self ) -> Path :
201
+ lib_path = self .temp_dir_path / "libdir"
202
+ lib_path .mkdir (exist_ok = True )
203
+ return lib_path
204
+
205
+ @cached_property
206
+ def libdir (self ) -> bytes :
207
+ return bytestring_path (self .lib_path )
208
+
175
209
# TODO automate teardown through hook registration
176
210
177
211
def setup_beets (self ):
@@ -194,8 +228,7 @@ def setup_beets(self):
194
228
195
229
Make sure you call ``teardown_beets()`` afterwards.
196
230
"""
197
- self .create_temp_dir ()
198
- temp_dir_str = os .fsdecode (self .temp_dir )
231
+ temp_dir_str = str (self .temp_dir_path )
199
232
self .env_patcher = patch .dict (
200
233
"os.environ" ,
201
234
{
@@ -205,22 +238,16 @@ def setup_beets(self):
205
238
)
206
239
self .env_patcher .start ()
207
240
208
- self .libdir = os .path .join (self .temp_dir , b"libdir" )
209
- os .mkdir (syspath (self .libdir ))
210
- self .config ["directory" ] = os .fsdecode (self .libdir )
241
+ self .config ["directory" ] = str (self .lib_path )
211
242
212
243
if self .db_on_disk :
213
244
dbpath = util .bytestring_path (self .config ["library" ].as_filename ())
214
245
else :
215
246
dbpath = ":memory:"
216
247
self .lib = Library (dbpath , self .libdir )
217
248
218
- # Initialize, but don't install, a DummyIO.
219
- self .io = _common .DummyIO ()
220
-
221
249
def teardown_beets (self ):
222
250
self .env_patcher .stop ()
223
- self .io .restore ()
224
251
self .lib ._close ()
225
252
self .remove_temp_dir ()
226
253
@@ -384,16 +411,12 @@ def run_with_output(self, *args):
384
411
385
412
# Safe file operations
386
413
387
- def create_temp_dir (self , ** kwargs ):
388
- """Create a temporary directory and assign it into
389
- `self.temp_dir`. Call `remove_temp_dir` later to delete it.
390
- """
391
- temp_dir = mkdtemp (** kwargs )
392
- self .temp_dir = util .bytestring_path (temp_dir )
414
+ def create_temp_dir (self , ** kwargs ) -> str :
415
+ return mkdtemp (** kwargs )
393
416
394
417
def remove_temp_dir (self ):
395
418
"""Delete the temporary directory created by `create_temp_dir`."""
396
- shutil .rmtree (syspath ( self .temp_dir ) )
419
+ shutil .rmtree (self .temp_dir_path )
397
420
398
421
def touch (self , path , dir = None , content = "" ):
399
422
"""Create a file at `path` with given content.
@@ -514,7 +537,6 @@ class ImportHelper(TestHelper):
514
537
autotagging library and several assertions for the library.
515
538
"""
516
539
517
- resource_path = syspath (os .path .join (_common .RSRC , b"full.mp3" ))
518
540
default_import_config = {
519
541
"autotag" : True ,
520
542
"copy" : True ,
@@ -531,7 +553,7 @@ class ImportHelper(TestHelper):
531
553
532
554
@cached_property
533
555
def import_path (self ) -> Path :
534
- import_path = Path ( os . fsdecode ( self .temp_dir )) / "import"
556
+ import_path = self .temp_dir_path / "import"
535
557
import_path .mkdir (exist_ok = True )
536
558
return import_path
537
559
@@ -599,7 +621,7 @@ def prepare_album_for_import(
599
621
]
600
622
601
623
def prepare_albums_for_import (self , count : int = 1 ) -> None :
602
- album_dirs = Path ( os . fsdecode ( self .import_dir )) .glob ("album_*" )
624
+ album_dirs = self .import_path .glob ("album_*" )
603
625
base_idx = int (str (max (album_dirs , default = "0" )).split ("_" )[- 1 ]) + 1
604
626
605
627
for album_id in range (base_idx , count + base_idx ):
@@ -623,21 +645,6 @@ def setup_importer(
623
645
def setup_singleton_importer (self , ** kwargs ) -> ImportSession :
624
646
return self .setup_importer (singletons = True , ** kwargs )
625
647
626
- def assert_file_in_lib (self , * segments ):
627
- """Join the ``segments`` and assert that this path exists in the
628
- library directory.
629
- """
630
- self .assertExists (os .path .join (self .libdir , * segments ))
631
-
632
- def assert_file_not_in_lib (self , * segments ):
633
- """Join the ``segments`` and assert that this path does not
634
- exist in the library directory.
635
- """
636
- self .assertNotExists (os .path .join (self .libdir , * segments ))
637
-
638
- def assert_lib_dir_empty (self ):
639
- assert not os .listdir (syspath (self .libdir ))
640
-
641
648
642
649
class AsIsImporterMixin :
643
650
def setUp (self ):
@@ -759,7 +766,7 @@ def _add_choice_input(self):
759
766
self ._add_choice_input ()
760
767
761
768
762
- class TerminalImportMixin (ImportHelper ):
769
+ class TerminalImportMixin (IOMixin , ImportHelper ):
763
770
"""Provides_a terminal importer for the import session."""
764
771
765
772
io : _common .DummyIO
0 commit comments