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,5 @@
"""Tests for extract_text.py; yt-dlp is always replaced by fakes (no network)."""
import json
import subprocess
import sys
from pathlib import Path
import pytest
@ -25,26 +22,7 @@ GERMAN_VIDEO = {
}
def fake_yt_dlp(info, vtt=VTT):
"""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), "")
track = command[command.index("--sub-langs") + 1]
template = command[command.index("-o") + 1]
Path(template.replace("%(ext)s", f"{track}.vtt")).write_text(vtt, encoding="utf-8")
return subprocess.CompletedProcess(command, 0, "", "")
run.calls = calls
return run
@pytest.fixture(autouse=True)
def yt_dlp_installed(monkeypatch):
monkeypatch.setattr(extract_text.shutil, "which", lambda name: f"/usr/bin/{name}")
pytestmark = pytest.mark.usefixtures("yt_dlp_installed")
def run_main(monkeypatch, *args):
@ -52,8 +30,8 @@ def run_main(monkeypatch, *args):
extract_text.main()
def test_default_language_is_original_audio(tmp_path, monkeypatch, capsys):
run = fake_yt_dlp(GERMAN_VIDEO)
def test_default_language_is_original_audio(fake_yt_dlp, tmp_path, monkeypatch, capsys):
run = fake_yt_dlp(GERMAN_VIDEO, vtt=VTT)
monkeypatch.setattr(yt.subprocess, "run", run)
monkeypatch.chdir(tmp_path)
run_main(monkeypatch, "-u", URL)
@ -68,8 +46,8 @@ def test_default_language_is_original_audio(tmp_path, monkeypatch, capsys):
assert [p.name for p in tmp_path.iterdir()] == ["output.txt"]
def test_explicit_language_prefers_manual(tmp_path, monkeypatch):
run = fake_yt_dlp(GERMAN_VIDEO)
def test_explicit_language_prefers_manual(fake_yt_dlp, tmp_path, monkeypatch):
run = fake_yt_dlp(GERMAN_VIDEO, vtt=VTT)
monkeypatch.setattr(yt.subprocess, "run", run)
target = tmp_path / "neu" / "rede.en.txt"
run_main(monkeypatch, "-u", URL, "-l", "en", "-o", target)
@ -78,9 +56,11 @@ def test_explicit_language_prefers_manual(tmp_path, monkeypatch):
assert "--write-subs" in run.calls[1]
def test_original_language_from_metadata_without_auto_captions(tmp_path, monkeypatch):
def test_original_language_from_metadata_without_auto_captions(
fake_yt_dlp, tmp_path, monkeypatch
):
info = {"id": "x", "language": "fr", "subtitles": {"fr": []}, "automatic_captions": {}}
run = fake_yt_dlp(info)
run = fake_yt_dlp(info, vtt=VTT)
monkeypatch.setattr(yt.subprocess, "run", run)
run_main(monkeypatch, "-u", URL, "-o", tmp_path / "out.txt")
assert run.calls[1][run.calls[1].index("--sub-langs") + 1] == "fr"
@ -91,14 +71,14 @@ def test_original_language_from_metadata_without_auto_captions(tmp_path, monkeyp
(GERMAN_VIDEO, ["-l", "fr"], "Keine Untertitel in „fr“"),
({"id": "x", "subtitles": {}, "automatic_captions": {}}, [], "Originalsprache"),
])
def test_no_matching_track(tmp_path, monkeypatch, info, args, message):
monkeypatch.setattr(yt.subprocess, "run", fake_yt_dlp(info))
def test_no_matching_track(fake_yt_dlp, tmp_path, monkeypatch, info, args, message):
monkeypatch.setattr(yt.subprocess, "run", fake_yt_dlp(info, vtt=VTT))
with pytest.raises(SystemExit, match=message):
run_main(monkeypatch, "-u", URL, "-o", tmp_path / "out.txt", *args)
assert not (tmp_path / "out.txt").exists()
def test_empty_subtitles(tmp_path, monkeypatch):
def test_empty_subtitles(fake_yt_dlp, tmp_path, monkeypatch):
monkeypatch.setattr(yt.subprocess, "run", fake_yt_dlp(GERMAN_VIDEO, vtt="WEBVTT\n"))
with pytest.raises(SystemExit, match="keinen Text"):
run_main(monkeypatch, "-u", URL, "-o", tmp_path / "out.txt")