| 1 |
import datetime |
| 2 |
from pathlib import Path |
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from beets.library import Album, Item |
| 8 |
from beets.test.helper import PluginTestCase |
| 9 |
from beets.util import FilesystemError |
| 10 |
from beetsplug.importfeeds import ImportFeedsPlugin |
| 11 |
|
| 12 |
|
| 13 |
class ImportFeedsTest(PluginTestCase): |
| 14 |
plugin = "importfeeds" |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
super().setUp() |
| 18 |
self.importfeeds = ImportFeedsPlugin() |
| 19 |
self.feeds_dir = self.temp_path / "importfeeds" |
| 20 |
self.config["importfeeds"]["dir"] = str(self.feeds_dir) |
| 21 |
|
| 22 |
def test_multi_format_album_playlist(self): |
| 23 |
self.config["importfeeds"]["formats"] = "m3u_multi" |
| 24 |
album = Album(album="album/name", id=1) |
| 25 |
item_path = Path("path") / "to" / "item" |
| 26 |
item = Item(title="song", album_id=1, path=item_path) |
| 27 |
self.lib.add(album) |
| 28 |
self.lib.add(item) |
| 29 |
|
| 30 |
self.importfeeds.album_imported(self.lib, album) |
| 31 |
playlist_path = self.feeds_dir / next(self.feeds_dir.iterdir()) |
| 32 |
assert str(playlist_path).endswith("album_name.m3u") |
| 33 |
assert str(item_path) in playlist_path.read_text() |
| 34 |
|
| 35 |
def test_playlist_in_subdir(self): |
| 36 |
self.config["importfeeds"]["formats"] = "m3u" |
| 37 |
self.config["importfeeds"]["m3u_name"] = str( |
| 38 |
Path("subdir") / "imported.m3u" |
| 39 |
) |
| 40 |
album = Album(album="album/name", id=1) |
| 41 |
item_path = Path("path") / "to" / "item" |
| 42 |
item = Item(title="song", album_id=1, path=item_path) |
| 43 |
self.lib.add(album) |
| 44 |
self.lib.add(item) |
| 45 |
|
| 46 |
self.importfeeds.album_imported(self.lib, album) |
| 47 |
playlist = self.feeds_dir / self.config["importfeeds"]["m3u_name"].get() |
| 48 |
playlist_subdir = playlist.parent |
| 49 |
assert playlist_subdir.is_dir() |
| 50 |
assert playlist.is_file() |
| 51 |
|
| 52 |
def test_playlist_per_session(self): |
| 53 |
self.config["importfeeds"]["formats"] = "m3u_session" |
| 54 |
self.config["importfeeds"]["m3u_name"] = "imports.m3u" |
| 55 |
album = Album(album="album/name", id=1) |
| 56 |
item_path = Path("path") / "to" / "item" |
| 57 |
item = Item(title="song", album_id=1, path=item_path) |
| 58 |
self.lib.add(album) |
| 59 |
self.lib.add(item) |
| 60 |
|
| 61 |
self.importfeeds.import_begin(self) |
| 62 |
self.importfeeds.album_imported(self.lib, album) |
| 63 |
date = datetime.datetime.now().strftime("%Y%m%d_%Hh%M") |
| 64 |
playlist = self.feeds_dir / f"imports_{date}.m3u" |
| 65 |
assert playlist.is_file() |
| 66 |
assert str(item_path) in playlist.read_text() |
| 67 |
|
| 68 |
def test_link_failure_warns_and_continues(self): |
| 69 |
"""A symlink that can't be created is logged as a warning and |
| 70 |
skipped; remaining items are still processed (#840).""" |
| 71 |
self.config["importfeeds"]["formats"] = "link" |
| 72 |
album = Album(album="album/name", id=1) |
| 73 |
item1 = Item(title="one", album_id=1, path=Path("path") / "one") |
| 74 |
item2 = Item(title="two", album_id=1, path=Path("path") / "two") |
| 75 |
self.lib.add(album) |
| 76 |
self.lib.add(item1) |
| 77 |
self.lib.add(item2) |
| 78 |
|
| 79 |
with ( |
| 80 |
mock.patch( |
| 81 |
"beetsplug.importfeeds.link", |
| 82 |
side_effect=[FilesystemError("x", "link", (b"a", b"b")), None], |
| 83 |
) as mock_link, |
| 84 |
mock.patch.object(self.importfeeds, "_log") as log, |
| 85 |
): |
| 86 |
self.importfeeds.album_imported(self.lib, album) |
| 87 |
|
| 88 |
log.warning.assert_called_once() |
| 89 |
assert mock_link.call_count == 2 |
| 90 |
|
| 91 |
def test_non_filesystem_error_is_not_swallowed(self): |
| 92 |
"""Only FilesystemError is caught; other errors still propagate.""" |
| 93 |
self.config["importfeeds"]["formats"] = "link" |
| 94 |
album = Album(album="album/name", id=1) |
| 95 |
item_path = Path("path") / "to" / "item" |
| 96 |
item = Item(title="song", album_id=1, path=item_path) |
| 97 |
self.lib.add(album) |
| 98 |
self.lib.add(item) |
| 99 |
|
| 100 |
with ( |
| 101 |
mock.patch( |
| 102 |
"beetsplug.importfeeds.link", |
| 103 |
side_effect=ValueError("unexpected"), |
| 104 |
), |
| 105 |
pytest.raises(ValueError, match="unexpected"), |
| 106 |
): |
| 107 |
self.importfeeds.album_imported(self.lib, album) |
| 108 |
|
| 109 |
def test_link_creates_symlink(self): |
| 110 |
"""The happy path still creates the symlink when it succeeds.""" |
| 111 |
self.config["importfeeds"]["formats"] = "link" |
| 112 |
self.feeds_dir.mkdir(parents=True, exist_ok=True) |
| 113 |
album = Album(album="album/name", id=1) |
| 114 |
item_path = Path("path") / "to" / "item" |
| 115 |
item = Item(title="song", album_id=1, path=item_path) |
| 116 |
self.lib.add(album) |
| 117 |
self.lib.add(item) |
| 118 |
|
| 119 |
self.importfeeds.album_imported(self.lib, album) |
| 120 |
|
| 121 |
linked = self.feeds_dir / item.filepath.name |
| 122 |
assert linked.is_symlink() |