refactor(temp): read the free space where the failed step deletes its temp files

This commit is contained in:
imSp4rky
2026-11-29 15:23:03 -07:00
parent 977cbe4aa8
commit 1ebc3e3624
13 changed files with 155 additions and 59 deletions
+34 -16
View File
@@ -2,6 +2,7 @@
import shutil
import subprocess
from functools import partial
from pathlib import Path
from types import SimpleNamespace
from typing import Any
@@ -9,10 +10,13 @@ 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 import temp as temp_module
from unshackle.core.config import config
from unshackle.core.temp import TASK_PREFIX
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
@@ -27,9 +31,9 @@ def free_space(monkeypatch: pytest.MonkeyPatch, free: int) -> 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]
return shutil._ntuple_diskusage(10 * LOW_FREE_SPACE, 0, free) # type: ignore[attr-defined]
monkeypatch.setattr(dl_module.shutil, "disk_usage", usage)
monkeypatch.setattr(temp_module.shutil, "disk_usage", usage)
return reads
@@ -43,7 +47,7 @@ def test_a_nearly_full_temp_volume_is_named_on_the_line(temp: Path, monkeypatch:
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)
free_space(monkeypatch, LOW_FREE_SPACE)
error = ValueError("boom")
assert dl.failure_lines(error) == [":x: Download Failed...", " ValueError: boom"]
@@ -59,7 +63,7 @@ def test_a_later_report_reuses_the_first_reading(temp: Path, monkeypatch: pytest
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)
free_space(monkeypatch, 5 * LOW_FREE_SPACE)
assert dl.failure_lines(error) == first
assert len(reads) == 1
@@ -80,18 +84,32 @@ def test_a_task_temp_dir_is_named_by_the_temp_dir_that_holds_it(temp: Path, monk
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 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 decrypt(*args: Any, **kwargs: Any) -> None:
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
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)
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}")