"""A failed mkvmerge stops the run with one line that names the tool, its exit and its error.""" import logging from pathlib import Path import pytest from unshackle.core import binaries from unshackle.core.config import config from unshackle.core.mux_step import mux_title from unshackle.core.placement import Placement from unshackle.core.run_plan import RunStopped from unshackle.core.titles import Movie from unshackle.core.tracks import Video from unshackle.core.tracks import tracks as tracks_module pytestmark = pytest.mark.skipif(not Path("/bin/sh").exists(), reason="needs a POSIX shell for the mkvmerge stub") def mux(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, script: str) -> None: monkeypatch.setattr(config.directories, "temp", tmp_path) stub = tmp_path / "mkvmerge" stub.write_text(f"#!/bin/sh\n{script}\n") stub.chmod(0o755) monkeypatch.setattr(binaries, "MKVToolNix", stub) monkeypatch.setattr(tracks_module, "file_track_ids", lambda path: {"video": 0}) movie = Movie(id_="movie-0001", service=type("EXAMPLE", (), {}), name="Some Movie", year=2024) video = Video(url="https://x.example/v.m3u8", language="en", codec=Video.Codec.AVC, id_="v") video.path = tmp_path / "v.mp4" video.path.write_bytes(b"video") movie.tracks.add(video) mux_title( movie, Placement(movie, root=tmp_path / "out", no_folder=True, no_source=True, sample=False), log=logging.getLogger("dl"), service_tag="EXAMPLE", split_audio=None, merge_video=None, audio_expected=False, skip_subtitles=True, quality=[], range_=[], vcodec=[], generate_dv=False, progress_sink=None, write_sidecars=None, ) def test_a_write_error_is_the_reason_of_the_stop( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture ) -> None: error = "The drive may be full. Exception details: No space left to write to" with pytest.raises(RunStopped) as raised: mux(tmp_path, monkeypatch, f"echo '#GUI#warning a minor thing'\necho '#GUI#error {error}'\nexit 2") assert raised.value.reason == f"mkvmerge failed (exit 2): {error}" assert [r.levelno for r in caplog.records if r.name == "dl" and r.levelno > logging.DEBUG] == [logging.WARNING] def test_a_failure_with_no_error_line_names_the_exit(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: with pytest.raises(RunStopped) as raised: mux(tmp_path, monkeypatch, "exit 2") assert raised.value.reason == "mkvmerge failed (exit 2)" def test_a_killed_muxer_stops_the_run(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: with pytest.raises(RunStopped) as raised: mux(tmp_path, monkeypatch, "kill -9 $$") assert raised.value.reason == "mkvmerge failed (signal 9)"