From 605cea9bdc2b030f1ee00d0854d2a37c9c722759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Mon, 16 Sep 2024 16:17:42 +0100 Subject: [PATCH] Rip away io stuff from TestHelper to IOMixin --- beets/test/helper.py | 20 +++++++++++++++----- test/plugins/test_embedart.py | 13 +++++++------ test/test_importer.py | 5 ++--- test/test_ui.py | 26 +++++++------------------- test/test_ui_commands.py | 13 ++----------- test/test_ui_init.py | 8 ++------ 6 files changed, 35 insertions(+), 50 deletions(-) diff --git a/beets/test/helper.py b/beets/test/helper.py index b86db5b23..7ce3d06d7 100644 --- a/beets/test/helper.py +++ b/beets/test/helper.py @@ -163,6 +163,20 @@ NEEDS_REFLINK = unittest.skipUnless( ) +class IOMixin: + @cached_property + def io(self) -> _common.DummyIO: + return _common.DummyIO() + + def setUp(self): + super().setUp() + self.io.install() + + def tearDown(self): + super().tearDown() + self.io.restore() + + class TestHelper(_common.Assertions, ConfigMixin): """Helper mixin for high-level cli and plugin tests. @@ -215,12 +229,8 @@ class TestHelper(_common.Assertions, ConfigMixin): dbpath = ":memory:" self.lib = Library(dbpath, self.libdir) - # Initialize, but don't install, a DummyIO. - self.io = _common.DummyIO() - def teardown_beets(self): self.env_patcher.stop() - self.io.restore() self.lib._close() self.remove_temp_dir() @@ -759,7 +769,7 @@ class TerminalImportSessionFixture(TerminalImportSession): self._add_choice_input() -class TerminalImportMixin(ImportHelper): +class TerminalImportMixin(IOMixin, ImportHelper): """Provides_a terminal importer for the import session.""" io: _common.DummyIO diff --git a/test/plugins/test_embedart.py b/test/plugins/test_embedart.py index f2f02137b..2cada1d5b 100644 --- a/test/plugins/test_embedart.py +++ b/test/plugins/test_embedart.py @@ -24,7 +24,12 @@ from mediafile import MediaFile from beets import art, config, logging, ui from beets.test import _common -from beets.test.helper import BeetsTestCase, FetchImageHelper, PluginMixin +from beets.test.helper import ( + BeetsTestCase, + FetchImageHelper, + IOMixin, + PluginMixin, +) from beets.util import bytestring_path, displayable_path, syspath from beets.util.artresizer import ArtResizer from test.test_art_resize import DummyIMBackend @@ -68,17 +73,13 @@ def require_artresizer_compare(test): return wrapper -class EmbedartCliTest(PluginMixin, FetchImageHelper, BeetsTestCase): +class EmbedartCliTest(IOMixin, PluginMixin, FetchImageHelper, BeetsTestCase): plugin = "embedart" small_artpath = os.path.join(_common.RSRC, b"image-2x3.jpg") abbey_artpath = os.path.join(_common.RSRC, b"abbey.jpg") abbey_similarpath = os.path.join(_common.RSRC, b"abbey-similar.jpg") abbey_differentpath = os.path.join(_common.RSRC, b"abbey-different.jpg") - def setUp(self): - super().setUp() # Converter is threaded - self.io.install() - def _setup_data(self, artpath=None): if not artpath: artpath = self.small_artpath diff --git a/test/test_importer.py b/test/test_importer.py index 9bb0e8a63..fc4141c5b 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -43,6 +43,7 @@ from beets.test.helper import ( AutotagStub, BeetsTestCase, ImportTestCase, + IOMixin, PluginMixin, capture_log, has_program, @@ -1588,13 +1589,11 @@ class ReimportTest(AutotagImportTestCase): assert self._album().data_source == "match_source" -class ImportPretendTest(AutotagImportTestCase): +class ImportPretendTest(IOMixin, AutotagImportTestCase): """Test the pretend commandline option""" def setUp(self): super().setUp() - self.io.install() - self.album_track_path = self.prepare_album_for_import(1)[0] self.single_path = self.prepare_track_for_import(2, self.import_path) self.album_path = self.album_track_path.parent diff --git a/test/test_ui.py b/test/test_ui.py index 8bb0218d5..519962b41 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -32,6 +32,7 @@ from beets.autotag.match import distance from beets.test import _common from beets.test.helper import ( BeetsTestCase, + IOMixin, PluginTestCase, capture_stdout, control_stdin, @@ -107,12 +108,10 @@ class ListTest(BeetsTestCase): assert "the album" not in stdout.getvalue() -class RemoveTest(BeetsTestCase): +class RemoveTest(IOMixin, BeetsTestCase): def setUp(self): super().setUp() - self.io.install() - # Copy a file into the library. self.item_path = os.path.join(_common.RSRC, b"full.mp3") self.i = library.Item.from_path(self.item_path) @@ -444,8 +443,6 @@ class MoveTest(BeetsTestCase): def setUp(self): super().setUp() - self.io.install() - self.itempath = os.path.join(self.libdir, b"srcfile") shutil.copy( syspath(os.path.join(_common.RSRC, b"full.mp3")), @@ -544,12 +541,10 @@ class MoveTest(BeetsTestCase): self.assertNotExists(self.otherdir) -class UpdateTest(BeetsTestCase): +class UpdateTest(IOMixin, BeetsTestCase): def setUp(self): super().setUp() - self.io.install() - # Copy a file into the library. item_path = os.path.join(_common.RSRC, b"full.mp3") item_path_two = os.path.join(_common.RSRC, b"full.flac") @@ -742,11 +737,7 @@ class UpdateTest(BeetsTestCase): assert item.lyrics != "new lyrics" -class PrintTest(BeetsTestCase): - def setUp(self): - super().setUp() - self.io.install() - +class PrintTest(IOMixin, BeetsTestCase): def test_print_without_locale(self): lang = os.environ.get("LANG") if lang: @@ -1120,10 +1111,9 @@ class ConfigTest(TestPluginTestCase): ) -class ShowModelChangeTest(BeetsTestCase): +class ShowModelChangeTest(IOMixin, BeetsTestCase): def setUp(self): super().setUp() - self.io.install() self.a = _common.item() self.b = _common.item() self.a.path = self.b.path @@ -1172,10 +1162,9 @@ class ShowModelChangeTest(BeetsTestCase): assert "bar" in out -class ShowChangeTest(BeetsTestCase): +class ShowChangeTest(IOMixin, BeetsTestCase): def setUp(self): super().setUp() - self.io.install() self.items = [_common.item()] self.items[0].track = 1 @@ -1397,7 +1386,7 @@ class PluginTest(TestPluginTestCase): os.environ.get("GITHUB_ACTIONS") == "true" and sys.platform == "linux", reason="Completion is for some reason unhappy on Ubuntu 24.04 in CI", ) -class CompletionTest(TestPluginTestCase): +class CompletionTest(IOMixin, TestPluginTestCase): def test_completion(self): # Do not load any other bash completion scripts on the system. env = dict(os.environ) @@ -1427,7 +1416,6 @@ class CompletionTest(TestPluginTestCase): self.skipTest("could not read bash-completion script") # Load completion script. - self.io.install() self.run_command("completion", lib=None) completion_script = self.io.getoutput().encode("utf-8") self.io.restore() diff --git a/test/test_ui_commands.py b/test/test_ui_commands.py index 897cba8a1..412ddc2b7 100644 --- a/test/test_ui_commands.py +++ b/test/test_ui_commands.py @@ -21,7 +21,7 @@ import pytest from beets import library, ui from beets.test import _common -from beets.test.helper import BeetsTestCase, ItemInDBTestCase +from beets.test.helper import BeetsTestCase, IOMixin, ItemInDBTestCase from beets.ui import commands from beets.util import syspath @@ -75,16 +75,7 @@ class QueryTest(BeetsTestCase): self.check_do_query(0, 2, album=True, also_items=False) -class FieldsTest(ItemInDBTestCase): - def setUp(self): - super().setUp() - - self.io.install() - - def tearDown(self): - super().tearDown() - self.io.restore() - +class FieldsTest(IOMixin, ItemInDBTestCase): def remove_keys(self, keys, text): for i in text: try: diff --git a/test/test_ui_init.py b/test/test_ui_init.py index df21b300c..0f42d7258 100644 --- a/test/test_ui_init.py +++ b/test/test_ui_init.py @@ -21,14 +21,10 @@ from random import random from beets import config, ui from beets.test import _common -from beets.test.helper import BeetsTestCase, control_stdin +from beets.test.helper import BeetsTestCase, IOMixin, control_stdin -class InputMethodsTest(BeetsTestCase): - def setUp(self): - super().setUp() - self.io.install() - +class InputMethodsTest(IOMixin, BeetsTestCase): def _print_helper(self, s): print(s)