Replace assert_equal_path

This commit is contained in:
Šarūnas Nejus 2025-05-31 23:20:28 +01:00
parent ca3adfe525
commit 0dd6cb3b6d
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
4 changed files with 35 additions and 85 deletions

View file

@ -111,16 +111,6 @@ def import_session(lib=None, loghandler=None, paths=[], query=[], cli=False):
return cls(lib, loghandler, paths, query) return cls(lib, loghandler, paths, query)
class Assertions:
"""A mixin with additional unit test assertions."""
def assert_equal_path(self, a, b):
"""Check that two paths are equal."""
a_bytes, b_bytes = util.normpath(a), util.normpath(b)
assert a_bytes == b_bytes, f"{a_bytes=} != {b_bytes=}"
# Mock I/O. # Mock I/O.

View file

@ -177,7 +177,7 @@ class IOMixin:
self.io.restore() self.io.restore()
class TestHelper(_common.Assertions, ConfigMixin): class TestHelper(ConfigMixin):
"""Helper mixin for high-level cli and plugin tests. """Helper mixin for high-level cli and plugin tests.
This mixin provides methods to isolate beets' global state provide This mixin provides methods to isolate beets' global state provide

View file

@ -131,9 +131,7 @@ class NonAutotaggedImportTest(PathsMixin, AsIsImporterMixin, ImportTestCase):
assert self.track_lib_path.exists() assert self.track_lib_path.exists()
assert self.track_lib_path.is_symlink() assert self.track_lib_path.is_symlink()
self.assert_equal_path( assert self.track_lib_path.resolve() == self.track_import_path
self.track_lib_path.resolve(), self.track_import_path
)
@unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks") @unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks")
def test_import_hardlink_arrives(self): def test_import_hardlink_arrives(self):
@ -711,9 +709,7 @@ class ImportExistingTest(PathsMixin, AutotagImportTestCase):
def test_outside_file_is_copied(self): def test_outside_file_is_copied(self):
config["import"]["copy"] = False config["import"]["copy"] = False
self.importer.run() self.importer.run()
self.assert_equal_path( assert self.lib.items().get().filepath == self.track_import_path
self.lib.items().get().path, self.import_media[0].path
)
self.reimporter = self.setup_importer() self.reimporter = self.setup_importer()
self.reimporter.add_choice(importer.Action.APPLY) self.reimporter.add_choice(importer.Action.APPLY)

View file

@ -21,6 +21,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import unittest import unittest
from pathlib import Path
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
import pytest import pytest
@ -828,9 +829,7 @@ class ConfigTest(TestPluginTestCase):
del os.environ["BEETSDIR"] del os.environ["BEETSDIR"]
# Also set APPDATA, the Windows equivalent of setting $HOME. # Also set APPDATA, the Windows equivalent of setting $HOME.
appdata_dir = os.fsdecode( appdata_dir = self.temp_dir_path / "AppData" / "Roaming"
os.path.join(self.temp_dir, b"AppData", b"Roaming")
)
self._orig_cwd = os.getcwd() self._orig_cwd = os.getcwd()
self.test_cmd = self._make_test_cmd() self.test_cmd = self._make_test_cmd()
@ -838,27 +837,21 @@ class ConfigTest(TestPluginTestCase):
# Default user configuration # Default user configuration
if platform.system() == "Windows": if platform.system() == "Windows":
self.user_config_dir = os.fsencode( self.user_config_dir = appdata_dir / "beets"
os.path.join(appdata_dir, "beets")
)
else: else:
self.user_config_dir = os.path.join( self.user_config_dir = self.temp_dir_path / ".config" / "beets"
self.temp_dir, b".config", b"beets" self.user_config_dir.mkdir(parents=True, exist_ok=True)
) self.user_config_path = self.user_config_dir / "config.yaml"
os.makedirs(syspath(self.user_config_dir))
self.user_config_path = os.path.join(
self.user_config_dir, b"config.yaml"
)
# Custom BEETSDIR # Custom BEETSDIR
self.beetsdir = os.path.join(self.temp_dir, b"beetsdir") self.beetsdir = self.temp_dir_path / "beetsdir"
self.cli_config_path = os.path.join( self.beetsdir.mkdir(parents=True, exist_ok=True)
os.fsdecode(self.temp_dir), "config.yaml"
) self.env_config_path = str(self.beetsdir / "config.yaml")
os.makedirs(syspath(self.beetsdir)) self.cli_config_path = str(self.temp_dir_path / "config.yaml")
self.env_patcher = patch( self.env_patcher = patch(
"os.environ", "os.environ",
{"HOME": os.fsdecode(self.temp_dir), "APPDATA": appdata_dir}, {"HOME": str(self.temp_dir_path), "APPDATA": str(appdata_dir)},
) )
self.env_patcher.start() self.env_patcher.start()
@ -957,9 +950,8 @@ class ConfigTest(TestPluginTestCase):
assert config["anoption"].get() == "cli overwrite" assert config["anoption"].get() == "cli overwrite"
def test_cli_config_file_overwrites_beetsdir_defaults(self): def test_cli_config_file_overwrites_beetsdir_defaults(self):
os.environ["BEETSDIR"] = os.fsdecode(self.beetsdir) os.environ["BEETSDIR"] = str(self.beetsdir)
env_config_path = os.path.join(self.beetsdir, b"config.yaml") with open(self.env_config_path, "w") as file:
with open(env_config_path, "w") as file:
file.write("anoption: value") file.write("anoption: value")
with open(self.cli_config_path, "w") as file: with open(self.cli_config_path, "w") as file:
@ -1006,39 +998,25 @@ class ConfigTest(TestPluginTestCase):
file.write("statefile: state") file.write("statefile: state")
self.run_command("--config", self.cli_config_path, "test", lib=None) self.run_command("--config", self.cli_config_path, "test", lib=None)
self.assert_equal_path( assert config["library"].as_path() == self.user_config_dir / "beets.db"
util.bytestring_path(config["library"].as_filename()), assert config["statefile"].as_path() == self.user_config_dir / "state"
os.path.join(self.user_config_dir, b"beets.db"),
)
self.assert_equal_path(
util.bytestring_path(config["statefile"].as_filename()),
os.path.join(self.user_config_dir, b"state"),
)
def test_cli_config_paths_resolve_relative_to_beetsdir(self): def test_cli_config_paths_resolve_relative_to_beetsdir(self):
os.environ["BEETSDIR"] = os.fsdecode(self.beetsdir) os.environ["BEETSDIR"] = str(self.beetsdir)
with open(self.cli_config_path, "w") as file: with open(self.cli_config_path, "w") as file:
file.write("library: beets.db\n") file.write("library: beets.db\n")
file.write("statefile: state") file.write("statefile: state")
self.run_command("--config", self.cli_config_path, "test", lib=None) self.run_command("--config", self.cli_config_path, "test", lib=None)
self.assert_equal_path( assert config["library"].as_path() == self.beetsdir / "beets.db"
util.bytestring_path(config["library"].as_filename()), assert config["statefile"].as_path() == self.beetsdir / "state"
os.path.join(self.beetsdir, b"beets.db"),
)
self.assert_equal_path(
util.bytestring_path(config["statefile"].as_filename()),
os.path.join(self.beetsdir, b"state"),
)
def test_command_line_option_relative_to_working_dir(self): def test_command_line_option_relative_to_working_dir(self):
config.read() config.read()
os.chdir(syspath(self.temp_dir)) os.chdir(syspath(self.temp_dir))
self.run_command("--library", "foo.db", "test", lib=None) self.run_command("--library", "foo.db", "test", lib=None)
self.assert_equal_path( assert config["library"].as_path() == Path.cwd() / "foo.db"
config["library"].as_filename(), os.path.join(os.getcwd(), "foo.db")
)
def test_cli_config_file_loads_plugin_commands(self): def test_cli_config_file_loads_plugin_commands(self):
with open(self.cli_config_path, "w") as file: with open(self.cli_config_path, "w") as file:
@ -1050,24 +1028,23 @@ class ConfigTest(TestPluginTestCase):
self.unload_plugins() self.unload_plugins()
def test_beetsdir_config(self): def test_beetsdir_config(self):
os.environ["BEETSDIR"] = os.fsdecode(self.beetsdir) os.environ["BEETSDIR"] = str(self.beetsdir)
env_config_path = os.path.join(self.beetsdir, b"config.yaml") with open(self.env_config_path, "w") as file:
with open(env_config_path, "w") as file:
file.write("anoption: overwrite") file.write("anoption: overwrite")
config.read() config.read()
assert config["anoption"].get() == "overwrite" assert config["anoption"].get() == "overwrite"
def test_beetsdir_points_to_file_error(self): def test_beetsdir_points_to_file_error(self):
beetsdir = os.path.join(self.temp_dir, b"beetsfile") beetsdir = str(self.temp_dir_path / "beetsfile")
open(beetsdir, "a").close() open(beetsdir, "a").close()
os.environ["BEETSDIR"] = os.fsdecode(beetsdir) os.environ["BEETSDIR"] = beetsdir
with pytest.raises(ConfigError): with pytest.raises(ConfigError):
self.run_command("test") self.run_command("test")
def test_beetsdir_config_does_not_load_default_user_config(self): def test_beetsdir_config_does_not_load_default_user_config(self):
os.environ["BEETSDIR"] = os.fsdecode(self.beetsdir) os.environ["BEETSDIR"] = str(self.beetsdir)
with open(self.user_config_path, "w") as file: with open(self.user_config_path, "w") as file:
file.write("anoption: value") file.write("anoption: value")
@ -1076,35 +1053,22 @@ class ConfigTest(TestPluginTestCase):
assert not config["anoption"].exists() assert not config["anoption"].exists()
def test_default_config_paths_resolve_relative_to_beetsdir(self): def test_default_config_paths_resolve_relative_to_beetsdir(self):
os.environ["BEETSDIR"] = os.fsdecode(self.beetsdir) os.environ["BEETSDIR"] = str(self.beetsdir)
config.read() config.read()
self.assert_equal_path( assert config["library"].as_path() == self.beetsdir / "library.db"
util.bytestring_path(config["library"].as_filename()), assert config["statefile"].as_path() == self.beetsdir / "state.pickle"
os.path.join(self.beetsdir, b"library.db"),
)
self.assert_equal_path(
util.bytestring_path(config["statefile"].as_filename()),
os.path.join(self.beetsdir, b"state.pickle"),
)
def test_beetsdir_config_paths_resolve_relative_to_beetsdir(self): def test_beetsdir_config_paths_resolve_relative_to_beetsdir(self):
os.environ["BEETSDIR"] = os.fsdecode(self.beetsdir) os.environ["BEETSDIR"] = str(self.beetsdir)
env_config_path = os.path.join(self.beetsdir, b"config.yaml") with open(self.env_config_path, "w") as file:
with open(env_config_path, "w") as file:
file.write("library: beets.db\n") file.write("library: beets.db\n")
file.write("statefile: state") file.write("statefile: state")
config.read() config.read()
self.assert_equal_path( assert config["library"].as_path() == self.beetsdir / "beets.db"
util.bytestring_path(config["library"].as_filename()), assert config["statefile"].as_path() == self.beetsdir / "state"
os.path.join(self.beetsdir, b"beets.db"),
)
self.assert_equal_path(
util.bytestring_path(config["statefile"].as_filename()),
os.path.join(self.beetsdir, b"state"),
)
class ShowModelChangeTest(IOMixin, unittest.TestCase): class ShowModelChangeTest(IOMixin, unittest.TestCase):