| 1 |
"""Tests for the play plugin""" |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
from unittest.mock import ANY, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from beets.test.helper import ( |
| 11 |
AutotagImportTestCase, |
| 12 |
BeetsTestCase, |
| 13 |
CleanupModulesMixin, |
| 14 |
IOMixin, |
| 15 |
PluginMixin, |
| 16 |
TerminalImportMixin, |
| 17 |
) |
| 18 |
from beets.ui import UserError |
| 19 |
from beets.util import open_anything |
| 20 |
from beetsplug.play import PlayPlugin |
| 21 |
|
| 22 |
|
| 23 |
class PlayPluginMixin(CleanupModulesMixin, PluginMixin): |
| 24 |
modules = (PlayPlugin.__module__,) |
| 25 |
plugin = "play" |
| 26 |
|
| 27 |
def setUp(self): |
| 28 |
super().setUp() |
| 29 |
self.config["play"]["command"] = "echo" |
| 30 |
|
| 31 |
|
| 32 |
@patch("beetsplug.play.util.interactive_open") |
| 33 |
class PlayPluginTest(IOMixin, PlayPluginMixin, BeetsTestCase): |
| 34 |
def setUp(self): |
| 35 |
super().setUp() |
| 36 |
self.item = self.add_item(album="a nice älbum", title="aNiceTitle") |
| 37 |
self.lib.add_album([self.item]) |
| 38 |
|
| 39 |
def run_and_assert( |
| 40 |
self, |
| 41 |
open_mock, |
| 42 |
args=("title:aNiceTitle",), |
| 43 |
expected_cmd="echo", |
| 44 |
expected_playlist=None, |
| 45 |
): |
| 46 |
self.run_command("play", *args) |
| 47 |
|
| 48 |
open_mock.assert_called_once_with(ANY, expected_cmd) |
| 49 |
expected_playlist = expected_playlist or self.item.path.decode("utf-8") |
| 50 |
exp_playlist = f"{expected_playlist}\n" |
| 51 |
with open(open_mock.call_args[0][0][0], "rb") as playlist: |
| 52 |
assert exp_playlist == playlist.read().decode("utf-8") |
| 53 |
|
| 54 |
def test_basic(self, open_mock): |
| 55 |
self.run_and_assert(open_mock) |
| 56 |
|
| 57 |
def test_album_option(self, open_mock): |
| 58 |
self.run_and_assert(open_mock, ["-a", "nice"]) |
| 59 |
|
| 60 |
def test_args_option(self, open_mock): |
| 61 |
self.run_and_assert( |
| 62 |
open_mock, ["-A", "foo", "title:aNiceTitle"], "echo foo" |
| 63 |
) |
| 64 |
|
| 65 |
def test_args_option_in_middle(self, open_mock): |
| 66 |
self.config["play"]["command"] = "echo $args other" |
| 67 |
|
| 68 |
self.run_and_assert( |
| 69 |
open_mock, ["-A", "foo", "title:aNiceTitle"], "echo foo other" |
| 70 |
) |
| 71 |
|
| 72 |
def test_unset_args_option_in_middle(self, open_mock): |
| 73 |
self.config["play"]["command"] = "echo $args other" |
| 74 |
|
| 75 |
self.run_and_assert(open_mock, ["title:aNiceTitle"], "echo other") |
| 76 |
|
| 77 |
# FIXME: fails on windows |
| 78 |
@unittest.skipIf(sys.platform == "win32", "win32") |
| 79 |
def test_relative_to(self, open_mock): |
| 80 |
self.config["play"]["command"] = "echo" |
| 81 |
self.config["play"]["relative_to"] = "/something" |
| 82 |
|
| 83 |
path = os.path.relpath(self.item.path, b"/something") |
| 84 |
playlist = path.decode("utf-8") |
| 85 |
self.run_and_assert( |
| 86 |
open_mock, expected_cmd="echo", expected_playlist=playlist |
| 87 |
) |
| 88 |
|
| 89 |
def test_use_folders(self, open_mock): |
| 90 |
self.config["play"]["command"] = None |
| 91 |
self.config["play"]["use_folders"] = True |
| 92 |
self.run_command("play", "-a", "nice") |
| 93 |
|
| 94 |
open_mock.assert_called_once_with(ANY, open_anything()) |
| 95 |
with open(open_mock.call_args[0][0][0], "rb") as f: |
| 96 |
playlist = f.read().decode("utf-8") |
| 97 |
assert f"{self.item.filepath.parent}\n" == playlist |
| 98 |
|
| 99 |
def test_raw(self, open_mock): |
| 100 |
self.config["play"]["raw"] = True |
| 101 |
|
| 102 |
self.run_command("play", "nice") |
| 103 |
|
| 104 |
open_mock.assert_called_once_with([self.item.path], "echo") |
| 105 |
|
| 106 |
def test_pls_marker(self, open_mock): |
| 107 |
self.config["play"]["command"] = ( |
| 108 |
"echo --some params --playlist=$playlist --some-more params" |
| 109 |
) |
| 110 |
|
| 111 |
self.run_command("play", "nice") |
| 112 |
|
| 113 |
open_mock.assert_called_once |
| 114 |
|
| 115 |
commandstr = open_mock.call_args_list[0][0][1] |
| 116 |
assert commandstr.startswith("echo --some params --playlist=") |
| 117 |
assert commandstr.endswith(" --some-more params") |
| 118 |
|
| 119 |
def test_not_found(self, open_mock): |
| 120 |
self.run_command("play", "not found") |
| 121 |
|
| 122 |
open_mock.assert_not_called() |
| 123 |
|
| 124 |
def test_warning_threshold(self, open_mock): |
| 125 |
self.config["play"]["warning_threshold"] = 1 |
| 126 |
self.add_item(title="another NiceTitle") |
| 127 |
|
| 128 |
self.io.addinput("a") |
| 129 |
self.run_command("play", "nice") |
| 130 |
|
| 131 |
open_mock.assert_not_called() |
| 132 |
|
| 133 |
def test_skip_warning_threshold_bypass(self, open_mock): |
| 134 |
self.config["play"]["warning_threshold"] = 1 |
| 135 |
self.other_item = self.add_item(title="another NiceTitle") |
| 136 |
|
| 137 |
expected_playlist = f"{self.item.filepath}\n{self.other_item.filepath}" |
| 138 |
|
| 139 |
self.io.addinput("a") |
| 140 |
self.run_and_assert( |
| 141 |
open_mock, ["-y", "NiceTitle"], expected_playlist=expected_playlist |
| 142 |
) |
| 143 |
|
| 144 |
def _playlist_lines(self, open_mock): |
| 145 |
"""Read the playlist file passed to interactive_open and return its lines.""" |
| 146 |
# interactive_open is called as: interactive_open([playlist_path], command) |
| 147 |
playlist_path = open_mock.call_args[0][0][0] |
| 148 |
with open(playlist_path, "rb") as playlist: |
| 149 |
return playlist.read().decode("utf-8").splitlines() |
| 150 |
|
| 151 |
def _add_many_ordered_items(self, *, count, album): |
| 152 |
items = [] |
| 153 |
for track in range(1, count + 1): |
| 154 |
items.append( |
| 155 |
self.add_item( |
| 156 |
album=album, |
| 157 |
artist="randomize artist", |
| 158 |
title=f"randomize {track:03d}", |
| 159 |
track=track, |
| 160 |
) |
| 161 |
) |
| 162 |
return items |
| 163 |
|
| 164 |
def test_randomize(self, open_mock): |
| 165 |
album = "randomize_test" |
| 166 |
items = self._add_many_ordered_items(count=100, album=album) |
| 167 |
baseline = [str(item.filepath) for item in items] |
| 168 |
|
| 169 |
self.run_command("play", "-R", f"album:{album}") |
| 170 |
lines = self._playlist_lines(open_mock) |
| 171 |
assert sorted(lines) == sorted(baseline), ( |
| 172 |
"playlist items are not the same" |
| 173 |
) |
| 174 |
assert lines != baseline, "playlist order hasn't changed" |
| 175 |
|
| 176 |
def test_command_failed(self, open_mock): |
| 177 |
open_mock.side_effect = OSError("some reason") |
| 178 |
|
| 179 |
with pytest.raises(UserError): |
| 180 |
self.run_command("play", "title:aNiceTitle") |
| 181 |
|
| 182 |
|
| 183 |
class PlayOnImportTest( |
| 184 |
TerminalImportMixin, PlayPluginMixin, AutotagImportTestCase |
| 185 |
): |
| 186 |
def setUp(self): |
| 187 |
super().setUp() |
| 188 |
self.prepare_album_for_import(1) |
| 189 |
self.importer = self.setup_importer() |
| 190 |
|
| 191 |
def test_play_on_import(self): |
| 192 |
self.importer.add_choice("y") |
| 193 |
self.importer.add_choice("1") |
| 194 |
|
| 195 |
playlist_path = self.temp_path / "beetsplug_play" / "playlist.m3u" |
| 196 |
playlist_path.parent.mkdir(parents=True) |
| 197 |
playlist_path.touch() |
| 198 |
playlist_path_bytes = os.fsencode(playlist_path) |
| 199 |
|
| 200 |
with ( |
| 201 |
patch( |
| 202 |
"beetsplug.play.get_temp_filename", |
| 203 |
side_effect=lambda *_, **__: playlist_path_bytes, |
| 204 |
), |
| 205 |
patch("beetsplug.play.subprocess.call") as subprocess_call_mock, |
| 206 |
): |
| 207 |
self.importer.run() |
| 208 |
|
| 209 |
# note that the call has mixed types (str and bytes) |
| 210 |
subprocess_call_mock.assert_called_once_with( |
| 211 |
["echo", playlist_path_bytes] |
| 212 |
) |