diff --git a/beets/test/helper.py b/beets/test/helper.py index c64998174..5409b3425 100644 --- a/beets/test/helper.py +++ b/beets/test/helper.py @@ -253,59 +253,6 @@ class TestHelper(_common.Assertions): Item._queries = Item._original_queries Album._queries = Album._original_queries - def create_importer(self, item_count=1, album_count=1): - """Create files to import and return corresponding session. - - Copies the specified number of files to a subdirectory of - `self.temp_dir` and creates a `ImportSessionFixture` for this path. - """ - import_dir = os.path.join(self.temp_dir, b"import") - if not os.path.isdir(syspath(import_dir)): - os.mkdir(syspath(import_dir)) - - album_no = 0 - while album_count: - album = util.bytestring_path(f"album {album_no}") - album_dir = os.path.join(import_dir, album) - if os.path.exists(syspath(album_dir)): - album_no += 1 - continue - os.mkdir(syspath(album_dir)) - album_count -= 1 - - track_no = 0 - album_item_count = item_count - while album_item_count: - title = f"track {track_no}" - src = os.path.join(_common.RSRC, b"full.mp3") - title_file = util.bytestring_path(f"{title}.mp3") - dest = os.path.join(album_dir, title_file) - if os.path.exists(syspath(dest)): - track_no += 1 - continue - album_item_count -= 1 - shutil.copy(syspath(src), syspath(dest)) - mediafile = MediaFile(dest) - mediafile.update( - { - "artist": "artist", - "albumartist": "album artist", - "title": title, - "album": album, - "mb_albumid": None, - "mb_trackid": None, - } - ) - mediafile.save() - - config["import"]["quiet"] = True - config["import"]["autotag"] = False - config["import"]["resume"] = False - - return ImportSessionFixture( - self.lib, loghandler=None, query=None, paths=[import_dir] - ) - # Library fixtures methods def create_item(self, **values): @@ -642,6 +589,59 @@ class ImportHelper: self._get_import_session(import_dir or self.import_dir) + def create_importer(self, item_count=1, album_count=1): + """Create files to import and return corresponding session. + + Copies the specified number of files to a subdirectory of + `self.temp_dir` and creates a `ImportSessionFixture` for this path. + """ + import_dir = os.path.join(self.temp_dir, b"import") + if not os.path.isdir(syspath(import_dir)): + os.mkdir(syspath(import_dir)) + + album_no = 0 + while album_count: + album = util.bytestring_path(f"album {album_no}") + album_dir = os.path.join(import_dir, album) + if os.path.exists(syspath(album_dir)): + album_no += 1 + continue + os.mkdir(syspath(album_dir)) + album_count -= 1 + + track_no = 0 + album_item_count = item_count + while album_item_count: + title = f"track {track_no}" + src = os.path.join(_common.RSRC, b"full.mp3") + title_file = util.bytestring_path(f"{title}.mp3") + dest = os.path.join(album_dir, title_file) + if os.path.exists(syspath(dest)): + track_no += 1 + continue + album_item_count -= 1 + shutil.copy(syspath(src), syspath(dest)) + mediafile = MediaFile(dest) + mediafile.update( + { + "artist": "artist", + "albumartist": "album artist", + "title": title, + "album": album, + "mb_albumid": None, + "mb_trackid": None, + } + ) + mediafile.save() + + config["import"]["quiet"] = True + config["import"]["autotag"] = False + config["import"]["resume"] = False + + return ImportSessionFixture( + self.lib, loghandler=None, query=None, paths=[import_dir] + ) + def assert_file_in_lib(self, *segments): """Join the ``segments`` and assert that this path exists in the library directory. diff --git a/test/plugins/test_convert.py b/test/plugins/test_convert.py index b82592d64..67b513bb9 100644 --- a/test/plugins/test_convert.py +++ b/test/plugins/test_convert.py @@ -23,7 +23,12 @@ from mediafile import MediaFile from beets import util from beets.test import _common -from beets.test.helper import PluginTestCase, capture_log, control_stdin +from beets.test.helper import ( + ImportHelper, + PluginTestCase, + capture_log, + control_stdin, +) from beets.util import bytestring_path, displayable_path @@ -90,7 +95,7 @@ class ConvertTestCase(ConvertMixin, PluginTestCase): @_common.slow_test() -class ImportConvertTest(ConvertTestCase): +class ImportConvertTest(ImportHelper, ConvertTestCase): def setUp(self): super().setUp() self.importer = self.create_importer() diff --git a/test/plugins/test_keyfinder.py b/test/plugins/test_keyfinder.py index fbf0ae0f6..d941df3ca 100644 --- a/test/plugins/test_keyfinder.py +++ b/test/plugins/test_keyfinder.py @@ -17,11 +17,11 @@ from unittest.mock import patch from beets import util from beets.library import Item -from beets.test.helper import PluginTestCase +from beets.test.helper import ImportTestCase, PluginMixin @patch("beets.util.command_output") -class KeyFinderTest(PluginTestCase): +class KeyFinderTest(PluginMixin, ImportTestCase): plugin = "keyfinder" def test_add_key(self, command_output): diff --git a/test/plugins/test_permissions.py b/test/plugins/test_permissions.py index e5db878d0..a83fbfb26 100644 --- a/test/plugins/test_permissions.py +++ b/test/plugins/test_permissions.py @@ -6,7 +6,7 @@ import platform from unittest.mock import Mock, patch from beets.test._common import touch -from beets.test.helper import PluginTestCase +from beets.test.helper import ImportTestCase, PluginMixin from beets.util import displayable_path from beetsplug.permissions import ( check_permissions, @@ -15,7 +15,7 @@ from beetsplug.permissions import ( ) -class PermissionsPluginTest(PluginTestCase): +class PermissionsPluginTest(PluginMixin, ImportTestCase): plugin = "permissions" def setUp(self): diff --git a/test/plugins/test_replaygain.py b/test/plugins/test_replaygain.py index 65b27d1bc..342902a63 100644 --- a/test/plugins/test_replaygain.py +++ b/test/plugins/test_replaygain.py @@ -19,7 +19,7 @@ from typing import ClassVar from mediafile import MediaFile from beets import config -from beets.test.helper import BeetsTestCase, has_program +from beets.test.helper import ImportTestCase, has_program from beetsplug.replaygain import ( FatalGstreamerPluginReplayGainError, GStreamerBackend, @@ -52,7 +52,7 @@ def reset_replaygain(item): item.store() -class ReplayGainTestCase(BeetsTestCase): +class ReplayGainTestCase(ImportTestCase): db_on_disk = True backend: ClassVar[str] diff --git a/test/test_importer.py b/test/test_importer.py index 4d150acc3..62daa8a10 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -1159,7 +1159,7 @@ def match_album_mock(*args, **kwargs): @patch("beets.autotag.mb.match_album", Mock(side_effect=match_album_mock)) -class ImportDuplicateAlbumTest(BeetsTestCase): +class ImportDuplicateAlbumTest(ImportTestCase): def setUp(self): super().setUp() @@ -1278,7 +1278,7 @@ def match_track_mock(*args, **kwargs): @patch("beets.autotag.mb.match_track", Mock(side_effect=match_track_mock)) -class ImportDuplicateSingletonTest(BeetsTestCase): +class ImportDuplicateSingletonTest(ImportTestCase): def setUp(self): super().setUp() @@ -1364,7 +1364,7 @@ class TagLogTest(BeetsTestCase): self.assertIn("status caf\xe9", sio.getvalue()) -class ResumeImportTest(BeetsTestCase): +class ResumeImportTest(ImportTestCase): @patch("beets.plugins.send") def test_resume_album(self, plugins_send): self.importer = self.create_importer(album_count=2) @@ -1409,7 +1409,7 @@ class ResumeImportTest(BeetsTestCase): self.assertIsNotNone(self.lib.items("title:track 1").get()) -class IncrementalImportTest(BeetsTestCase): +class IncrementalImportTest(ImportTestCase): def setUp(self): super().setUp() self.config["import"]["incremental"] = True diff --git a/test/test_logging.py b/test/test_logging.py index fbc6a5f33..2919e6030 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -9,7 +9,7 @@ import beets.logging as blog import beetsplug from beets import plugins, ui from beets.test import _common, helper -from beets.test.helper import BeetsTestCase, PluginTestCase +from beets.test.helper import BeetsTestCase, ImportTestCase, PluginMixin class LoggingTest(BeetsTestCase): @@ -46,7 +46,7 @@ class LoggingTest(BeetsTestCase): self.assertTrue(stream.getvalue(), "foo oof baz") -class LoggingLevelTest(PluginTestCase): +class LoggingLevelTest(PluginMixin, ImportTestCase): plugin = "dummy" class DummyModule: @@ -161,7 +161,7 @@ class LoggingLevelTest(PluginTestCase): @_common.slow_test() -class ConcurrentEventsTest(BeetsTestCase): +class ConcurrentEventsTest(ImportTestCase): """Similar to LoggingLevelTest but lower-level and focused on multiple events interaction. Since this is a bit heavy we don't do it in LoggingLevelTest.