Files
unshackle/tests/remote/unit/test_cdm_patch_leaves_no_shadow.py
T
imSp4rky 22cc739e83 fix(downloader): correct truncation, ranged sizing, and session carry
Check a sequential download against the complete length whenever one is known, and settle the unknown-total case on the 416. An empty body of unknown length, a 206 with no complete length, and a 206 starting past the resume offset all passed the size checks before and were renamed into place. Four restart paths spent no attempt, so a host that always answered the same way cycled forever.
2026-09-06 20:03:08 -06:00

41 lines
1.7 KiB
Python

"""No test may patch ``load_cdm`` on the CDM package with ``setattr``.
``unshackle.core.cdm`` resolves ``load_cdm`` through a lazy ``__getattr__`` (PEP 562), so the
name never sits in the package dict. ``monkeypatch.setattr`` on the package reads the old value
through ``__getattr__`` and, on undo, writes the real function into the package dict. That key
shadows ``__getattr__`` for the rest of the process, and every later test that patches
``unshackle.core.cdm.loader`` loses its stub. ``tests/commands/test_cdm_cache.py`` ran the real
loader and raised ``ValueError: device_b does not exist``.
``monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", ...)`` leaves no residue: it deletes the key
it added. This guard names the file at fault instead of leaving the damage to surface in an
unrelated test.
"""
from __future__ import annotations
import re
from pathlib import Path
from typing import List
import pytest
pytestmark = pytest.mark.unit
TESTS_ROOT = Path(__file__).resolve().parents[2]
# A setattr of load_cdm on anything but the loader module, which holds the name for real.
PACKAGE_SETATTR = re.compile(r"""setattr\(\s*(?!loader\b)[\w.]+\s*,\s*["']load_cdm["']""")
def test_no_test_patches_load_cdm_on_the_package() -> None:
offenders: List[str] = []
for path in sorted(TESTS_ROOT.rglob("test_*.py")):
if path == Path(__file__).resolve():
continue
for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1):
if PACKAGE_SETATTR.search(line):
offenders.append(f"{path.relative_to(TESTS_ROOT)}:{number}: {line.strip()}")
assert not offenders, "patch the package dict with monkeypatch.setitem instead:\n" + "\n".join(offenders)