| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import ctypes |
| 4 |
import sys |
| 5 |
from typing import TYPE_CHECKING, Any, ClassVar |
| 6 |
from unittest import mock |
| 7 |
|
| 8 |
from beets import importer |
| 9 |
from beets.test.helper import ( |
| 10 |
AutotagImportHelper, |
| 11 |
IOMixin, |
| 12 |
PluginMixin, |
| 13 |
PluginTestHelper, |
| 14 |
) |
| 15 |
from beetsplug.fetchart import CoverArtArchive, FetchArtPlugin, FileSystem |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from pathlib import Path |
| 19 |
|
| 20 |
|
| 21 |
class TestFetchartImport(PluginMixin, AutotagImportHelper): |
| 22 |
plugin = "fetchart" |
| 23 |
preload_plugin = False |
| 24 |
plugin_defaults: ClassVar[Any] = {"sources": ["filesystem", "coverart"]} |
| 25 |
|
| 26 |
def setup_beets(self): |
| 27 |
super().setup_beets() |
| 28 |
|
| 29 |
self.prepare_album_for_import(1) |
| 30 |
self.setup_importer() |
| 31 |
|
| 32 |
self.local_art_patcher = mock.patch.object( |
| 33 |
FileSystem, "get", autospec=True, return_value=iter(()) |
| 34 |
) |
| 35 |
self.local_art_mock = self.local_art_patcher.start() |
| 36 |
self.remote_art_patcher = mock.patch.object( |
| 37 |
CoverArtArchive, "get", autospec=True, return_value=iter(()) |
| 38 |
) |
| 39 |
self.remote_art_mock = self.remote_art_patcher.start() |
| 40 |
|
| 41 |
def teardown_beets(self): |
| 42 |
super().teardown_beets() |
| 43 |
self.local_art_patcher.stop() |
| 44 |
self.remote_art_patcher.stop() |
| 45 |
|
| 46 |
def test_asis_skips_network_sources(self): |
| 47 |
self.importer.add_choice(importer.Action.ASIS) |
| 48 |
with self.configure_plugin(self.plugin_defaults): |
| 49 |
self.importer.run() |
| 50 |
self.local_art_mock.assert_called() |
| 51 |
self.remote_art_mock.assert_not_called() |
| 52 |
|
| 53 |
def test_apply_uses_network_sources(self): |
| 54 |
self.importer.add_choice(importer.Action.APPLY) |
| 55 |
with self.configure_plugin(self.plugin_defaults): |
| 56 |
self.importer.run() |
| 57 |
self.local_art_mock.assert_called() |
| 58 |
self.remote_art_mock.assert_called() |
| 59 |
|
| 60 |
def test_fetch_for_asis_uses_network_sources(self): |
| 61 |
self.importer.add_choice(importer.Action.ASIS) |
| 62 |
with self.configure_plugin( |
| 63 |
self.plugin_defaults | {"fetch_for_asis": True} |
| 64 |
): |
| 65 |
self.importer.run() |
| 66 |
self.local_art_mock.assert_called() |
| 67 |
self.remote_art_mock.assert_called() |
| 68 |
|
| 69 |
|
| 70 |
class TestFetchartCli(IOMixin, PluginTestHelper): |
| 71 |
plugin = "fetchart" |
| 72 |
|
| 73 |
def setup_beets(self): |
| 74 |
super().setup_beets() |
| 75 |
self.config["fetchart"]["cover_names"] = "c\xc3\xb6ver.jpg" |
| 76 |
self.config["art_filename"] = "mycover" |
| 77 |
self.album = self.add_album() |
| 78 |
self.album.filepath.mkdir(parents=True) |
| 79 |
|
| 80 |
def cover_path(self, ext: str = "jpg"): |
| 81 |
return self.album.filepath / f"mycover.{ext}" |
| 82 |
|
| 83 |
def check_cover_is_stored(self, ext: str = "jpg"): |
| 84 |
cover_path = self.cover_path(ext) |
| 85 |
assert cover_path == self.album.art_filepath |
| 86 |
assert cover_path.read_text() == "IMAGE" |
| 87 |
|
| 88 |
def hide_file_windows(self, path: Path) -> None: |
| 89 |
if sys.platform == "win32": |
| 90 |
hidden_mask = 2 |
| 91 |
assert ctypes.windll.kernel32.SetFileAttributesW( |
| 92 |
str(path), hidden_mask |
| 93 |
) |
| 94 |
|
| 95 |
def test_set_art_from_folder(self): |
| 96 |
(self.album.filepath / "c\xc3\xb6ver.jpg").write_text("IMAGE") |
| 97 |
|
| 98 |
self.run_command("fetchart") |
| 99 |
|
| 100 |
self.album.load() |
| 101 |
self.check_cover_is_stored() |
| 102 |
|
| 103 |
def test_filesystem_does_not_pick_up_folder(self): |
| 104 |
self.run_command("fetchart") |
| 105 |
self.album.load() |
| 106 |
assert self.album["artpath"] is None |
| 107 |
|
| 108 |
def test_filesystem_does_not_pick_up_ignored_file(self): |
| 109 |
(self.album.filepath / "co_ver.jpg").write_text("IMAGE") |
| 110 |
self.config["ignore"] = ["*_*"] |
| 111 |
self.run_command("fetchart") |
| 112 |
self.album.load() |
| 113 |
assert self.album["artpath"] is None |
| 114 |
|
| 115 |
def test_filesystem_picks_up_non_ignored_file(self): |
| 116 |
(self.album.filepath / "cover.jpg").write_text("IMAGE") |
| 117 |
self.config["ignore"] = ["*_*"] |
| 118 |
self.run_command("fetchart") |
| 119 |
self.album.load() |
| 120 |
self.check_cover_is_stored() |
| 121 |
|
| 122 |
def test_filesystem_does_not_pick_up_hidden_file(self): |
| 123 |
path = self.album.filepath / ".cover.jpg" |
| 124 |
path.write_text("IMAGE") |
| 125 |
self.hide_file_windows(path) |
| 126 |
self.config["ignore"] = [] # By default, ignore includes '.*'. |
| 127 |
self.config["ignore_hidden"] = True |
| 128 |
self.run_command("fetchart") |
| 129 |
self.album.load() |
| 130 |
assert self.album["artpath"] is None |
| 131 |
|
| 132 |
def test_filesystem_picks_up_non_hidden_file(self): |
| 133 |
(self.album.filepath / "cover.jpg").write_text("IMAGE") |
| 134 |
self.config["ignore_hidden"] = True |
| 135 |
self.run_command("fetchart") |
| 136 |
self.album.load() |
| 137 |
self.check_cover_is_stored() |
| 138 |
|
| 139 |
def test_filesystem_picks_up_hidden_file(self): |
| 140 |
path = self.album.filepath / ".cover.jpg" |
| 141 |
path.write_text("IMAGE") |
| 142 |
self.hide_file_windows(path) |
| 143 |
self.config["ignore"] = [] # By default, ignore includes '.*'. |
| 144 |
self.config["ignore_hidden"] = False |
| 145 |
self.run_command("fetchart") |
| 146 |
self.album.load() |
| 147 |
self.check_cover_is_stored() |
| 148 |
|
| 149 |
def test_filesystem_picks_up_webp_file(self): |
| 150 |
(self.album.filepath / "cover.webp").write_text("IMAGE") |
| 151 |
self.run_command("fetchart") |
| 152 |
self.album.load() |
| 153 |
self.check_cover_is_stored("webp") |
| 154 |
|
| 155 |
def test_filesystem_picks_up_png_file(self): |
| 156 |
(self.album.filepath / "cover.png").write_text("IMAGE") |
| 157 |
self.run_command("fetchart") |
| 158 |
self.album.load() |
| 159 |
self.check_cover_is_stored("png") |
| 160 |
|
| 161 |
def test_colorization(self): |
| 162 |
self.config["ui"]["color"] = True |
| 163 |
out = self.run_with_output("fetchart") |
| 164 |
assert " - the älbum: \x1b[1;31mno art found\x1b[39;49;00m\n" == out |
| 165 |
|
| 166 |
def test_set_art_oserror_is_handled_gracefully(self): |
| 167 |
"""OSError (e.g. PermissionError) in set_art is logged as a warning, |
| 168 |
not an unhandled crash. Regression test for #6193. |
| 169 |
""" |
| 170 |
(self.album.filepath / "c\xc3\xb6ver.jpg").write_text("IMAGE") |
| 171 |
with mock.patch( |
| 172 |
"beets.library.Album.set_art", |
| 173 |
side_effect=PermissionError("[WinError 32] file in use"), |
| 174 |
): |
| 175 |
out = self.run_with_output("fetchart") |
| 176 |
self.album.load() |
| 177 |
assert "error writing album art" in out |
| 178 |
assert self.album["artpath"] is None |
| 179 |
|
| 180 |
def test_sources_is_a_string(self): |
| 181 |
self.config["fetchart"].set({"sources": "filesystem"}) |
| 182 |
fa = FetchArtPlugin() |
| 183 |
assert len(fa.sources) == 1 |
| 184 |
assert isinstance(fa.sources[0], FileSystem) |
| 185 |
|
| 186 |
def test_sources_is_an_asterisk(self): |
| 187 |
self.config["fetchart"].set({"sources": "*"}) |
| 188 |
fa = FetchArtPlugin() |
| 189 |
assert len(fa.sources) == 10 |
| 190 |
|
| 191 |
def test_sources_is_a_string_list(self): |
| 192 |
self.config["fetchart"].set({"sources": ["filesystem", "coverart"]}) |
| 193 |
fa = FetchArtPlugin() |
| 194 |
assert len(fa.sources) == 3 |
| 195 |
|
| 196 |
def test_sources_is_a_mapping_list(self): |
| 197 |
self.config["fetchart"].set( |
| 198 |
{"sources": {"filesystem": "*", "coverart": "*"}} |
| 199 |
) |
| 200 |
fa = FetchArtPlugin() |
| 201 |
assert len(fa.sources) == 3 |