"""The exit code of the dl command when a title's track download fails.""" import signal from pathlib import Path from typing import Any import pytest from click.testing import CliRunner, Result from unshackle.commands import dl as dl_module from unshackle.core import process_exit from unshackle.core.titles import Movie, Movies from unshackle.core.tracks import Chapters, Tracks, Video pytestmark = pytest.mark.usefixtures("example_service") def run_dl( monkeypatch: pytest.MonkeyPatch, tmp_path: Path, download: Any, *args: str, standalone_mode: bool = True ) -> Result: """Run ``dl EXAMPLE`` on one movie with one video track, with ``download`` as the track download step.""" from unshackle.core import services service = services.ensure_loaded("EXAMPLE") assert service is not None movie = Movie(id_="movie-1", service=service, name="Title", year=2020, language="en") monkeypatch.setattr(service, "authenticate", lambda self, *a, **k: None) monkeypatch.setattr(service, "get_titles", lambda self: Movies([movie])) monkeypatch.setattr( service, "get_tracks", lambda self, title: Tracks(Video(url="https://example.invalid/v.mp4", language="en", codec=Video.Codec.AVC)), ) monkeypatch.setattr(service, "get_chapters", lambda self, title: Chapters()) monkeypatch.setattr(dl_module.TitleMetadata, "search", lambda self, *a, **k: None) monkeypatch.setattr(dl_module.config, "key_vaults", []) monkeypatch.setattr(dl_module.config, "output_template", {"movies": "{title}"}) monkeypatch.setattr(dl_module.config.directories, "temp", tmp_path / "temp") monkeypatch.setattr(dl_module.dl, "get_cdm", lambda self, *a, **k: None) monkeypatch.setattr(dl_module, "download_tracks_in_passes", download) monkeypatch.setattr(dl_module.dl.cli, "_result_callback", dl_module.dl.cli._result_callback) args = ("-o", str(tmp_path / "out"), "-l", "en", *args, "EXAMPLE", "--movie", "movie-1") return CliRunner().invoke(dl_module.dl.cli, args, standalone_mode=standalone_mode) def fail(*_: Any, **__: Any) -> None: raise ConnectionError("segment refused") @pytest.mark.parametrize("standalone_mode", [True, False], ids=["cli", "embedded"]) def test_a_failed_track_download_exits_1( monkeypatch: pytest.MonkeyPatch, tmp_path: Path, standalone_mode: bool ) -> None: result = run_dl(monkeypatch, tmp_path, fail, standalone_mode=standalone_mode) assert "segment refused" in result.output assert result.exit_code == 1 def test_a_download_that_a_signal_stopped_keeps_the_signal_exit( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: def stopped(*_: Any, **__: Any) -> None: monkeypatch.setattr(process_exit.Stop, "signum", signal.SIGTERM) fail() assert run_dl(monkeypatch, tmp_path, stopped).exit_code == 128 + signal.SIGTERM @pytest.mark.parametrize("flag", ["--no-mux", "--skip-dl", "--list"]) def test_a_run_without_a_failed_download_exits_0(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, flag: str) -> None: assert run_dl(monkeypatch, tmp_path, lambda *a, **k: None, flag).exit_code == 0