"""The failure line names the free space of a nearly full temp volume, read once per error.""" import shutil import subprocess from pathlib import Path from types import SimpleNamespace from typing import Any import click import pytest from unshackle.commands import dl as dl_module from unshackle.commands.dl import dl from unshackle.core.config import config from unshackle.core.temp import TASK_PREFIX @pytest.fixture def temp(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: monkeypatch.setattr(config.directories, "temp", tmp_path) return tmp_path def free_space(monkeypatch: pytest.MonkeyPatch, free: int) -> list[str]: """Make each disk_usage read report ``free`` bytes and return the list of read paths.""" reads: list[str] = [] def usage(path: Any) -> Any: reads.append(str(path)) return shutil._ntuple_diskusage(10 * dl_module.LOW_FREE_SPACE, 0, free) # type: ignore[attr-defined] monkeypatch.setattr(dl_module.shutil, "disk_usage", usage) return reads def test_a_nearly_full_temp_volume_is_named_on_the_line(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None: free_space(monkeypatch, 38_000_000) error = subprocess.CalledProcessError(2, ["packager"]) _, line = dl.failure_lines(error) assert line == f" CalledProcessError: {str(error).rstrip('.')}, 38.0 MB free on {temp}" def test_enough_free_space_leaves_the_line_as_it_was(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None: free_space(monkeypatch, dl_module.LOW_FREE_SPACE) error = ValueError("boom") assert dl.failure_lines(error) == [":x: Download Failed...", " ValueError: boom"] def test_a_temp_dir_that_cannot_be_read_adds_nothing(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(config.directories, "temp", tmp_path / "missing") assert dl.failure_lines(ValueError("boom"))[1] == " ValueError: boom" def test_a_later_report_reuses_the_first_reading(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None: reads = free_space(monkeypatch, 38_000_000) error = OSError(28, "No space left on device") first = dl.failure_lines(error) free_space(monkeypatch, 5 * dl_module.LOW_FREE_SPACE) assert dl.failure_lines(error) == first assert len(reads) == 1 def test_a_click_exception_keeps_only_its_message(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None: reads = free_space(monkeypatch, 0) assert dl.failure_lines(click.ClickException("no key"))[1] == " no key" assert reads == [] def test_a_task_temp_dir_is_named_by_the_temp_dir_that_holds_it(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None: reads = free_space(monkeypatch, 0) monkeypatch.setattr(config.directories, "temp", temp / f"{TASK_PREFIX}example") assert dl.failure_lines(ValueError("boom"))[1] == f" ValueError: boom, 0 bytes free on {temp}" assert reads == [str(temp)] def test_a_failed_decrypt_is_read_before_its_track_deletes_its_files( temp: Path, monkeypatch: pytest.MonkeyPatch ) -> None: free_space(monkeypatch, 38_000_000) failure = subprocess.CalledProcessError(2, ["packager"]) def decrypt(*args: Any, **kwargs: Any) -> None: raise failure run = SimpleNamespace(keys=SimpleNamespace(decrypt_verified=decrypt)) with pytest.raises(subprocess.CalledProcessError): dl.decrypt_verified(run, "drm", temp / "video.mp4", track=None) # type: ignore[arg-type] free_space(monkeypatch, 5 * dl_module.LOW_FREE_SPACE) assert dl.failure_lines(failure)[1].endswith(f", 38.0 MB free on {temp}")