| 1 |
"""Test file manipulation functionality of Item.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import stat |
| 6 |
import unittest |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import beets.library |
| 10 |
from beets import util |
| 11 |
from beets.test import _common |
| 12 |
from beets.test._common import item |
| 13 |
from beets.test.helper import NEEDS_REFLINK, BeetsTestCase |
| 14 |
from beets.util import MoveOperation, syspath |
| 15 |
|
| 16 |
|
| 17 |
class MoveTest(BeetsTestCase): |
| 18 |
def setUp(self): |
| 19 |
super().setUp() |
| 20 |
|
| 21 |
# make a temporary file |
| 22 |
self.temp_music_file_name = Path("temp.mp3") |
| 23 |
self.path = self.temp_path / self.temp_music_file_name |
| 24 |
shutil.copy(self.resource_path, self.path) |
| 25 |
|
| 26 |
# add it to a temporary library |
| 27 |
self.i = beets.library.Item.from_path(self.path) |
| 28 |
self.lib.add(self.i) |
| 29 |
|
| 30 |
# set up the destination |
| 31 |
self.lib.path_formats = [ |
| 32 |
("default", str(Path("$artist") / "$album" / "$title")) |
| 33 |
] |
| 34 |
self.i.artist = "one" |
| 35 |
self.i.album = "two" |
| 36 |
self.i.title = "three" |
| 37 |
self.dest = self.lib_path / "one" / "two" / "three.mp3" |
| 38 |
|
| 39 |
self.otherdir = self.temp_path / "testotherdir" |
| 40 |
|
| 41 |
def test_move_arrives(self): |
| 42 |
self.i.move() |
| 43 |
assert self.dest.exists() |
| 44 |
|
| 45 |
def test_move_to_custom_dir(self): |
| 46 |
self.i.move(basedir=os.fsencode(self.otherdir)) |
| 47 |
assert (self.otherdir / "one" / "two" / "three.mp3").exists() |
| 48 |
|
| 49 |
def test_move_departs(self): |
| 50 |
self.i.move() |
| 51 |
assert not self.path.exists() |
| 52 |
|
| 53 |
def test_move_in_lib_prunes_empty_dir(self): |
| 54 |
self.i.move() |
| 55 |
old_path = self.i.filepath |
| 56 |
assert old_path.exists() |
| 57 |
|
| 58 |
self.i.artist = "newArtist" |
| 59 |
self.i.move() |
| 60 |
assert not old_path.exists() |
| 61 |
assert not old_path.parent.exists() |
| 62 |
|
| 63 |
def test_copy_arrives(self): |
| 64 |
self.i.move(operation=MoveOperation.COPY) |
| 65 |
assert self.dest.exists() |
| 66 |
|
| 67 |
def test_copy_does_not_depart(self): |
| 68 |
self.i.move(operation=MoveOperation.COPY) |
| 69 |
assert self.path.exists() |
| 70 |
|
| 71 |
def test_reflink_arrives(self): |
| 72 |
self.i.move(operation=MoveOperation.REFLINK_AUTO) |
| 73 |
assert self.dest.exists() |
| 74 |
|
| 75 |
def test_reflink_does_not_depart(self): |
| 76 |
self.i.move(operation=MoveOperation.REFLINK_AUTO) |
| 77 |
assert self.path.exists() |
| 78 |
|
| 79 |
@NEEDS_REFLINK |
| 80 |
def test_force_reflink_arrives(self): |
| 81 |
self.i.move(operation=MoveOperation.REFLINK) |
| 82 |
assert self.dest.exists() |
| 83 |
|
| 84 |
@NEEDS_REFLINK |
| 85 |
def test_force_reflink_does_not_depart(self): |
| 86 |
self.i.move(operation=MoveOperation.REFLINK) |
| 87 |
assert self.path.exists() |
| 88 |
|
| 89 |
def test_move_changes_path(self): |
| 90 |
self.i.move() |
| 91 |
assert self.i.path == util.normpath(self.dest) |
| 92 |
|
| 93 |
def test_copy_already_at_destination(self): |
| 94 |
self.i.move() |
| 95 |
old_path = self.i.path |
| 96 |
self.i.move(operation=MoveOperation.COPY) |
| 97 |
assert self.i.path == old_path |
| 98 |
|
| 99 |
def test_move_already_at_destination(self): |
| 100 |
self.i.move() |
| 101 |
old_path = self.i.path |
| 102 |
self.i.move() |
| 103 |
assert self.i.path == old_path |
| 104 |
|
| 105 |
def test_move_file_with_colon(self): |
| 106 |
self.i.artist = "C:DOS" |
| 107 |
self.i.move() |
| 108 |
assert "C_DOS" in self.i.path.decode() |
| 109 |
|
| 110 |
def test_move_file_with_multiple_colons(self): |
| 111 |
# print(beets.config["replace"]) |
| 112 |
self.i.artist = "COM:DOS" |
| 113 |
self.i.move() |
| 114 |
assert "COM_DOS" in self.i.path.decode() |
| 115 |
|
| 116 |
def test_move_file_with_colon_alt_separator(self): |
| 117 |
old = beets.config["drive_sep_replace"] |
| 118 |
beets.config["drive_sep_replace"] = "0" |
| 119 |
self.i.artist = "C:DOS" |
| 120 |
self.i.move() |
| 121 |
assert "C0DOS" in self.i.path.decode() |
| 122 |
beets.config["drive_sep_replace"] = old |
| 123 |
|
| 124 |
def test_read_only_file_copied_writable(self): |
| 125 |
# Make the source file read-only. |
| 126 |
self.path.chmod(0o444) |
| 127 |
|
| 128 |
try: |
| 129 |
self.i.move(operation=MoveOperation.COPY) |
| 130 |
assert os.access(self.i.filepath, os.W_OK) |
| 131 |
finally: |
| 132 |
# Make everything writable so it can be cleaned up. |
| 133 |
self.path.chmod(0o777) |
| 134 |
self.i.filepath.chmod(0o777) |
| 135 |
|
| 136 |
def test_move_avoids_collision_with_existing_file(self): |
| 137 |
# Make a conflicting file at the destination. |
| 138 |
dest = Path(os.fsdecode(self.i.destination())) |
| 139 |
util.mkdirall(dest) |
| 140 |
dest.touch() |
| 141 |
|
| 142 |
self.i.move() |
| 143 |
assert self.i.filepath != dest |
| 144 |
assert self.i.filepath.parent == dest.parent |
| 145 |
|
| 146 |
@unittest.skipUnless(_common.HAVE_SYMLINK, "need symlinks") |
| 147 |
def test_link_arrives(self): |
| 148 |
self.i.move(operation=MoveOperation.LINK) |
| 149 |
assert self.dest.exists() |
| 150 |
assert self.dest.is_symlink() |
| 151 |
assert self.dest.resolve() == self.path.resolve() |
| 152 |
|
| 153 |
@unittest.skipUnless(_common.HAVE_SYMLINK, "need symlinks") |
| 154 |
def test_link_does_not_depart(self): |
| 155 |
self.i.move(operation=MoveOperation.LINK) |
| 156 |
assert self.path.exists() |
| 157 |
|
| 158 |
@unittest.skipUnless(_common.HAVE_SYMLINK, "need symlinks") |
| 159 |
def test_link_changes_path(self): |
| 160 |
self.i.move(operation=MoveOperation.LINK) |
| 161 |
assert self.i.path == util.normpath(self.dest) |
| 162 |
|
| 163 |
@unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks") |
| 164 |
def test_hardlink_arrives(self): |
| 165 |
self.i.move(operation=MoveOperation.HARDLINK) |
| 166 |
assert self.dest.exists() |
| 167 |
s1 = self.path.stat() |
| 168 |
s2 = self.dest.stat() |
| 169 |
assert (s1[stat.ST_INO], s1[stat.ST_DEV]) == ( |
| 170 |
s2[stat.ST_INO], |
| 171 |
s2[stat.ST_DEV], |
| 172 |
) |
| 173 |
|
| 174 |
@unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks") |
| 175 |
def test_hardlink_does_not_depart(self): |
| 176 |
self.i.move(operation=MoveOperation.HARDLINK) |
| 177 |
assert self.path.exists() |
| 178 |
|
| 179 |
@unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks") |
| 180 |
def test_hardlink_changes_path(self): |
| 181 |
self.i.move(operation=MoveOperation.HARDLINK) |
| 182 |
assert self.i.path == util.normpath(self.dest) |
| 183 |
|
| 184 |
@unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks") |
| 185 |
def test_hardlink_from_symlink(self): |
| 186 |
link_path = self.temp_path / "temp_link.mp3" |
| 187 |
link_path.symlink_to(self.temp_music_file_name) |
| 188 |
self.i.path = link_path |
| 189 |
self.i.move(operation=MoveOperation.HARDLINK) |
| 190 |
|
| 191 |
s1 = self.path.stat() |
| 192 |
s2 = self.dest.stat() |
| 193 |
assert (s1[stat.ST_INO], s1[stat.ST_DEV]) == ( |
| 194 |
s2[stat.ST_INO], |
| 195 |
s2[stat.ST_DEV], |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
class AlbumFileTest(BeetsTestCase): |
| 200 |
def setUp(self): |
| 201 |
super().setUp() |
| 202 |
|
| 203 |
# Make library and item. |
| 204 |
self.lib.path_formats = [ |
| 205 |
("default", str(Path("$albumartist") / "$album" / "$title")) |
| 206 |
] |
| 207 |
self.i = item(self.lib) |
| 208 |
# Make a file for the item. |
| 209 |
self.i.path = self.i.destination() |
| 210 |
util.mkdirall(self.i.filepath) |
| 211 |
self.i.filepath.touch() |
| 212 |
# Make an album. |
| 213 |
self.ai = self.lib.add_album((self.i,)) |
| 214 |
# Alternate destination dir. |
| 215 |
self.otherdir = os.fsencode(self.temp_path / "testotherdir") |
| 216 |
|
| 217 |
def test_albuminfo_move_changes_paths(self): |
| 218 |
self.ai.album = "newAlbumName" |
| 219 |
self.ai.move() |
| 220 |
self.ai.store() |
| 221 |
self.i.load() |
| 222 |
|
| 223 |
assert b"newAlbumName" in self.i.path |
| 224 |
|
| 225 |
def test_albuminfo_move_moves_file(self): |
| 226 |
oldpath = self.i.filepath |
| 227 |
self.ai.album = "newAlbumName" |
| 228 |
self.ai.move() |
| 229 |
self.ai.store() |
| 230 |
self.i.load() |
| 231 |
|
| 232 |
assert not oldpath.exists() |
| 233 |
assert self.i.filepath.exists() |
| 234 |
|
| 235 |
def test_albuminfo_move_copies_file(self): |
| 236 |
oldpath = self.i.filepath |
| 237 |
self.ai.album = "newAlbumName" |
| 238 |
self.ai.move(operation=MoveOperation.COPY) |
| 239 |
self.ai.store() |
| 240 |
self.i.load() |
| 241 |
|
| 242 |
assert oldpath.exists() |
| 243 |
assert self.i.filepath.exists() |
| 244 |
|
| 245 |
@NEEDS_REFLINK |
| 246 |
def test_albuminfo_move_reflinks_file(self): |
| 247 |
oldpath = self.i.filepath |
| 248 |
self.ai.album = "newAlbumName" |
| 249 |
self.ai.move(operation=MoveOperation.REFLINK) |
| 250 |
self.ai.store() |
| 251 |
self.i.load() |
| 252 |
|
| 253 |
assert oldpath.exists() |
| 254 |
assert self.i.filepath.exists() |
| 255 |
|
| 256 |
def test_albuminfo_move_to_custom_dir(self): |
| 257 |
self.ai.move(basedir=self.otherdir) |
| 258 |
self.i.load() |
| 259 |
self.ai.store() |
| 260 |
assert b"testotherdir" in self.i.path |
| 261 |
|
| 262 |
|
| 263 |
class ArtFileTest(BeetsTestCase): |
| 264 |
def setUp(self): |
| 265 |
super().setUp() |
| 266 |
|
| 267 |
# Make library and item. |
| 268 |
self.i = item(self.lib) |
| 269 |
self.i.path = self.i.destination() |
| 270 |
# Make a music file. |
| 271 |
util.mkdirall(self.i.filepath) |
| 272 |
self.i.filepath.touch() |
| 273 |
# Make an album. |
| 274 |
self.ai = self.lib.add_album((self.i,)) |
| 275 |
# Make an art file too. |
| 276 |
art_bytes = self.lib.get_album(self.i).art_destination("something.jpg") |
| 277 |
self.art = Path(os.fsdecode(art_bytes)) |
| 278 |
self.art.touch() |
| 279 |
self.ai.artpath = art_bytes |
| 280 |
self.ai.store() |
| 281 |
# Alternate destination dir. |
| 282 |
self.otherdir = os.fsencode(self.temp_path / "testotherdir") |
| 283 |
|
| 284 |
def test_art_deleted_when_items_deleted(self): |
| 285 |
assert self.art.exists() |
| 286 |
self.ai.remove(True) |
| 287 |
assert not self.art.exists() |
| 288 |
|
| 289 |
def test_art_moves_with_album(self): |
| 290 |
assert self.art.exists() |
| 291 |
oldpath = self.i.path |
| 292 |
self.ai.album = "newAlbum" |
| 293 |
self.ai.move() |
| 294 |
self.i.load() |
| 295 |
|
| 296 |
assert self.i.path != oldpath |
| 297 |
assert not self.art.exists() |
| 298 |
newart = self.lib.get_album(self.i).art_destination(self.art) |
| 299 |
assert Path(os.fsdecode(newart)).exists() |
| 300 |
|
| 301 |
def test_art_moves_with_album_to_custom_dir(self): |
| 302 |
# Move the album to another directory. |
| 303 |
self.ai.move(basedir=self.otherdir) |
| 304 |
self.ai.store() |
| 305 |
self.i.load() |
| 306 |
|
| 307 |
# Art should be in new directory. |
| 308 |
assert not self.art.exists() |
| 309 |
newart = self.lib.get_album(self.i).art_filepath |
| 310 |
assert newart.exists() |
| 311 |
assert "testotherdir" in str(newart) |
| 312 |
|
| 313 |
def test_setart_copies_image(self): |
| 314 |
util.remove(self.art) |
| 315 |
|
| 316 |
newart = self.lib_path / "newart.jpg" |
| 317 |
newart.touch() |
| 318 |
i2 = item() |
| 319 |
i2.path = self.i.path |
| 320 |
i2.artist = "someArtist" |
| 321 |
ai = self.lib.add_album((i2,)) |
| 322 |
i2.move(operation=MoveOperation.COPY) |
| 323 |
|
| 324 |
assert ai.artpath is None |
| 325 |
ai.set_art(newart) |
| 326 |
assert ai.art_filepath.exists() |
| 327 |
|
| 328 |
def test_setart_to_existing_art_works(self): |
| 329 |
util.remove(self.art) |
| 330 |
|
| 331 |
# Original art. |
| 332 |
newart = self.lib_path / "newart.jpg" |
| 333 |
newart.touch() |
| 334 |
i2 = item() |
| 335 |
i2.path = self.i.path |
| 336 |
i2.artist = "someArtist" |
| 337 |
ai = self.lib.add_album((i2,)) |
| 338 |
i2.move(operation=MoveOperation.COPY) |
| 339 |
ai.set_art(newart) |
| 340 |
|
| 341 |
# Set the art again. |
| 342 |
ai.set_art(ai.artpath) |
| 343 |
assert ai.art_filepath.exists() |
| 344 |
|
| 345 |
def test_setart_to_existing_but_unset_art_works(self): |
| 346 |
newart = self.lib_path / "newart.jpg" |
| 347 |
newart.touch() |
| 348 |
i2 = item() |
| 349 |
i2.path = self.i.path |
| 350 |
i2.artist = "someArtist" |
| 351 |
ai = self.lib.add_album((i2,)) |
| 352 |
i2.move(operation=MoveOperation.COPY) |
| 353 |
|
| 354 |
# Copy the art to the destination. |
| 355 |
artdest = ai.art_destination(newart) |
| 356 |
shutil.copy(newart, syspath(artdest)) |
| 357 |
|
| 358 |
# Set the art again. |
| 359 |
ai.set_art(artdest) |
| 360 |
assert ai.art_filepath.exists() |
| 361 |
|
| 362 |
def test_setart_to_conflicting_file_replaces_it(self): |
| 363 |
newart = self.lib_path / "newart.jpg" |
| 364 |
newart.touch() |
| 365 |
i2 = item() |
| 366 |
i2.path = self.i.path |
| 367 |
i2.artist = "someArtist" |
| 368 |
ai = self.lib.add_album((i2,)) |
| 369 |
i2.move(operation=MoveOperation.COPY) |
| 370 |
|
| 371 |
# Make a file at the destination. |
| 372 |
artdest = Path(os.fsdecode(ai.art_destination(newart))) |
| 373 |
artdest.touch() |
| 374 |
|
| 375 |
# Set the art - should replace the existing file, not create a suffixed |
| 376 |
# duplicate like cover.2.jpg. |
| 377 |
ai.set_art(newart) |
| 378 |
assert artdest == ai.art_filepath |
| 379 |
|
| 380 |
def test_setart_replaces_old_art_at_different_path(self): |
| 381 |
newart = self.lib_path / "newart.png" |
| 382 |
newart.touch() |
| 383 |
i2 = item() |
| 384 |
i2.path = self.i.path |
| 385 |
i2.artist = "someArtist" |
| 386 |
ai = self.lib.add_album((i2,)) |
| 387 |
i2.move(operation=MoveOperation.COPY) |
| 388 |
|
| 389 |
# Set initial art. |
| 390 |
ai.set_art(newart) |
| 391 |
old_artpath = ai.art_filepath |
| 392 |
assert old_artpath.exists() |
| 393 |
|
| 394 |
# Set new art with a different extension. |
| 395 |
another_art = self.lib_path / "another.jpg" |
| 396 |
another_art.touch() |
| 397 |
ai.set_art(another_art) |
| 398 |
|
| 399 |
# Old art should be removed. |
| 400 |
assert not old_artpath.exists() |
| 401 |
assert ai.art_filepath.exists() |
| 402 |
|
| 403 |
def test_setart_sets_permissions(self): |
| 404 |
util.remove(self.art) |
| 405 |
|
| 406 |
newart = self.lib_path / "newart.jpg" |
| 407 |
newart.touch() |
| 408 |
newart.chmod(0o400) # read-only |
| 409 |
|
| 410 |
try: |
| 411 |
i2 = item() |
| 412 |
i2.path = self.i.path |
| 413 |
i2.artist = "someArtist" |
| 414 |
ai = self.lib.add_album((i2,)) |
| 415 |
i2.move(operation=MoveOperation.COPY) |
| 416 |
ai.set_art(newart) |
| 417 |
|
| 418 |
mode = stat.S_IMODE(ai.art_filepath.stat().st_mode) |
| 419 |
assert mode & stat.S_IRGRP |
| 420 |
assert os.access(ai.art_filepath, os.W_OK) |
| 421 |
|
| 422 |
finally: |
| 423 |
# Make everything writable so it can be cleaned up. |
| 424 |
newart.chmod(0o777) |
| 425 |
ai.art_filepath.chmod(0o777) |
| 426 |
|
| 427 |
def test_move_last_file_moves_albumart(self): |
| 428 |
oldartpath = self.lib.albums()[0].art_filepath |
| 429 |
assert oldartpath.exists() |
| 430 |
|
| 431 |
self.ai.album = "different_album" |
| 432 |
self.ai.store() |
| 433 |
self.ai.items()[0].move() |
| 434 |
|
| 435 |
artpath = self.lib.albums()[0].art_filepath |
| 436 |
assert "different_album" in str(artpath) |
| 437 |
assert artpath.exists() |
| 438 |
assert not oldartpath.exists() |
| 439 |
|
| 440 |
def test_move_not_last_file_does_not_move_albumart(self): |
| 441 |
i2 = item() |
| 442 |
i2.albumid = self.ai.id |
| 443 |
self.lib.add(i2) |
| 444 |
|
| 445 |
oldartpath = self.lib.albums()[0].art_filepath |
| 446 |
assert oldartpath.exists() |
| 447 |
|
| 448 |
self.i.album = "different_album" |
| 449 |
self.i.album_id = None # detach from album |
| 450 |
self.i.move() |
| 451 |
|
| 452 |
artpath = self.lib.albums()[0].art_filepath |
| 453 |
assert "different_album" not in str(artpath) |
| 454 |
assert artpath == oldartpath |
| 455 |
assert oldartpath.exists() |
| 456 |
|
| 457 |
|
| 458 |
class RemoveTest(BeetsTestCase): |
| 459 |
def setUp(self): |
| 460 |
super().setUp() |
| 461 |
|
| 462 |
# Make library and item. |
| 463 |
self.i = item(self.lib) |
| 464 |
self.i.path = self.i.destination() |
| 465 |
# Make a music file. |
| 466 |
util.mkdirall(self.i.filepath) |
| 467 |
self.i.filepath.touch() |
| 468 |
# Make an album with the item. |
| 469 |
self.ai = self.lib.add_album((self.i,)) |
| 470 |
|
| 471 |
def test_removing_last_item_prunes_empty_dir(self): |
| 472 |
assert self.i.filepath.parent.exists() |
| 473 |
self.i.remove(True) |
| 474 |
assert not self.i.filepath.parent.exists() |
| 475 |
|
| 476 |
def test_removing_last_item_preserves_nonempty_dir(self): |
| 477 |
(self.i.filepath.parent / "dummy.txt").touch() |
| 478 |
self.i.remove(True) |
| 479 |
assert self.i.filepath.parent.exists() |
| 480 |
|
| 481 |
def test_removing_last_item_prunes_dir_with_blacklisted_file(self): |
| 482 |
(self.i.filepath.parent / ".DS_Store").touch() |
| 483 |
self.i.remove(True) |
| 484 |
assert not self.i.filepath.parent.exists() |
| 485 |
|
| 486 |
def test_removing_without_delete_leaves_file(self): |
| 487 |
self.i.remove(False) |
| 488 |
assert self.i.filepath.parent.exists() |
| 489 |
|
| 490 |
def test_removing_last_item_preserves_library_dir(self): |
| 491 |
self.i.remove(True) |
| 492 |
assert self.lib_path.exists() |
| 493 |
|
| 494 |
def test_removing_last_item_in_album_with_albumart_prunes_dir(self): |
| 495 |
artfile = self.temp_path / "testart.jpg" |
| 496 |
artfile.touch() |
| 497 |
self.ai.set_art(artfile) |
| 498 |
self.ai.store() |
| 499 |
|
| 500 |
self.i.remove(True) |
| 501 |
assert not self.i.filepath.parent.exists() |