Files
unshackle/tests/tracks/test_ccextractor_retry.py
T

56 lines
1.9 KiB
Python

"""Tests for the repackaged-copy retry in ``Video.ccextractor``."""
from __future__ import annotations
import subprocess
from pathlib import Path
from typing import Any
import pytest
from langcodes import Language
from unshackle.core.tracks import Video
from unshackle.core.tracks import video as video_module
def no_captions_exit(args: list[Any]) -> subprocess.CompletedProcess:
"""ccextractor exits 2 when the input file is missing and 10 when it finds no captions."""
if not Path(args[-1]).exists():
raise subprocess.CalledProcessError(2, args)
raise subprocess.CalledProcessError(10, args)
def empty_output(args: list[Any]) -> subprocess.CompletedProcess:
Path(args[-2]).write_bytes(b"")
return subprocess.CompletedProcess(args, 0, b"", b"")
@pytest.mark.parametrize("ccextractor", [no_captions_exit, empty_output])
def test_no_captions_retries_on_the_repackaged_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, ccextractor: Any
) -> None:
original = tmp_path / "v.mp4"
original.write_bytes(b"x")
repacked = tmp_path / "v_repack.mp4"
track = Video(id_="v", url="https://example.test/v.m3u8", language="en", codec=Video.Codec.AVC)
track.path = original
def fake_repackage() -> None:
original.unlink()
repacked.write_bytes(b"x")
track.path = repacked
inputs: list[Path] = []
def fake_run(args: list[Any], **_: Any) -> subprocess.CompletedProcess:
inputs.append(Path(args[-1]))
return ccextractor(args)
monkeypatch.setattr(video_module.binaries, "CCExtractor", Path("/usr/bin/ccextractor"))
monkeypatch.setattr(video_module.subprocess, "run", fake_run)
monkeypatch.setattr(track, "repackage", fake_repackage)
assert track.ccextractor("cc", tmp_path / "cc.srt", Language.get("en")) is None
assert inputs == [original, repacked]
assert not (tmp_path / "cc.srt").exists()