Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralize test setup #5362

Merged
merged 29 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
74fee8d
Remove unused classes and constant
snejus Jul 2, 2024
2566e22
Incorporate _common.Assertions into helper.TestHelper
snejus Jul 3, 2024
3e27815
Dedupe TestHelper and _common.TestCase setup
snejus Jul 13, 2024
6c1e26a
Replace unittest.TestCase, TestHelper by BeetsTestCase
snejus Jul 7, 2024
0b5b944
Replace unittest.TestCase, ImportHelper by ImportTestCase
snejus Jul 3, 2024
8373181
Create PluginImportTestCase in test_plugins
snejus Jul 3, 2024
6ccf33d
test_replaygain: restructure tests to share setup
snejus Jul 3, 2024
91099d3
Deduplicate TerminalImportHelper and rename to TerminalImportMixin
snejus Jun 29, 2024
b64eaed
Rename LibTestCase to ItemInDBTestCase
snejus Jul 13, 2024
2d5fd90
Remove redundant setup_beets and teardown_beets instructions
snejus Jul 4, 2024
7d7f163
Remove redundant library setUp instructions
snejus Jul 5, 2024
16cf8dd
Centralize db setup on disk
snejus Jul 6, 2024
432da56
Create PluginTestCase to dedupe plugin setup
snejus Jul 6, 2024
fcff5d7
Remove def suite TestLoader definitions
snejus Jul 7, 2024
1f8466f
Move create_importer to ImportHelper
snejus Jul 8, 2024
41bbb77
Centralise 'import_dir' creation
snejus Jul 8, 2024
c2fdf98
Synchronise ImportHelper._create_import_dir and TestHelper.create_imp…
snejus Jul 8, 2024
7e444db
Rename _create_import_dir -> prepare_album_for_import
snejus Jul 9, 2024
8065ff0
Split album and singleton tests in edit and filefilter
snejus Jul 14, 2024
f042f5a
Leave a single source of truth for importer setup
snejus Jul 12, 2024
8d85cfd
Define AsIsImporterMixin to run autotag=False importer
snejus Jul 12, 2024
92f79db
Fix ConfigTest failures locally
snejus Jul 13, 2024
139b54e
Add setup_singleton_importer for singleton importer
snejus Jul 14, 2024
01c24bb
Return paths from import files prep
snejus Jul 14, 2024
6dda984
Standardize ImportPretendTest import files prep
snejus Jul 14, 2024
e097f1a
Rewrite FileFilterPluginTest
snejus Jul 15, 2024
9c75513
test_ui: Fix Config/Plugin/Completion tests interdependency
snejus Jul 17, 2024
199f307
Use PluginMixin in tests that load plugins manually
snejus Jul 17, 2024
5f395ab
Configure plugins using PluginMixin.configure_plugin
snejus Jul 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 6 additions & 125 deletions beets/test/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@
"""Some common functionality for beets' test cases."""

import os
import shutil
import sys
import tempfile
import time
import unittest
from contextlib import contextmanager

import beets # noqa: E402
import beets.library # noqa: E402
import beets
import beets.library

# Make sure the development versions of the plugins are used
import beetsplug # noqa: E402
from beets import util # noqa: E402
from beets import importer, logging # noqa: E402
from beets.ui import commands # noqa: E402
from beets.util import bytestring_path, syspath # noqa: E402
import beetsplug
from beets import importer, logging, util
from beets.ui import commands
from beets.util import syspath

beetsplug.__path__ = [
os.path.abspath(
Expand Down Expand Up @@ -121,9 +118,6 @@ def item(lib=None):
return i


_album_ident = 0


def album(lib=None):
global _item_ident
_item_ident += 1
Expand Down Expand Up @@ -190,100 +184,6 @@ def assert_equal_path(self, a, b):
)


# A test harness for all beets tests.
# Provides temporary, isolated configuration.
class TestCase(unittest.TestCase, Assertions):
"""A unittest.TestCase subclass that saves and restores beets'
global configuration. This allows tests to make temporary
modifications that will then be automatically removed when the test
completes. Also provides some additional assertion methods, a
temporary directory, and a DummyIO.
"""

def setUp(self):
# A "clean" source list including only the defaults.
beets.config.sources = []
beets.config.read(user=False, defaults=True)

# Direct paths to a temporary directory. Tests can also use this
# temporary directory.
self.temp_dir = util.bytestring_path(tempfile.mkdtemp())

beets.config["statefile"] = os.fsdecode(
os.path.join(self.temp_dir, b"state.pickle")
)
beets.config["library"] = os.fsdecode(
os.path.join(self.temp_dir, b"library.db")
)
beets.config["directory"] = os.fsdecode(
os.path.join(self.temp_dir, b"libdir")
)

# Set $HOME, which is used by Confuse to create directories.
self._old_home = os.environ.get("HOME")
os.environ["HOME"] = os.fsdecode(self.temp_dir)

# Initialize, but don't install, a DummyIO.
self.io = DummyIO()

def tearDown(self):
if os.path.isdir(syspath(self.temp_dir)):
shutil.rmtree(syspath(self.temp_dir))
if self._old_home is None:
del os.environ["HOME"]
else:
os.environ["HOME"] = self._old_home
self.io.restore()

beets.config.clear()
beets.config._materialized = False


class LibTestCase(TestCase):
"""A test case that includes an in-memory library object (`lib`) and
an item added to the library (`i`).
"""

def setUp(self):
super().setUp()
self.lib = beets.library.Library(":memory:")
self.i = item(self.lib)

def tearDown(self):
self.lib._connection().close()
super().tearDown()


# Mock timing.


class Timecop:
"""Mocks the timing system (namely time() and sleep()) for testing.
Inspired by the Ruby timecop library.
"""

def __init__(self):
self.now = time.time()

def time(self):
return self.now

def sleep(self, amount):
self.now += amount

def install(self):
self.orig = {
"time": time.time,
"sleep": time.sleep,
}
time.time = self.time
time.sleep = self.sleep

def restore(self):
time.time = self.orig["time"]
time.sleep = self.orig["sleep"]


# Mock I/O.


Expand Down Expand Up @@ -388,25 +288,6 @@ def __getattr__(self, key):
return self.fields.get(key)


# Convenience methods for setting up a temporary sandbox directory for tests
# that need to interact with the filesystem.


class TempDirMixin:
"""Text mixin for creating and deleting a temporary directory."""

def create_temp_dir(self):
"""Create a temporary directory and assign it into `self.temp_dir`.
Call `remove_temp_dir` later to delete it.
"""
self.temp_dir = bytestring_path(tempfile.mkdtemp())

def remove_temp_dir(self):
"""Delete the temporary directory created by `create_temp_dir`."""
if os.path.isdir(syspath(self.temp_dir)):
shutil.rmtree(syspath(self.temp_dir))


# Platform mocking.


Expand Down
Loading
Loading