testsAndMisc/python_pkg/wsg_grabber/tests/conftest.py
Krzysztof kuhy Rudnicki e03fbd17e9 Split wsg_grabber/tests/test_review.py under the 250-line cap
Split along the same seams the source took: test_review_render.py covers what
now lives in review._render, test_review_undo.py covers review._undo.

The two fixture builders they share moved into the existing conftest.py rather
than a _helpers.py -- the name-tests-test hook requires everything else under
tests/ to be named test_*.py, and syncyomi_guard/tests/conftest.py is the
precedent for plain shared builders living there. Sharing beats copying: jscpd
fails the commit above 2% duplication and this repo already carries four partN
test files.

277 passed, the same count as before, so nothing was dropped. Two intermediate
runs caught real NameErrors from imports the extracted module needed.

test_review.py 297 -> 112, test_review_render.py 102, test_review_undo.py 79.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-16 16:24:48 +02:00

86 lines
2.7 KiB
Python

"""Test isolation for the /wsg/ grabber.
This package writes an index and tens of gigabytes of video into
``~/.local/share/wsg_grabber``. A test that escapes into the real directory
would corrupt live user data, so redirection is autouse and belt-and-braces:
1. ``Path.home`` is patched on the *class*, so it holds no matter whether a
module wrote ``from pathlib import Path`` or ``import pathlib``.
2. ``HOME`` and ``XDG_DATA_HOME`` are redirected, catching anything that goes
through ``os.environ`` or ``expanduser`` instead.
3. Every module reaches storage only through :mod:`python_pkg.wsg_grabber.paths`
functions, which resolve on each call rather than at import.
``tests/test_paths.py`` adds a static check that no module captures a home
directory at import time, so layer 3 stays true as the package grows.
Also holds the fixture builders the reviewer test modules share. They live
here rather than in a ``_helpers.py`` because the ``name-tests-test`` hook
requires every other file under ``tests/`` to be named ``test_*.py``.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from python_pkg.wsg_grabber.models import ReviewedItem, ReviewItem, Verdict
@pytest.fixture(autouse=True)
def isolate_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Point every storage location at a throwaway directory.
No teardown is needed: ``monkeypatch`` unwinds both the environment and the
``Path.home`` patch when the test ends.
Args:
tmp_path: Per-test temporary directory supplied by pytest.
monkeypatch: Patching helper, unwound automatically after the test.
Returns:
Path: The fake home directory backing this test.
"""
home = tmp_path / "home"
data_home = home / ".local" / "share"
data_home.mkdir(parents=True)
monkeypatch.setenv("HOME", str(home))
monkeypatch.setenv("XDG_DATA_HOME", str(data_home))
monkeypatch.setattr(Path, "home", classmethod(lambda _cls: home))
return home
def item(name: str = "a") -> ReviewItem:
"""Build a ReviewItem.
Args:
name: Distinguishes items within a test.
Returns:
ReviewItem: Fixture value.
"""
return ReviewItem(
md5=f"{name}-md5",
path=Path(f"/incoming/{name}.webm"),
orig_name=f"{name}.webm",
fsize=2 * 1024 * 1024,
width=480,
height=360,
)
def reviewed(item: ReviewItem, choice: Verdict = Verdict.SKIP) -> ReviewedItem:
"""Build the record a verdict would have persisted.
Args:
item: The reviewed video.
choice: What was decided.
Returns:
ReviewedItem: Reversible verdict.
"""
return ReviewedItem(item=item, choice=choice, reviewed_name=item.path.name)