Files
unshackle/tests/commands/test_failure_lines.py
T

93 lines
3.3 KiB
Python

"""The failure line names the free space of a nearly full temp volume, read once per error."""
import shutil
import subprocess
from functools import partial
from pathlib import Path
from types import SimpleNamespace
from typing import Any
import click
import pytest
from unshackle.commands.dl import dl
from unshackle.core import temp as temp_module
from unshackle.core.config import config
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY
from unshackle.core.manifests import DASH
from unshackle.core.temp import LOW_FREE_SPACE, TASK_PREFIX
from unshackle.core.tracks import Video
@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 * LOW_FREE_SPACE, 0, free) # type: ignore[attr-defined]
monkeypatch.setattr(temp_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_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_track_is_read_before_it_deletes_its_files(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
written = temp / "Video_v.mp4"
seen: list[bool] = []
failure = OSError(28, "No space left on device")
def usage(path: Any) -> Any:
seen.append(written.exists())
return SimpleNamespace(free=38_000_000)
def parser(track: Video, ctx: Any) -> None:
written.write_bytes(b"partial")
raise failure
monkeypatch.setattr(temp_module.shutil, "disk_usage", usage)
monkeypatch.setattr(DASH, "download_track", staticmethod(parser))
DOWNLOAD_CANCELLED.clear()
DOWNLOAD_LICENCE_ONLY.clear()
track = Video(
url="https://x.example/v.mpd", language="en", codec=Video.Codec.AVC, id_="v", descriptor=Video.Descriptor.DASH
)
with pytest.raises(OSError):
track.download( # type: ignore[arg-type]
session=SimpleNamespace(proxies={}), prepare_drm=partial(lambda: None), progress=partial(lambda **kw: None)
)
free_space(monkeypatch, 5 * LOW_FREE_SPACE)
assert seen == [True]
assert not written.exists()
assert dl.failure_lines(failure)[1].endswith(f", 38.0 MB free on {temp}")