mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +01:00
Replace assertFileTag and assertNoFileTag
This commit is contained in:
parent
43b8cce063
commit
c6b5b3bed3
1 changed files with 22 additions and 43 deletions
|
|
@ -18,6 +18,7 @@ import os.path
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from mediafile import MediaFile
|
from mediafile import MediaFile
|
||||||
|
|
@ -32,7 +33,6 @@ from beets.test.helper import (
|
||||||
capture_log,
|
capture_log,
|
||||||
control_stdin,
|
control_stdin,
|
||||||
)
|
)
|
||||||
from beets.util import displayable_path
|
|
||||||
from beetsplug import convert
|
from beetsplug import convert
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -58,36 +58,11 @@ class ConvertMixin:
|
||||||
shell_quote(sys.executable), shell_quote(stub), tag
|
shell_quote(sys.executable), shell_quote(stub), tag
|
||||||
)
|
)
|
||||||
|
|
||||||
def assert_is_file(self, path):
|
def file_endswith(self, path: Path, tag: str):
|
||||||
path = Path(os.fsdecode(path))
|
"""Check the path is a file and if its content ends with `tag`."""
|
||||||
assert path.exists()
|
assert path.exists()
|
||||||
assert path.is_file()
|
assert path.is_file()
|
||||||
|
return path.read_bytes().endswith(tag.encode("utf-8"))
|
||||||
def assertFileTag(self, path, tag):
|
|
||||||
"""Assert that the path is a file and the files content ends
|
|
||||||
with `tag`.
|
|
||||||
"""
|
|
||||||
display_tag = tag
|
|
||||||
tag = tag.encode("utf-8")
|
|
||||||
self.assert_is_file(path)
|
|
||||||
with open(path, "rb") as f:
|
|
||||||
f.seek(-len(display_tag), os.SEEK_END)
|
|
||||||
assert f.read() == tag, (
|
|
||||||
f"{displayable_path(path)} is not tagged with {display_tag}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def assertNoFileTag(self, path, tag):
|
|
||||||
"""Assert that the path is a file and the files content does not
|
|
||||||
end with `tag`.
|
|
||||||
"""
|
|
||||||
display_tag = tag
|
|
||||||
tag = tag.encode("utf-8")
|
|
||||||
self.assert_is_file(path)
|
|
||||||
with open(path, "rb") as f:
|
|
||||||
f.seek(-len(tag), os.SEEK_END)
|
|
||||||
assert f.read() != tag, (
|
|
||||||
f"{displayable_path(path)} is unexpectedly tagged with {display_tag}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ConvertTestCase(ConvertMixin, PluginTestCase):
|
class ConvertTestCase(ConvertMixin, PluginTestCase):
|
||||||
|
|
@ -111,7 +86,7 @@ class ImportConvertTest(AsIsImporterMixin, ImportHelper, ConvertTestCase):
|
||||||
def test_import_converted(self):
|
def test_import_converted(self):
|
||||||
self.run_asis_importer()
|
self.run_asis_importer()
|
||||||
item = self.lib.items().get()
|
item = self.lib.items().get()
|
||||||
self.assertFileTag(item.filepath, "convert")
|
assert self.file_endswith(item.filepath, "convert")
|
||||||
|
|
||||||
# FIXME: fails on windows
|
# FIXME: fails on windows
|
||||||
@unittest.skipIf(sys.platform == "win32", "win32")
|
@unittest.skipIf(sys.platform == "win32", "win32")
|
||||||
|
|
@ -122,7 +97,7 @@ class ImportConvertTest(AsIsImporterMixin, ImportHelper, ConvertTestCase):
|
||||||
|
|
||||||
item = self.lib.items().get()
|
item = self.lib.items().get()
|
||||||
assert item is not None
|
assert item is not None
|
||||||
self.assert_is_file(item.path)
|
assert item.filepath.is_file()
|
||||||
|
|
||||||
def test_delete_originals(self):
|
def test_delete_originals(self):
|
||||||
self.config["convert"]["delete_originals"] = True
|
self.config["convert"]["delete_originals"] = True
|
||||||
|
|
@ -183,11 +158,11 @@ class ConvertCliTest(ConvertTestCase, ConvertCommand):
|
||||||
def test_convert(self):
|
def test_convert(self):
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertFileTag(self.converted_mp3, "mp3")
|
assert self.file_endswith(self.converted_mp3, "mp3")
|
||||||
|
|
||||||
def test_convert_with_auto_confirmation(self):
|
def test_convert_with_auto_confirmation(self):
|
||||||
self.run_convert("--yes")
|
self.run_convert("--yes")
|
||||||
self.assertFileTag(self.converted_mp3, "mp3")
|
assert self.file_endswith(self.converted_mp3, "mp3")
|
||||||
|
|
||||||
def test_reject_confirmation(self):
|
def test_reject_confirmation(self):
|
||||||
with control_stdin("n"):
|
with control_stdin("n"):
|
||||||
|
|
@ -206,7 +181,7 @@ class ConvertCliTest(ConvertTestCase, ConvertCommand):
|
||||||
def test_format_option(self):
|
def test_format_option(self):
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert("--format", "opus")
|
self.run_convert("--format", "opus")
|
||||||
self.assertFileTag(self.convert_dest / "converted.ops", "opus")
|
assert self.file_endswith(self.convert_dest / "converted.ops", "opus")
|
||||||
|
|
||||||
def test_embed_album_art(self):
|
def test_embed_album_art(self):
|
||||||
self.config["convert"]["embed"] = True
|
self.config["convert"]["embed"] = True
|
||||||
|
|
@ -241,38 +216,42 @@ class ConvertCliTest(ConvertTestCase, ConvertCommand):
|
||||||
self.config["convert"]["max_bitrate"] = 5000
|
self.config["convert"]["max_bitrate"] = 5000
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertFileTag(self.converted_mp3, "mp3")
|
assert self.file_endswith(self.converted_mp3, "mp3")
|
||||||
|
|
||||||
def test_transcode_when_maxbr_set_low_and_different_formats(self):
|
def test_transcode_when_maxbr_set_low_and_different_formats(self):
|
||||||
self.config["convert"]["max_bitrate"] = 5
|
self.config["convert"]["max_bitrate"] = 5
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertFileTag(self.converted_mp3, "mp3")
|
assert self.file_endswith(self.converted_mp3, "mp3")
|
||||||
|
|
||||||
def test_transcode_when_maxbr_set_to_none_and_different_formats(self):
|
def test_transcode_when_maxbr_set_to_none_and_different_formats(self):
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertFileTag(self.converted_mp3, "mp3")
|
assert self.file_endswith(self.converted_mp3, "mp3")
|
||||||
|
|
||||||
def test_no_transcode_when_maxbr_set_high_and_same_formats(self):
|
def test_no_transcode_when_maxbr_set_high_and_same_formats(self):
|
||||||
self.config["convert"]["max_bitrate"] = 5000
|
self.config["convert"]["max_bitrate"] = 5000
|
||||||
self.config["convert"]["format"] = "ogg"
|
self.config["convert"]["format"] = "ogg"
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertNoFileTag(self.convert_dest / "converted.ogg", "ogg")
|
assert not self.file_endswith(
|
||||||
|
self.convert_dest / "converted.ogg", "ogg"
|
||||||
|
)
|
||||||
|
|
||||||
def test_transcode_when_maxbr_set_low_and_same_formats(self):
|
def test_transcode_when_maxbr_set_low_and_same_formats(self):
|
||||||
self.config["convert"]["max_bitrate"] = 5
|
self.config["convert"]["max_bitrate"] = 5
|
||||||
self.config["convert"]["format"] = "ogg"
|
self.config["convert"]["format"] = "ogg"
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertFileTag(self.convert_dest / "converted.ogg", "ogg")
|
assert self.file_endswith(self.convert_dest / "converted.ogg", "ogg")
|
||||||
|
|
||||||
def test_transcode_when_maxbr_set_to_none_and_same_formats(self):
|
def test_transcode_when_maxbr_set_to_none_and_same_formats(self):
|
||||||
self.config["convert"]["format"] = "ogg"
|
self.config["convert"]["format"] = "ogg"
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert()
|
self.run_convert()
|
||||||
self.assertNoFileTag(self.convert_dest / "converted.ogg", "ogg")
|
assert not self.file_endswith(
|
||||||
|
self.convert_dest / "converted.ogg", "ogg"
|
||||||
|
)
|
||||||
|
|
||||||
def test_playlist(self):
|
def test_playlist(self):
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
|
|
@ -307,7 +286,7 @@ class NeverConvertLossyFilesTest(ConvertTestCase, ConvertCommand):
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert_path(item)
|
self.run_convert_path(item)
|
||||||
converted = self.convert_dest / "converted.mp3"
|
converted = self.convert_dest / "converted.mp3"
|
||||||
self.assertFileTag(converted, "mp3")
|
assert self.file_endswith(converted, "mp3")
|
||||||
|
|
||||||
def test_transcode_from_lossy(self):
|
def test_transcode_from_lossy(self):
|
||||||
self.config["convert"]["never_convert_lossy_files"] = False
|
self.config["convert"]["never_convert_lossy_files"] = False
|
||||||
|
|
@ -315,14 +294,14 @@ class NeverConvertLossyFilesTest(ConvertTestCase, ConvertCommand):
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert_path(item)
|
self.run_convert_path(item)
|
||||||
converted = self.convert_dest / "converted.mp3"
|
converted = self.convert_dest / "converted.mp3"
|
||||||
self.assertFileTag(converted, "mp3")
|
assert self.file_endswith(converted, "mp3")
|
||||||
|
|
||||||
def test_transcode_from_lossy_prevented(self):
|
def test_transcode_from_lossy_prevented(self):
|
||||||
[item] = self.add_item_fixtures(ext="ogg")
|
[item] = self.add_item_fixtures(ext="ogg")
|
||||||
with control_stdin("y"):
|
with control_stdin("y"):
|
||||||
self.run_convert_path(item)
|
self.run_convert_path(item)
|
||||||
converted = self.convert_dest / "converted.ogg"
|
converted = self.convert_dest / "converted.ogg"
|
||||||
self.assertNoFileTag(converted, "mp3")
|
assert not self.file_endswith(converted, "mp3")
|
||||||
|
|
||||||
|
|
||||||
class TestNoConvert:
|
class TestNoConvert:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue