refactor: Spur-Fehlermeldung und yt-dlp-Fake gemeinsam nutzen

- yt_untertitel.no_track_message() für yt_untertitel.py und extract_text.py
- tests/conftest.py: Fixtures fake_yt_dlp und yt_dlp_installed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-10-10 19:52:20 +02:00
commit 0413455ea5

View file

@ -1,8 +1,6 @@
"""Tests for yt_untertitel.py; yt-dlp is always replaced by fakes (no network)."""
import json
import subprocess
import sys
from pathlib import Path
import pytest
@ -18,31 +16,7 @@ AUTO_ONLY = {
}
def fake_yt_dlp(info, write=True, fail_download=False):
"""Stand-in for subprocess.run: answers the info request, writes the VTT file."""
calls = []
def run(command, capture_output, text):
calls.append(command)
if "--dump-single-json" in command:
return subprocess.CompletedProcess(command, 0, json.dumps(info), "")
if fail_download:
return subprocess.CompletedProcess(command, 1, "", "HTTP Error 429")
track = command[command.index("--sub-langs") + 1]
template = command[command.index("-o") + 1]
if write:
Path(template.replace("%(ext)s", f"{track}.vtt")).write_text(
"WEBVTT\n", encoding="utf-8"
)
return subprocess.CompletedProcess(command, 0, "", "")
run.calls = calls
return run
@pytest.fixture(autouse=True)
def yt_dlp_installed(monkeypatch):
monkeypatch.setattr(yt.shutil, "which", lambda name: f"/usr/bin/{name}")
pytestmark = pytest.mark.usefixtures("yt_dlp_installed")
def run_main(monkeypatch, *args):
@ -79,7 +53,7 @@ def test_available_tracks():
# === Command line ===
def test_main_downloads_auto_original(tmp_path, monkeypatch, capsys):
def test_main_downloads_auto_original(fake_yt_dlp, tmp_path, monkeypatch, capsys):
run = fake_yt_dlp(AUTO_ONLY)
monkeypatch.setattr(yt.subprocess, "run", run)
run_main(monkeypatch, "-u", URL, "-n", "Rede", "-d", tmp_path / "neu")
@ -93,7 +67,7 @@ def test_main_downloads_auto_original(tmp_path, monkeypatch, capsys):
assert "vtt2txt.py -i" in out
def test_main_prefers_manual_and_defaults_to_video_id(tmp_path, monkeypatch):
def test_main_prefers_manual_and_defaults_to_video_id(fake_yt_dlp, tmp_path, monkeypatch):
info = dict(AUTO_ONLY, subtitles={"de": []})
run = fake_yt_dlp(info)
monkeypatch.setattr(yt.subprocess, "run", run)
@ -103,7 +77,7 @@ def test_main_prefers_manual_and_defaults_to_video_id(tmp_path, monkeypatch):
assert "--write-subs" in run.calls[1]
def test_main_no_matching_track(tmp_path, monkeypatch):
def test_main_no_matching_track(fake_yt_dlp, tmp_path, monkeypatch):
monkeypatch.setattr(yt.subprocess, "run", fake_yt_dlp(AUTO_ONLY))
with pytest.raises(SystemExit, match="Automatisch \\(Originalsprache\\): de"):
run_main(monkeypatch, "-u", URL, "-l", "en", "-d", tmp_path)
@ -122,7 +96,7 @@ def test_main_info_error(tmp_path, monkeypatch):
({"fail_download": True}, "HTTP Error 429"),
({"write": False}, "keine Datei"),
])
def test_main_download_errors(tmp_path, monkeypatch, kwargs, message):
def test_main_download_errors(fake_yt_dlp, tmp_path, monkeypatch, kwargs, message):
monkeypatch.setattr(yt.subprocess, "run", fake_yt_dlp(AUTO_ONLY, **kwargs))
with pytest.raises(SystemExit, match=message):
run_main(monkeypatch, "-u", URL, "-d", tmp_path)