| 1 |
import os.path |
| 2 |
from unittest.mock import Mock, call, patch |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from beets.test.helper import BeetsTestCase |
| 7 |
from beets.util import syspath |
| 8 |
from beetsplug.thumbnails import ( |
| 9 |
LARGE_DIR, |
| 10 |
NORMAL_DIR, |
| 11 |
GioURI, |
| 12 |
PathlibURI, |
| 13 |
ThumbnailsPlugin, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class ThumbnailsTest(BeetsTestCase): |
| 18 |
@patch("beetsplug.thumbnails.ArtResizer") |
| 19 |
@patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock()) |
| 20 |
@patch("beetsplug.thumbnails.os.stat") |
| 21 |
def test_add_tags(self, mock_stat, mock_artresizer): |
| 22 |
plugin = ThumbnailsPlugin() |
| 23 |
plugin.get_uri = Mock( |
| 24 |
side_effect={b"/path/to/cover": "COVER_URI"}.__getitem__ |
| 25 |
) |
| 26 |
artpath = b"/path/to/cover" |
| 27 |
mock_stat.return_value.st_mtime = 12345 |
| 28 |
|
| 29 |
plugin.add_tags(artpath, b"/path/to/thumbnail") |
| 30 |
|
| 31 |
metadata = {"Thumb::URI": "COVER_URI", "Thumb::MTime": "12345"} |
| 32 |
mock_artresizer.shared.write_metadata.assert_called_once_with( |
| 33 |
b"/path/to/thumbnail", metadata |
| 34 |
) |
| 35 |
mock_stat.assert_called_once_with(syspath(artpath)) |
| 36 |
|
| 37 |
@patch("beetsplug.thumbnails.os") |
| 38 |
@patch("beetsplug.thumbnails.ArtResizer") |
| 39 |
@patch("beetsplug.thumbnails.GioURI") |
| 40 |
def test_check_local_ok(self, mock_giouri, mock_artresizer, mock_os): |
| 41 |
# test local resizing capability |
| 42 |
mock_artresizer.shared.local = False |
| 43 |
mock_artresizer.shared.can_write_metadata = False |
| 44 |
plugin = ThumbnailsPlugin() |
| 45 |
assert not plugin._check_local_ok() |
| 46 |
|
| 47 |
# test dirs creation |
| 48 |
mock_artresizer.shared.local = True |
| 49 |
mock_artresizer.shared.can_write_metadata = True |
| 50 |
|
| 51 |
def exists(path): |
| 52 |
if path == syspath(NORMAL_DIR): |
| 53 |
return False |
| 54 |
if path == syspath(LARGE_DIR): |
| 55 |
return True |
| 56 |
raise ValueError(f"unexpected path {path!r}") |
| 57 |
|
| 58 |
mock_os.path.exists = exists |
| 59 |
plugin = ThumbnailsPlugin() |
| 60 |
mock_os.makedirs.assert_called_once_with(syspath(NORMAL_DIR)) |
| 61 |
assert plugin._check_local_ok() |
| 62 |
|
| 63 |
# test metadata writer function |
| 64 |
mock_os.path.exists = lambda _: True |
| 65 |
|
| 66 |
mock_artresizer.shared.local = True |
| 67 |
mock_artresizer.shared.can_write_metadata = False |
| 68 |
with pytest.raises(RuntimeError): |
| 69 |
ThumbnailsPlugin() |
| 70 |
|
| 71 |
mock_artresizer.shared.local = True |
| 72 |
mock_artresizer.shared.can_write_metadata = True |
| 73 |
assert ThumbnailsPlugin()._check_local_ok() |
| 74 |
|
| 75 |
# test URI getter function |
| 76 |
giouri_inst = mock_giouri.return_value |
| 77 |
giouri_inst.available = True |
| 78 |
assert ThumbnailsPlugin().get_uri == giouri_inst.uri |
| 79 |
|
| 80 |
giouri_inst.available = False |
| 81 |
assert ThumbnailsPlugin().get_uri.__self__.__class__ == PathlibURI |
| 82 |
|
| 83 |
@patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock()) |
| 84 |
@patch("beetsplug.thumbnails.ArtResizer") |
| 85 |
@patch("beets.util.syspath", Mock(side_effect=lambda x: x)) |
| 86 |
@patch("beetsplug.thumbnails.os") |
| 87 |
@patch("beetsplug.thumbnails.shutil") |
| 88 |
def test_make_cover_thumbnail(self, mock_shutils, mock_os, mock_artresizer): |
| 89 |
thumbnail_dir = os.path.normpath(b"/thumbnail/dir") |
| 90 |
md5_file = os.path.join(thumbnail_dir, b"md5") |
| 91 |
artpath = os.path.normpath(b"/path/to/art") |
| 92 |
path_to_resized_art = os.path.normpath(b"/path/to/resized/artwork") |
| 93 |
|
| 94 |
mock_os.path.join = os.path.join # don't mock that function |
| 95 |
plugin = ThumbnailsPlugin() |
| 96 |
plugin.add_tags = Mock() |
| 97 |
|
| 98 |
album = Mock(artpath=artpath) |
| 99 |
plugin.thumbnail_file_name = Mock(return_value=b"md5") |
| 100 |
mock_os.path.exists.return_value = False |
| 101 |
|
| 102 |
mock_resize = mock_artresizer.shared.resize |
| 103 |
mock_resize.return_value = path_to_resized_art |
| 104 |
|
| 105 |
plugin.make_cover_thumbnail(album, artpath, 12345, thumbnail_dir) |
| 106 |
|
| 107 |
mock_os.path.exists.assert_called_once_with(syspath(md5_file)) |
| 108 |
|
| 109 |
mock_resize.assert_called_once_with(12345, artpath, md5_file) |
| 110 |
plugin.add_tags.assert_called_once_with(artpath, path_to_resized_art) |
| 111 |
mock_shutils.move.assert_called_once_with( |
| 112 |
syspath(path_to_resized_art), syspath(md5_file) |
| 113 |
) |
| 114 |
|
| 115 |
# now test with recent thumbnail & with force |
| 116 |
mock_os.path.exists.return_value = True |
| 117 |
plugin.force = False |
| 118 |
mock_resize.reset_mock() |
| 119 |
|
| 120 |
def os_stat(target): |
| 121 |
if target == syspath(md5_file): |
| 122 |
return Mock(st_mtime=3) |
| 123 |
if target == syspath(artpath): |
| 124 |
return Mock(st_mtime=2) |
| 125 |
raise ValueError(f"invalid target {target}") |
| 126 |
|
| 127 |
mock_os.stat.side_effect = os_stat |
| 128 |
|
| 129 |
plugin.make_cover_thumbnail(album, artpath, 12345, thumbnail_dir) |
| 130 |
assert mock_resize.call_count == 0 |
| 131 |
|
| 132 |
# and with force |
| 133 |
plugin.config["force"] = True |
| 134 |
plugin.make_cover_thumbnail(album, artpath, 12345, thumbnail_dir) |
| 135 |
mock_resize.assert_called_once_with(12345, artpath, md5_file) |
| 136 |
|
| 137 |
@patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock()) |
| 138 |
def test_make_dolphin_cover_thumbnail(self): |
| 139 |
plugin = ThumbnailsPlugin() |
| 140 |
tmp = self.temp_path |
| 141 |
album = Mock( |
| 142 |
path=os.fsencode(tmp), artpath=os.fsencode(tmp / "cover.jpg") |
| 143 |
) |
| 144 |
plugin.make_dolphin_cover_thumbnail(album.path, album.artpath) |
| 145 |
filename = tmp / ".directory" |
| 146 |
assert filename.read_text() == "[Desktop Entry]\nIcon=./cover.jpg" |
| 147 |
|
| 148 |
# not rewritten when it already exists (yup that's a big limitation) |
| 149 |
album.artpath = b"/my/awesome/art.tiff" |
| 150 |
plugin.make_dolphin_cover_thumbnail(album.path, album.artpath) |
| 151 |
assert filename.read_text() == "[Desktop Entry]\nIcon=./cover.jpg" |
| 152 |
|
| 153 |
@patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock()) |
| 154 |
@patch("beetsplug.thumbnails.ArtResizer") |
| 155 |
def test_process_album(self, mock_artresizer): |
| 156 |
get_size = mock_artresizer.shared.get_size |
| 157 |
|
| 158 |
plugin = ThumbnailsPlugin() |
| 159 |
make_cover = plugin.make_cover_thumbnail = Mock(return_value=True) |
| 160 |
make_dolphin = plugin.make_dolphin_cover_thumbnail = Mock() |
| 161 |
|
| 162 |
# no art |
| 163 |
album = Mock(artpath=None) |
| 164 |
plugin.process_album(album) |
| 165 |
assert get_size.call_count == 0 |
| 166 |
assert make_dolphin.call_count == 0 |
| 167 |
|
| 168 |
# cannot get art size |
| 169 |
album.artpath = b"/path/to/art" |
| 170 |
get_size.return_value = None |
| 171 |
plugin.process_album(album) |
| 172 |
get_size.assert_called_once_with(b"/path/to/art") |
| 173 |
assert make_cover.call_count == 0 |
| 174 |
|
| 175 |
# dolphin tests |
| 176 |
plugin.config["dolphin"] = False |
| 177 |
plugin.process_album(album) |
| 178 |
assert make_dolphin.call_count == 0 |
| 179 |
|
| 180 |
plugin.config["dolphin"] = True |
| 181 |
plugin.process_album(album) |
| 182 |
make_dolphin.assert_called_once_with(album.path, album.artpath) |
| 183 |
|
| 184 |
# small art |
| 185 |
get_size.return_value = 200, 200 |
| 186 |
plugin.process_album(album) |
| 187 |
make_cover.assert_called_once_with( |
| 188 |
album, album.artpath, 128, NORMAL_DIR |
| 189 |
) |
| 190 |
|
| 191 |
# big art |
| 192 |
make_cover.reset_mock() |
| 193 |
get_size.return_value = 500, 500 |
| 194 |
plugin.process_album(album) |
| 195 |
make_cover.assert_has_calls( |
| 196 |
[ |
| 197 |
call(album, album.artpath, 128, NORMAL_DIR), |
| 198 |
call(album, album.artpath, 256, LARGE_DIR), |
| 199 |
], |
| 200 |
any_order=True, |
| 201 |
) |
| 202 |
|
| 203 |
@patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock()) |
| 204 |
def test_invokations(self): |
| 205 |
plugin = ThumbnailsPlugin() |
| 206 |
plugin.process_album = Mock() |
| 207 |
album = Mock() |
| 208 |
|
| 209 |
plugin.process_album.reset_mock() |
| 210 |
lib = Mock() |
| 211 |
album2 = Mock() |
| 212 |
lib.albums.return_value = [album, album2] |
| 213 |
plugin.process_query(lib, Mock(), None) |
| 214 |
plugin.process_album.assert_has_calls( |
| 215 |
[call(album), call(album2)], any_order=True |
| 216 |
) |
| 217 |
|
| 218 |
@patch("beetsplug.thumbnails.BaseDirectory") |
| 219 |
def test_thumbnail_file_name(self, mock_basedir): |
| 220 |
plug = ThumbnailsPlugin() |
| 221 |
plug.get_uri = Mock(return_value="file:///my/uri") |
| 222 |
assert ( |
| 223 |
plug.thumbnail_file_name(b"idontcare") |
| 224 |
== b"9488f5797fbe12ffb316d607dfd93d04.png" |
| 225 |
) |
| 226 |
|
| 227 |
def test_uri(self): |
| 228 |
gio = GioURI() |
| 229 |
if not gio.available: |
| 230 |
self.skipTest("GIO library not found") |
| 231 |
|
| 232 |
import ctypes |
| 233 |
|
| 234 |
with pytest.raises(ctypes.ArgumentError): |
| 235 |
gio.uri("/foo") |
| 236 |
assert gio.uri(b"/foo") == "file:///foo" |
| 237 |
assert gio.uri(b"/foo!") == "file:///foo!" |
| 238 |
assert ( |
| 239 |
gio.uri(b"/music/\xec\x8b\xb8\xec\x9d\xb4") |
| 240 |
== "file:///music/%EC%8B%B8%EC%9D%B4" |
| 241 |
) |
| 242 |
|
| 243 |
|
| 244 |
class TestPathlibURI: |
| 245 |
"""Test PathlibURI class""" |
| 246 |
|
| 247 |
def test_uri(self): |
| 248 |
test_uri = PathlibURI() |
| 249 |
|
| 250 |
# test it won't break if we pass it bytes for a path |
| 251 |
test_uri.uri(b"/") |