v0.68.0: DJ vocal splits survive tag edits; a size limit for their cache
This commit is contained in:
@@ -33,3 +33,118 @@ def test_dragging_narrower_snaps_back_and_saves_the_minimum(qapp):
|
||||
def test_wider_widths_are_left_alone(qapp):
|
||||
view = _view({"rating": 200})
|
||||
assert view.columnWidth(view.model_.fields.index("rating")) == 200
|
||||
|
||||
|
||||
# ---- DJ stems: a cache that survives a tag edit, held to a size ----
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from lintunes.dj import stems
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cache(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache"))
|
||||
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config"))
|
||||
return tmp_path
|
||||
|
||||
|
||||
def _mp3(path):
|
||||
subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i",
|
||||
"sine=f=440:d=2", str(path)], check=True)
|
||||
return str(path)
|
||||
|
||||
|
||||
def _make(folder, size=1):
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
for name in stems.STEMS:
|
||||
(folder / f"{name}.flac").write_bytes(b"f" * size)
|
||||
|
||||
|
||||
needs_ffmpeg = pytest.mark.skipif(shutil.which("ffmpeg") is None,
|
||||
reason="needs the ffmpeg CLI")
|
||||
|
||||
|
||||
@needs_ffmpeg
|
||||
def test_writing_the_bpm_tag_keeps_the_split(cache):
|
||||
from mutagen.id3 import ID3, TBPM
|
||||
song = _mp3(cache / "a.mp3")
|
||||
_make(stems.cache_root() / stems.cache_key(song) / "htdemucs")
|
||||
tags = ID3()
|
||||
tags.add(TBPM(encoding=3, text=["128"]))
|
||||
tags.save(song) # what loading a song with no BPM does
|
||||
os.utime(song, (time.time() + 60, time.time() + 60))
|
||||
assert stems.cached(song) is not None
|
||||
|
||||
|
||||
@needs_ffmpeg
|
||||
def test_a_split_under_the_old_path_key_is_moved_to_the_audio_key(cache):
|
||||
song = _mp3(cache / "a.mp3")
|
||||
_make(stems.cache_root() / stems._path_key(song) / "htdemucs")
|
||||
assert stems.cached(song) is not None
|
||||
assert (stems.cache_root() / stems.cache_key(song) / "htdemucs").is_dir()
|
||||
assert not (stems.cache_root() / stems._path_key(song)).exists()
|
||||
|
||||
|
||||
@needs_ffmpeg
|
||||
def test_the_hash_runs_once_per_version_of_a_file(cache, monkeypatch):
|
||||
song = _mp3(cache / "a.mp3")
|
||||
calls = []
|
||||
real = stems.audio_hash
|
||||
monkeypatch.setattr(stems, "audio_hash",
|
||||
lambda p: calls.append(p) or real(p))
|
||||
first = stems.cache_key(song)
|
||||
assert stems.cache_key(song) == first
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
def test_unreadable_audio_falls_back_to_the_path_key(cache):
|
||||
song = cache / "a.mp3"
|
||||
song.write_bytes(b"x")
|
||||
assert stems.cache_key(str(song)) == stems._path_key(str(song))
|
||||
|
||||
|
||||
def test_prune_drops_the_songs_used_longest_ago(cache):
|
||||
root = stems.cache_root()
|
||||
now = time.time()
|
||||
for i, name in enumerate(["old", "mid", "new"]):
|
||||
_make(root / name / "htdemucs", size=400 * 1024) # 800 KiB a song
|
||||
os.utime(root / name, (now - 100 + i, now - 100 + i))
|
||||
removed = stems.prune(limit_gb=2 * 800 / 1024 ** 2) # room for two
|
||||
assert removed == 1
|
||||
assert sorted(p.name for p in root.iterdir()) == ["mid", "new"]
|
||||
stems.prune(limit_gb=0, keep="mid")
|
||||
assert [p.name for p in root.iterdir()] == ["mid"]
|
||||
|
||||
|
||||
def test_the_limit_comes_from_config_json(cache):
|
||||
from lintunes.config import save_config
|
||||
assert stems.cache_limit_gb() == 1.0
|
||||
save_config({stems.CACHE_LIMIT_KEY: 2.5})
|
||||
assert stems.cache_limit_gb() == 2.5
|
||||
assert stems.cache_limit_gb({stems.CACHE_LIMIT_KEY: "junk"}) == 1.0
|
||||
|
||||
|
||||
def test_preferences_saves_the_limit_to_config_json(qapp, cache, monkeypatch):
|
||||
from lintunes.config import load_config
|
||||
from lintunes.gui.preferences_dialog import PreferencesDialog
|
||||
from lintunes.preferences import Preferences
|
||||
from tests.test_round40 import _FakeLastFm
|
||||
pruned = []
|
||||
monkeypatch.setattr(stems, "prune", lambda limit: pruned.append(limit))
|
||||
dialog = PreferencesDialog(Preferences(cache), _FakeLastFm())
|
||||
assert dialog._dj_cache_edit.text() == "1"
|
||||
dialog._dj_cache_edit.setText("3.5")
|
||||
dialog._save_stem_cache_limit()
|
||||
assert load_config()[stems.CACHE_LIMIT_KEY] == 3.5
|
||||
for _ in range(50):
|
||||
if pruned:
|
||||
break
|
||||
time.sleep(0.01)
|
||||
assert pruned == [3.5]
|
||||
dialog.deleteLater()
|
||||
|
||||
@@ -136,6 +136,7 @@ class TestStems:
|
||||
song = self._song(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(stems, "available", lambda: True)
|
||||
monkeypatch.setattr(stems.os, "cpu_count", lambda: 8)
|
||||
monkeypatch.setattr(stems, "audio_hash", lambda path: None)
|
||||
seen = {}
|
||||
|
||||
class Proc:
|
||||
|
||||
Reference in New Issue
Block a user