| 1 |
import shutil |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
import pytest |
| 5 |
from mediafile import MediaFile |
| 6 |
|
| 7 |
from beets.exceptions import UserError |
| 8 |
from beets.test import _common |
| 9 |
from beets.test.helper import PluginTestHelper |
| 10 |
from beetsplug.replace import ReplacePlugin |
| 11 |
|
| 12 |
replace = ReplacePlugin() |
| 13 |
|
| 14 |
|
| 15 |
class TestReplaceCommand(PluginTestHelper): |
| 16 |
plugin = "replace" |
| 17 |
|
| 18 |
def test_command_callback_accepts_cli_arguments(self): |
| 19 |
with pytest.raises(UserError, match="Usage: beet replace"): |
| 20 |
self.run_command("replace") |
| 21 |
|
| 22 |
|
| 23 |
class TestReplace: |
| 24 |
@pytest.fixture(autouse=True) |
| 25 |
def _fake_dir(self, tmp_path): |
| 26 |
self.fake_dir = tmp_path |
| 27 |
|
| 28 |
@pytest.fixture(autouse=True) |
| 29 |
def _fake_file(self, tmp_path): |
| 30 |
self.fake_file = tmp_path |
| 31 |
|
| 32 |
def test_path_is_dir(self): |
| 33 |
fake_directory = self.fake_dir / "fakeDir" |
| 34 |
fake_directory.mkdir() |
| 35 |
with pytest.raises(UserError): |
| 36 |
replace.file_check(fake_directory) |
| 37 |
|
| 38 |
def test_path_is_unsupported_file(self): |
| 39 |
fake_file = self.fake_file / "fakefile.txt" |
| 40 |
fake_file.write_text("test", encoding="utf-8") |
| 41 |
with pytest.raises(UserError): |
| 42 |
replace.file_check(fake_file) |
| 43 |
|
| 44 |
def test_path_is_supported_file(self): |
| 45 |
dest = self.fake_file / "full.mp3" |
| 46 |
src = _common.RSRC / "full.mp3" |
| 47 |
shutil.copyfile(src, dest) |
| 48 |
|
| 49 |
mediafile = MediaFile(dest) |
| 50 |
mediafile.albumartist = "AAA" |
| 51 |
mediafile.disctitle = "DDD" |
| 52 |
mediafile.genres = ["a", "b", "c"] |
| 53 |
mediafile.composer = None |
| 54 |
mediafile.save() |
| 55 |
|
| 56 |
replace.file_check(Path(str(dest))) |
| 57 |
|
| 58 |
def test_select_song_valid_choice(self, monkeypatch, capfd): |
| 59 |
songs = ["Song A", "Song B", "Song C"] |
| 60 |
monkeypatch.setattr("builtins.input", lambda _: "2") |
| 61 |
|
| 62 |
selected_song = replace.select_song(songs) |
| 63 |
|
| 64 |
captured = capfd.readouterr() |
| 65 |
|
| 66 |
assert "1. Song A" in captured.out |
| 67 |
assert "2. Song B" in captured.out |
| 68 |
assert "3. Song C" in captured.out |
| 69 |
assert selected_song == "Song B" |
| 70 |
|
| 71 |
def test_select_song_cancel(self, monkeypatch): |
| 72 |
songs = ["Song A", "Song B", "Song C"] |
| 73 |
monkeypatch.setattr("builtins.input", lambda _: "0") |
| 74 |
|
| 75 |
selected_song = replace.select_song(songs) |
| 76 |
|
| 77 |
assert selected_song is None |
| 78 |
|
| 79 |
def test_select_song_invalid_then_valid(self, monkeypatch, capfd): |
| 80 |
songs = ["Song A", "Song B", "Song C"] |
| 81 |
inputs = iter(["invalid", "4", "3"]) |
| 82 |
monkeypatch.setattr("builtins.input", lambda _: next(inputs)) |
| 83 |
|
| 84 |
selected_song = replace.select_song(songs) |
| 85 |
|
| 86 |
captured = capfd.readouterr() |
| 87 |
|
| 88 |
assert "Invalid input. Please type in a number." in captured.out |
| 89 |
assert ( |
| 90 |
"Invalid choice. Please enter a number between 1 and 3." |
| 91 |
in captured.out |
| 92 |
) |
| 93 |
assert selected_song == "Song C" |
| 94 |
|
| 95 |
def test_confirm_replacement_file_not_exist(self): |
| 96 |
class Song: |
| 97 |
path = b"test123321.txt" |
| 98 |
|
| 99 |
song = Song() |
| 100 |
|
| 101 |
with pytest.raises(UserError): |
| 102 |
replace.confirm_replacement("test", song) |
| 103 |
|
| 104 |
def test_confirm_replacement_yes(self, monkeypatch): |
| 105 |
src = _common.RSRC / "full.mp3" |
| 106 |
monkeypatch.setattr("builtins.input", lambda _: "YES ") |
| 107 |
|
| 108 |
class Song: |
| 109 |
path = str(src).encode() |
| 110 |
|
| 111 |
song = Song() |
| 112 |
|
| 113 |
assert replace.confirm_replacement("test", song) is True |
| 114 |
|
| 115 |
def test_confirm_replacement_no(self, monkeypatch): |
| 116 |
src = _common.RSRC / "full.mp3" |
| 117 |
monkeypatch.setattr("builtins.input", lambda _: "test123") |
| 118 |
|
| 119 |
class Song: |
| 120 |
path = str(src).encode() |
| 121 |
|
| 122 |
song = Song() |
| 123 |
|
| 124 |
assert replace.confirm_replacement("test", song) is False |