Use pathlib.Path in test_smartplaylist.py

This commit is contained in:
Šarūnas Nejus 2025-05-27 13:03:30 +01:00
parent edd3df99ba
commit d017270196
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435

View file

@ -13,7 +13,8 @@
# included in all copies or substantial portions of the Software. # included in all copies or substantial portions of the Software.
from os import fsdecode, path, remove from os import path, remove
from pathlib import Path
from shutil import rmtree from shutil import rmtree
from tempfile import mkdtemp from tempfile import mkdtemp
from unittest.mock import MagicMock, Mock, PropertyMock from unittest.mock import MagicMock, Mock, PropertyMock
@ -26,7 +27,7 @@ from beets.dbcore.query import FixedFieldSort, MultipleSort, NullSort
from beets.library import Album, Item, parse_query_string from beets.library import Album, Item, parse_query_string
from beets.test.helper import BeetsTestCase, PluginTestCase from beets.test.helper import BeetsTestCase, PluginTestCase
from beets.ui import UserError from beets.ui import UserError
from beets.util import CHAR_REPLACE, bytestring_path, syspath from beets.util import CHAR_REPLACE, syspath
from beetsplug.smartplaylist import SmartPlaylistPlugin from beetsplug.smartplaylist import SmartPlaylistPlugin
@ -165,9 +166,9 @@ class SmartPlaylistTest(BeetsTestCase):
pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None) pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None)
spl._matched_playlists = [pl] spl._matched_playlists = [pl]
dir = bytestring_path(mkdtemp()) dir = mkdtemp()
config["smartplaylist"]["relative_to"] = False config["smartplaylist"]["relative_to"] = False
config["smartplaylist"]["playlist_dir"] = fsdecode(dir) config["smartplaylist"]["playlist_dir"] = str(dir)
try: try:
spl.update_playlists(lib) spl.update_playlists(lib)
except Exception: except Exception:
@ -177,10 +178,9 @@ class SmartPlaylistTest(BeetsTestCase):
lib.items.assert_called_once_with(q, None) lib.items.assert_called_once_with(q, None)
lib.albums.assert_called_once_with(a_q, None) lib.albums.assert_called_once_with(a_q, None)
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u") m3u_filepath = Path(dir, "ta_ga_da-my_playlist_.m3u")
self.assertExists(m3u_filepath) assert m3u_filepath.exists()
with open(syspath(m3u_filepath), "rb") as f: content = m3u_filepath.read_bytes()
content = f.read()
rmtree(syspath(dir)) rmtree(syspath(dir))
assert content == b"/tagada.mp3\n" assert content == b"/tagada.mp3\n"
@ -208,11 +208,11 @@ class SmartPlaylistTest(BeetsTestCase):
pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None) pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None)
spl._matched_playlists = [pl] spl._matched_playlists = [pl]
dir = bytestring_path(mkdtemp()) dir = mkdtemp()
config["smartplaylist"]["output"] = "extm3u" config["smartplaylist"]["output"] = "extm3u"
config["smartplaylist"]["prefix"] = "http://beets:8337/files" config["smartplaylist"]["prefix"] = "http://beets:8337/files"
config["smartplaylist"]["relative_to"] = False config["smartplaylist"]["relative_to"] = False
config["smartplaylist"]["playlist_dir"] = fsdecode(dir) config["smartplaylist"]["playlist_dir"] = str(dir)
try: try:
spl.update_playlists(lib) spl.update_playlists(lib)
except Exception: except Exception:
@ -222,10 +222,9 @@ class SmartPlaylistTest(BeetsTestCase):
lib.items.assert_called_once_with(q, None) lib.items.assert_called_once_with(q, None)
lib.albums.assert_called_once_with(a_q, None) lib.albums.assert_called_once_with(a_q, None)
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u") m3u_filepath = Path(dir, "ta_ga_da-my_playlist_.m3u")
self.assertExists(m3u_filepath) assert m3u_filepath.exists()
with open(syspath(m3u_filepath), "rb") as f: content = m3u_filepath.read_bytes()
content = f.read()
rmtree(syspath(dir)) rmtree(syspath(dir))
assert ( assert (
@ -260,10 +259,10 @@ class SmartPlaylistTest(BeetsTestCase):
pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None) pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None)
spl._matched_playlists = [pl] spl._matched_playlists = [pl]
dir = bytestring_path(mkdtemp()) dir = mkdtemp()
config["smartplaylist"]["output"] = "extm3u" config["smartplaylist"]["output"] = "extm3u"
config["smartplaylist"]["relative_to"] = False config["smartplaylist"]["relative_to"] = False
config["smartplaylist"]["playlist_dir"] = fsdecode(dir) config["smartplaylist"]["playlist_dir"] = str(dir)
config["smartplaylist"]["fields"] = ["id", "genre"] config["smartplaylist"]["fields"] = ["id", "genre"]
try: try:
spl.update_playlists(lib) spl.update_playlists(lib)
@ -274,10 +273,9 @@ class SmartPlaylistTest(BeetsTestCase):
lib.items.assert_called_once_with(q, None) lib.items.assert_called_once_with(q, None)
lib.albums.assert_called_once_with(a_q, None) lib.albums.assert_called_once_with(a_q, None)
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u") m3u_filepath = Path(dir, "ta_ga_da-my_playlist_.m3u")
self.assertExists(m3u_filepath) assert m3u_filepath.exists()
with open(syspath(m3u_filepath), "rb") as f: content = m3u_filepath.read_bytes()
content = f.read()
rmtree(syspath(dir)) rmtree(syspath(dir))
assert ( assert (
@ -307,10 +305,10 @@ class SmartPlaylistTest(BeetsTestCase):
pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None) pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None)
spl._matched_playlists = [pl] spl._matched_playlists = [pl]
dir = bytestring_path(mkdtemp()) dir = mkdtemp()
tpl = "http://beets:8337/item/$id/file" tpl = "http://beets:8337/item/$id/file"
config["smartplaylist"]["uri_format"] = tpl config["smartplaylist"]["uri_format"] = tpl
config["smartplaylist"]["playlist_dir"] = fsdecode(dir) config["smartplaylist"]["playlist_dir"] = dir
# The following options should be ignored when uri_format is set # The following options should be ignored when uri_format is set
config["smartplaylist"]["relative_to"] = "/data" config["smartplaylist"]["relative_to"] = "/data"
config["smartplaylist"]["prefix"] = "/prefix" config["smartplaylist"]["prefix"] = "/prefix"
@ -324,10 +322,9 @@ class SmartPlaylistTest(BeetsTestCase):
lib.items.assert_called_once_with(q, None) lib.items.assert_called_once_with(q, None)
lib.albums.assert_called_once_with(a_q, None) lib.albums.assert_called_once_with(a_q, None)
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u") m3u_filepath = Path(dir, "ta_ga_da-my_playlist_.m3u")
self.assertExists(m3u_filepath) assert m3u_filepath.exists()
with open(syspath(m3u_filepath), "rb") as f: content = m3u_filepath.read_bytes()
content = f.read()
rmtree(syspath(dir)) rmtree(syspath(dir))
assert content == b"http://beets:8337/item/3/file\n" assert content == b"http://beets:8337/item/3/file\n"
@ -346,22 +343,20 @@ class SmartPlaylistCLITest(PluginTestCase):
{"name": "all.m3u", "query": ""}, {"name": "all.m3u", "query": ""},
] ]
) )
config["smartplaylist"]["playlist_dir"].set(fsdecode(self.temp_dir)) config["smartplaylist"]["playlist_dir"].set(str(self.temp_dir_path))
def test_splupdate(self): def test_splupdate(self):
with pytest.raises(UserError): with pytest.raises(UserError):
self.run_with_output("splupdate", "tagada") self.run_with_output("splupdate", "tagada")
self.run_with_output("splupdate", "my_playlist") self.run_with_output("splupdate", "my_playlist")
m3u_path = path.join(self.temp_dir, b"my_playlist.m3u") m3u_path = self.temp_dir_path / "my_playlist.m3u"
self.assertExists(m3u_path) assert m3u_path.exists()
with open(syspath(m3u_path), "rb") as f: assert m3u_path.read_bytes() == self.item.path + b"\n"
assert f.read() == self.item.path + b"\n"
remove(syspath(m3u_path)) remove(syspath(m3u_path))
self.run_with_output("splupdate", "my_playlist.m3u") self.run_with_output("splupdate", "my_playlist.m3u")
with open(syspath(m3u_path), "rb") as f: assert m3u_path.read_bytes() == self.item.path + b"\n"
assert f.read() == self.item.path + b"\n"
remove(syspath(m3u_path)) remove(syspath(m3u_path))
self.run_with_output("splupdate") self.run_with_output("splupdate")