Rip away io stuff from TestHelper to IOMixin

This commit is contained in:
Šarūnas Nejus 2024-09-16 16:17:42 +01:00
parent a815305fcd
commit 605cea9bdc
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
6 changed files with 35 additions and 50 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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()

View file

@ -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:

View file

@ -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)