Files
lintunes/tests/test_round111.py
T

99 lines
3.7 KiB
Python

"""Round 111: a tag or art save on the playing song doesn't glitch playback.
mutagen saves in place, shifting the audio inside the file when a tag
outgrows its padding; a decoder holding the file open then reads from the
wrong offsets. The playing file is instead edited as a copy and renamed
over it, so what's already open stays byte for byte what it was."""
import os
import pytest
from lintunes import tagging
def _big_art():
# Far more than any padding: the in-place save would move the audio.
return b"\xff\xd8\xff\xe0" + os.urandom(300_000)
def _strays(path):
return [p for p in path.parent.iterdir() if p.name.startswith(".lintunes-tag-")]
@pytest.mark.parametrize("fixture", ["mp3_file", "flac_file", "m4a_file"])
def test_the_playing_file_is_replaced_not_rewritten(request, monkeypatch, fixture):
path = request.getfixturevalue(fixture)
original = path.read_bytes()
monkeypatch.setattr(tagging, "in_use", lambda: (str(path),))
with open(path, "rb") as playing: # the decoder's open file
inode = os.fstat(playing.fileno()).st_ino
tagging.write_artwork(path, _big_art())
tagging.write_tags(path, {"name": "New Name", "artist": "Someone"})
assert playing.read() == original # nothing moved under it
assert os.stat(path).st_ino != inode
assert tagging.read_tags(path)["name"] == "New Name"
assert tagging.read_embedded_artwork(path) is not None
assert _strays(path) == []
def test_mode_is_kept(mp3_file, monkeypatch):
os.chmod(mp3_file, 0o640)
monkeypatch.setattr(tagging, "in_use", lambda: (str(mp3_file),))
tagging.write_tags(mp3_file, {"name": "x"})
assert os.stat(mp3_file).st_mode & 0o777 == 0o640
def test_other_files_are_still_saved_in_place(mp3_file):
inode = os.stat(mp3_file).st_ino
tagging.write_tags(mp3_file, {"name": "In Place"})
assert os.stat(mp3_file).st_ino == inode
assert tagging.read_tags(mp3_file)["name"] == "In Place"
def test_a_failed_save_leaves_the_file_and_no_copy(mp3_file, monkeypatch):
original = mp3_file.read_bytes()
monkeypatch.setattr(tagging, "in_use", lambda: (str(mp3_file),))
real = tagging._write_tags
def broken(work, fields, path):
real(work, fields, path)
raise RuntimeError("disk on fire")
monkeypatch.setattr(tagging, "_write_tags", broken)
with pytest.raises(RuntimeError):
tagging.write_tags(mp3_file, {"name": "never"})
assert mp3_file.read_bytes() == original
assert _strays(mp3_file) == []
def test_no_room_for_a_copy_saves_in_place(mp3_file, monkeypatch):
monkeypatch.setattr(tagging, "in_use", lambda: (str(mp3_file),))
def full(*_a, **_k):
raise OSError(28, "No space left on device")
monkeypatch.setattr(tagging.shutil, "copyfile", full)
tagging.write_tags(mp3_file, {"name": "Squeezed"})
assert tagging.read_tags(mp3_file)["name"] == "Squeezed"
assert _strays(mp3_file) == []
def test_the_window_names_the_playing_song(qapp, tmp_path):
from lintunes.gui.main_window import MainWindow
from lintunes.library_manager import LibraryManager
from lintunes.models.library import Library
from lintunes.models.track import Track
from lintunes.preferences import Preferences
library = Library()
library.tracks[1] = Track(track_id=1, name="Song", location="/music/song.mp3")
window = MainWindow(LibraryManager(library, tmp_path / "data"),
Preferences(tmp_path / "data"))
try:
assert tagging.in_use == window._files_in_use
assert tagging.in_use() == ()
window.player._current_track = library.tracks[1]
assert tagging.in_use() == ("/music/song.mp3",)
finally:
window.player._current_track = None
window.close()