| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import fnmatch |
| 4 |
import os |
| 5 |
import shlex |
| 6 |
import sys |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
import pytest |
| 10 |
from mediafile import MediaFile |
| 11 |
|
| 12 |
from beets import util |
| 13 |
from beets.library import Item |
| 14 |
from beets.test import _common |
| 15 |
from beets.test.helper import ( |
| 16 |
AsIsImporterMixin, |
| 17 |
ImportHelper, |
| 18 |
IOMixin, |
| 19 |
PluginTestHelper, |
| 20 |
) |
| 21 |
from beetsplug import convert |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from pathlib import Path |
| 25 |
|
| 26 |
_p = pytest.param |
| 27 |
|
| 28 |
|
| 29 |
class ConvertPluginHelper(IOMixin, PluginTestHelper): |
| 30 |
db_on_disk = True |
| 31 |
plugin = "convert" |
| 32 |
|
| 33 |
def setup_beets(self): |
| 34 |
super().setup_beets() |
| 35 |
self.convert_dest = self.temp_path / "convert_dest" |
| 36 |
self.config["convert"] = {"dest": str(self.convert_dest)} |
| 37 |
|
| 38 |
def tagged_copy_cmd(self, tag): |
| 39 |
"""Return a conversion command that copies files and appends |
| 40 |
`tag` to the copy. |
| 41 |
""" |
| 42 |
# A Python script that copies the file and appends a tag. |
| 43 |
stub = str(_common.RSRC / "convert_stub.py") |
| 44 |
return f"{shlex.quote(sys.executable)} {shlex.quote(stub)} $source $dest {tag}" |
| 45 |
|
| 46 |
def file_endswith(self, path: Path, tag: str): |
| 47 |
"""Check the path is a file and if its content ends with `tag`.""" |
| 48 |
assert path.exists() |
| 49 |
assert path.is_file() |
| 50 |
return path.read_bytes().endswith(tag.encode("utf-8")) |
| 51 |
|
| 52 |
|
| 53 |
class TestImportConvert(AsIsImporterMixin, ImportHelper, ConvertPluginHelper): |
| 54 |
def setup_beets(self): |
| 55 |
super().setup_beets() |
| 56 |
self.config["convert"] = { |
| 57 |
"command": self.tagged_copy_cmd("convert"), |
| 58 |
# Enforce running convert |
| 59 |
"max_bitrate": 1, |
| 60 |
"auto": True, |
| 61 |
"quiet": False, |
| 62 |
} |
| 63 |
|
| 64 |
def test_import_converted(self): |
| 65 |
self.run_asis_importer() |
| 66 |
item = self.lib.items().get() |
| 67 |
assert self.file_endswith(item.filepath, "convert") |
| 68 |
|
| 69 |
# FIXME: fails on windows |
| 70 |
@pytest.mark.skipif(sys.platform == "win32", reason="win32") |
| 71 |
def test_import_original_on_convert_error(self): |
| 72 |
# `false` exits with non-zero code |
| 73 |
self.config["convert"]["command"] = "false" |
| 74 |
self.run_asis_importer() |
| 75 |
|
| 76 |
item = self.lib.items().get() |
| 77 |
assert item is not None |
| 78 |
assert item.filepath.is_file() |
| 79 |
|
| 80 |
def test_delete_originals(self): |
| 81 |
self.config["convert"]["delete_originals"] = True |
| 82 |
self.run_asis_importer() |
| 83 |
for path in self.importer.paths: |
| 84 |
for root, dirnames, filenames in os.walk(path): |
| 85 |
assert len(fnmatch.filter(filenames, "*.mp3")) == 0, ( |
| 86 |
f"Non-empty import directory {util.displayable_path(path)}" |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
class ConvertCommand: |
| 91 |
"""A mixin providing a utility method to run the `convert`command |
| 92 |
in tests. |
| 93 |
""" |
| 94 |
|
| 95 |
def run_convert_path(self, item, *args): |
| 96 |
"""Run the `convert` command on a given path.""" |
| 97 |
return self.run_command("convert", *args, f"path:{item.filepath}") |
| 98 |
|
| 99 |
def run_convert(self, *args): |
| 100 |
"""Run the `convert` command on `self.item`.""" |
| 101 |
return self.run_convert_path(self.item, *args) |
| 102 |
|
| 103 |
|
| 104 |
class TestConvertCli(ConvertPluginHelper, ConvertCommand): |
| 105 |
def setup_beets(self): |
| 106 |
super().setup_beets() |
| 107 |
self.album = self.add_album_fixture(ext="ogg") |
| 108 |
self.item = self.album.items()[0] |
| 109 |
|
| 110 |
self.converted_mp3 = self.convert_dest / "converted.mp3" |
| 111 |
self.config["convert"] = { |
| 112 |
"paths": {"default": "converted"}, |
| 113 |
"format": "mp3", |
| 114 |
"formats": { |
| 115 |
"mp3": self.tagged_copy_cmd("mp3"), |
| 116 |
"ogg": self.tagged_copy_cmd("ogg"), |
| 117 |
"opus": { |
| 118 |
"command": self.tagged_copy_cmd("opus"), |
| 119 |
"extension": "ops", |
| 120 |
}, |
| 121 |
}, |
| 122 |
} |
| 123 |
|
| 124 |
def test_convert(self): |
| 125 |
self.io.addinput("y") |
| 126 |
self.run_convert() |
| 127 |
assert self.file_endswith(self.converted_mp3, "mp3") |
| 128 |
|
| 129 |
def test_convert_with_auto_confirmation(self): |
| 130 |
self.run_convert("--yes") |
| 131 |
assert self.file_endswith(self.converted_mp3, "mp3") |
| 132 |
|
| 133 |
def test_reject_confirmation(self): |
| 134 |
self.io.addinput("n") |
| 135 |
self.run_convert() |
| 136 |
assert not self.converted_mp3.exists() |
| 137 |
|
| 138 |
def test_convert_keep_new(self): |
| 139 |
assert self.item.filepath.suffix == ".ogg" |
| 140 |
|
| 141 |
self.io.addinput("y") |
| 142 |
self.run_convert("--keep-new") |
| 143 |
|
| 144 |
self.item.load() |
| 145 |
assert self.item.filepath.suffix == ".mp3" |
| 146 |
|
| 147 |
def test_format_option(self): |
| 148 |
self.io.addinput("y") |
| 149 |
self.run_convert("--format", "opus") |
| 150 |
assert self.file_endswith(self.convert_dest / "converted.ops", "opus") |
| 151 |
|
| 152 |
def test_embed_album_art(self): |
| 153 |
self.config["convert"]["embed"] = True |
| 154 |
image_path = _common.RSRC / "image-2x3.jpg" |
| 155 |
self.album.artpath = image_path |
| 156 |
self.album.store() |
| 157 |
image_data = image_path.read_bytes() |
| 158 |
|
| 159 |
self.io.addinput("y") |
| 160 |
self.run_convert() |
| 161 |
mediafile = MediaFile(self.converted_mp3) |
| 162 |
assert mediafile.images[0].data == image_data |
| 163 |
|
| 164 |
def test_copy_album_art_missing_source(self, caplog): |
| 165 |
# A missing/stale art source should be skipped instead of crashing |
| 166 |
# the conversion (see #4692). |
| 167 |
self.config["convert"]["copy_album_art"] = True |
| 168 |
self.album.artpath = _common.RSRC / "nonexistent.jpg" |
| 169 |
self.album.store() |
| 170 |
|
| 171 |
with caplog.at_level("INFO", logger="beets.convert"): |
| 172 |
self.run_command("convert", "-a", "--yes") |
| 173 |
|
| 174 |
assert any( |
| 175 |
"source file not found" in message for message in caplog.messages |
| 176 |
) |
| 177 |
assert self.file_endswith(self.converted_mp3, "mp3") |
| 178 |
|
| 179 |
def test_skip_existing(self): |
| 180 |
converted = self.converted_mp3 |
| 181 |
util.mkdirall(converted) |
| 182 |
converted.write_text("XXX") |
| 183 |
self.run_convert("--yes") |
| 184 |
assert converted.read_text() == "XXX" |
| 185 |
|
| 186 |
def test_pretend(self): |
| 187 |
self.run_convert("--pretend") |
| 188 |
assert not self.converted_mp3.exists() |
| 189 |
|
| 190 |
def test_empty_query(self, caplog): |
| 191 |
with caplog.at_level("INFO", logger="beets.convert"): |
| 192 |
self.run_convert("An impossible query") |
| 193 |
assert caplog.messages[0] == "Empty query result." |
| 194 |
|
| 195 |
@pytest.mark.parametrize( |
| 196 |
"max_bitrate,convert_format,args,should_transcode", |
| 197 |
[ |
| 198 |
_p(5000, "mp3", (), True, id="different-format-high-bitrate"), |
| 199 |
_p(5, "mp3", (), True, id="different-format-low-bitrate"), |
| 200 |
_p(None, "mp3", (), True, id="different-format-no-max-bitrate"), |
| 201 |
_p(5000, "ogg", (), False, id="same-format-high-bitrate"), |
| 202 |
_p(5000, "ogg", ("--force",), True, id="same-format-force"), |
| 203 |
_p(5, "ogg", (), True, id="same-format-low-bitrate"), |
| 204 |
_p(None, "ogg", (), False, id="same-format-no-max-bitrate"), |
| 205 |
], |
| 206 |
) |
| 207 |
def test_transcode_selection( |
| 208 |
self, max_bitrate, convert_format, args, should_transcode |
| 209 |
): |
| 210 |
if max_bitrate is not None: |
| 211 |
self.config["convert"]["max_bitrate"] = max_bitrate |
| 212 |
self.config["convert"]["format"] = convert_format |
| 213 |
|
| 214 |
self.io.addinput("y") |
| 215 |
self.run_convert(*args) |
| 216 |
|
| 217 |
converted_path = self.convert_dest / f"converted.{convert_format}" |
| 218 |
assert ( |
| 219 |
self.file_endswith(converted_path, convert_format) |
| 220 |
is should_transcode |
| 221 |
) |
| 222 |
|
| 223 |
def test_playlist(self): |
| 224 |
self.io.addinput("y") |
| 225 |
self.run_convert("--playlist", "playlist.m3u8") |
| 226 |
assert (self.convert_dest / "playlist.m3u8").exists() |
| 227 |
|
| 228 |
def test_playlist_pretend(self): |
| 229 |
self.run_convert("--playlist", "playlist.m3u8", "--pretend") |
| 230 |
assert not (self.convert_dest / "playlist.m3u8").exists() |
| 231 |
|
| 232 |
@pytest.mark.parametrize( |
| 233 |
"config_overrides", |
| 234 |
[ |
| 235 |
_p({"no_convert": "format:ogg"}, id="no-covert"), |
| 236 |
_p({"never_convert_lossy_files": True}, id="never-convert-lossy-files"), |
| 237 |
], |
| 238 |
) # fmt: skip |
| 239 |
def test_force_overrides(self, config_overrides): |
| 240 |
[item] = self.add_item_fixtures(ext="ogg") |
| 241 |
self.io.addinput("y") |
| 242 |
|
| 243 |
with self.configure_plugin(config_overrides): |
| 244 |
self.run_convert_path(item, "--format", "opus", "--force") |
| 245 |
|
| 246 |
converted = self.convert_dest / "converted.ops" |
| 247 |
assert self.file_endswith(converted, "opus") |
| 248 |
|
| 249 |
@pytest.mark.parametrize( |
| 250 |
"args,no_convert,expected_entry", |
| 251 |
[ |
| 252 |
_p((), None, "converted.mp3", id="config-format"), |
| 253 |
_p(("--format", "opus"), None, "converted.ops", id="cli-format"), |
| 254 |
_p((), "format:ogg", "converted.ogg", id="not-transcoded"), |
| 255 |
_p(("--keep-new",), None, "converted.ogg", id="keep-new"), |
| 256 |
], |
| 257 |
) |
| 258 |
def test_playlist_entry(self, args, no_convert, expected_entry): |
| 259 |
if no_convert: |
| 260 |
self.config["convert"]["no_convert"] = no_convert |
| 261 |
|
| 262 |
self.io.addinput("y") |
| 263 |
self.run_convert(*args, "--playlist", "playlist.m3u8") |
| 264 |
lines = (self.convert_dest / "playlist.m3u8").read_text().splitlines() |
| 265 |
assert lines[0] == "#EXTM3U" |
| 266 |
assert lines[1] == expected_entry |
| 267 |
|
| 268 |
|
| 269 |
class TestNeverConvertLossyFiles(ConvertPluginHelper, ConvertCommand): |
| 270 |
"""Test the effect of the `never_convert_lossy_files` option.""" |
| 271 |
|
| 272 |
@pytest.mark.parametrize( |
| 273 |
"source_ext,never_convert_lossy_files,expected_ext,should_convert", |
| 274 |
[ |
| 275 |
_p("flac", True, "mp3", True, id="lossless-converts-flag-on"), |
| 276 |
_p("flac", False, "mp3", True, id="lossless-converts-flag-off"), |
| 277 |
_p("ogg", False, "mp3", True, id="lossy-converts-allowed"), |
| 278 |
_p("ogg", True, "ogg", False, id="lossy-kept-prevented"), |
| 279 |
], |
| 280 |
) |
| 281 |
def test_transcode( |
| 282 |
self, |
| 283 |
source_ext, |
| 284 |
never_convert_lossy_files, |
| 285 |
expected_ext, |
| 286 |
should_convert, |
| 287 |
): |
| 288 |
[item] = self.add_item_fixtures(ext=source_ext) |
| 289 |
self.io.addinput("y") |
| 290 |
|
| 291 |
convert_fmt = "mp3" |
| 292 |
config = { |
| 293 |
"paths": {"default": "converted"}, |
| 294 |
"format": convert_fmt, |
| 295 |
"formats": {convert_fmt: self.tagged_copy_cmd(convert_fmt)}, |
| 296 |
"never_convert_lossy_files": never_convert_lossy_files, |
| 297 |
} |
| 298 |
with self.configure_plugin(config): |
| 299 |
self.run_convert_path(item) |
| 300 |
|
| 301 |
converted = self.convert_dest / f"converted.{expected_ext}" |
| 302 |
assert self.file_endswith(converted, convert_fmt) is should_convert |
| 303 |
|
| 304 |
|
| 305 |
class TestNoConvert(PluginTestHelper): |
| 306 |
"""Test the effect of the `no_convert` option.""" |
| 307 |
|
| 308 |
plugin = "convert" |
| 309 |
|
| 310 |
@pytest.mark.parametrize( |
| 311 |
"config_value, should_skip", |
| 312 |
[ |
| 313 |
("", False), |
| 314 |
("bitrate:320", False), |
| 315 |
("bitrate:320 format:ogg", False), |
| 316 |
("bitrate:320 , format:ogg", True), |
| 317 |
], |
| 318 |
) |
| 319 |
def test_no_convert_skip(self, config, config_value, should_skip): |
| 320 |
item = Item(format="ogg", bitrate=256) |
| 321 |
config["convert"]["no_convert"] = config_value |
| 322 |
assert convert.ConvertPlugin().in_no_convert(item) == should_skip |