beets/test/test_util.py
2025-05-04 12:23:26 +01:00

255 lines
8.9 KiB
Python

# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Tests for base utils from the beets.util package."""
import os
import platform
import re
import subprocess
import sys
import unittest
from unittest.mock import Mock, patch
import pytest
from beets import util
from beets.test import _common
from beets.test.helper import BeetsTestCase
class UtilTest(unittest.TestCase):
def test_open_anything(self):
with _common.system_mock("Windows"):
assert util.open_anything() == "start"
with _common.system_mock("Darwin"):
assert util.open_anything() == "open"
with _common.system_mock("Tagada"):
assert util.open_anything() == "xdg-open"
@patch("os.execlp")
@patch("beets.util.open_anything")
def test_interactive_open(self, mock_open, mock_execlp):
mock_open.return_value = "tagada"
util.interactive_open(["foo"], util.open_anything())
mock_execlp.assert_called_once_with("tagada", "tagada", "foo")
mock_execlp.reset_mock()
util.interactive_open(["foo"], "bar")
mock_execlp.assert_called_once_with("bar", "bar", "foo")
def test_sanitize_unix_replaces_leading_dot(self):
with _common.platform_posix():
p = util.sanitize_path("one/.two/three")
assert "." not in p
def test_sanitize_windows_replaces_trailing_dot(self):
with _common.platform_windows():
p = util.sanitize_path("one/two./three")
assert "." not in p
def test_sanitize_windows_replaces_illegal_chars(self):
with _common.platform_windows():
p = util.sanitize_path(':*?"<>|')
assert ":" not in p
assert "*" not in p
assert "?" not in p
assert '"' not in p
assert "<" not in p
assert ">" not in p
assert "|" not in p
def test_sanitize_windows_replaces_trailing_space(self):
with _common.platform_windows():
p = util.sanitize_path("one/two /three")
assert " " not in p
def test_sanitize_path_works_on_empty_string(self):
with _common.platform_posix():
p = util.sanitize_path("")
assert p == ""
def test_sanitize_with_custom_replace_overrides_built_in_sub(self):
with _common.platform_posix():
p = util.sanitize_path("a/.?/b", [(re.compile(r"foo"), "bar")])
assert p == "a/.?/b"
def test_sanitize_with_custom_replace_adds_replacements(self):
with _common.platform_posix():
p = util.sanitize_path("foo/bar", [(re.compile(r"foo"), "bar")])
assert p == "bar/bar"
@unittest.skip("unimplemented: #359")
def test_sanitize_empty_component(self):
with _common.platform_posix():
p = util.sanitize_path("foo//bar", [(re.compile(r"^$"), "_")])
assert p == "foo/_/bar"
@patch("beets.util.subprocess.Popen")
def test_command_output(self, mock_popen):
def popen_fail(*args, **kwargs):
m = Mock(returncode=1)
m.communicate.return_value = "foo", "bar"
return m
mock_popen.side_effect = popen_fail
with pytest.raises(subprocess.CalledProcessError) as exc_info:
util.command_output(["taga", "\xc3\xa9"])
assert exc_info.value.returncode == 1
assert exc_info.value.cmd == "taga \xc3\xa9"
def test_case_sensitive_default(self):
path = util.bytestring_path(
util.normpath(
"/this/path/does/not/exist",
)
)
assert util.case_sensitive(path) == (platform.system() != "Windows")
@unittest.skipIf(sys.platform == "win32", "fs is not case sensitive")
def test_case_sensitive_detects_sensitive(self):
# FIXME: Add tests for more code paths of case_sensitive()
# when the filesystem on the test runner is not case sensitive
pass
@unittest.skipIf(sys.platform != "win32", "fs is case sensitive")
def test_case_sensitive_detects_insensitive(self):
# FIXME: Add tests for more code paths of case_sensitive()
# when the filesystem on the test runner is case sensitive
pass
class PathConversionTest(BeetsTestCase):
def test_syspath_windows_format(self):
with _common.platform_windows():
path = os.path.join("a", "b", "c")
outpath = util.syspath(path)
assert isinstance(outpath, str)
assert outpath.startswith("\\\\?\\")
def test_syspath_windows_format_unc_path(self):
# The \\?\ prefix on Windows behaves differently with UNC
# (network share) paths.
path = "\\\\server\\share\\file.mp3"
with _common.platform_windows():
outpath = util.syspath(path)
assert isinstance(outpath, str)
assert outpath == "\\\\?\\UNC\\server\\share\\file.mp3"
def test_syspath_posix_unchanged(self):
with _common.platform_posix():
path = os.path.join("a", "b", "c")
outpath = util.syspath(path)
assert path == outpath
def _windows_bytestring_path(self, path):
with _common.platform_windows():
return util.bytestring_path(path)
def test_bytestring_path_windows_encodes_utf8(self):
path = "caf\xe9"
outpath = self._windows_bytestring_path(path)
assert path == outpath.decode("utf-8")
def test_bytesting_path_windows_removes_magic_prefix(self):
path = "\\\\?\\C:\\caf\xe9"
outpath = self._windows_bytestring_path(path)
assert outpath == "C:\\caf\xe9".encode()
class TestPathLegalization:
@pytest.fixture(autouse=True)
def _patch_max_filename_length(self, monkeypatch):
monkeypatch.setattr("beets.util.get_max_filename_length", lambda: 5)
@pytest.mark.parametrize(
"path, expected",
[
("abcdeX/fgh", "abcde/fgh"),
("abcde/fXX.ext", "abcde/f.ext"),
("a🎹/a.ext", "a🎹/a.ext"),
("ab🎹/a.ext", "ab/a.ext"),
],
)
def test_truncate(self, path, expected):
path = path.replace("/", os.path.sep)
expected = expected.replace("/", os.path.sep)
assert util.truncate_path(path) == expected
@pytest.mark.parametrize(
"pre_trunc_repl, post_trunc_repl, expected",
[
pytest.param(
[],
[],
("_abcd", False),
id="default",
),
pytest.param(
[(re.compile(r"abcdX$"), "PRE")],
[],
(":PRE", False),
id="valid path after initial repl",
),
pytest.param(
[(re.compile(r"abcdX$"), "PRE_LONG")],
[],
(":PRE_", False),
id="too long path after initial repl is truncated",
),
pytest.param(
[],
[(re.compile(r"abcdX$"), "POST")],
(":POST", False),
id="valid path after post-trunc repl",
),
pytest.param(
[],
[(re.compile(r"abcdX$"), "POST_LONG")],
(":POST", False),
id="too long path after post-trunc repl is truncated",
),
pytest.param(
[(re.compile(r"abcdX$"), "PRE")],
[(re.compile(r"PRE$"), "POST")],
(":POST", False),
id="both replacements within filename length limit",
),
pytest.param(
[(re.compile(r"abcdX$"), "PRE_LONG")],
[(re.compile(r"PRE_$"), "POST")],
(":POST", False),
id="too long initial path is truncated and valid post-trunc repl",
),
pytest.param(
[(re.compile(r"abcdX$"), "PRE")],
[(re.compile(r"PRE$"), "POST_LONG")],
(":POST", False),
id="valid pre-trunc repl and too long post-trunc path is truncated",
),
pytest.param(
[(re.compile(r"abcdX$"), "PRE_LONG")],
[(re.compile(r"PRE_$"), "POST_LONG")],
("_PRE_", True),
id="too long repl both times force default ones to be applied",
),
],
)
def test_replacements(self, pre_trunc_repl, post_trunc_repl, expected):
replacements = pre_trunc_repl + post_trunc_repl
assert util.legalize_path(":abcdX", replacements, "") == expected