testsAndMisc/linux_configuration/tests/conftest.py
Krzysztof kuhy Rudnicki 7587cd9604 transcribe_helpers: cover 0% -> 100% before splitting
transcribe_helpers.py is 289 lines against the 250-line cap and was
completely untested. Tests land first, in their own commit, so the split
that follows is verified against tests that predate the seam.

Left at its current path rather than moved under python_pkg/: transcribe.sh
invokes it by path inside a dedicated ML venv, and transcribe_fw.py imports
two siblings by bare name, so the closure is five files (1236 lines), not
three. Tests go in linux_configuration/tests/ instead, whose conftest
already puts standalone script directories on sys.path - the same
arrangement the usage_report work used, and still collected by CI.

The ML dependencies are never installed in CI and the module reaches them
only through _try_import, so patching that one seam covers every probe in
both directions. An AST scan at every nesting level confirmed there is no
other path to them: the only direct numpy imports sit under TYPE_CHECKING.

41 tests, 100% line and branch. The last gap was the dispatch guard at
line 284, unreachable through the CLI because argparse's choices list and
the dispatch dict carry the same eight commands; covering it by admitting a
ninth choice also documents why the guard is there.

Split across four test files because a single module hit 291 lines - over
the cap this work exists to clear.

Verified: python-version prints 3.14, check-faster-whisper exits 7 with no
ML deps present, generate-wav writes a 96044-byte WAV; 243 tests pass in the
directory, up from 202 by exactly the 41 added.

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

95 lines
2.9 KiB
Python

"""Pytest bootstrap: make non-package script dirs importable for these tests.
Several helper modules live in standalone script directories (outside
``python_pkg/``) and are invoked as ``python <file>.py`` rather than imported as
packages. To unit-test them they must be importable by bare module name, so each
directory is placed on ``sys.path`` before the tests import them.
"""
from __future__ import annotations
from pathlib import Path
import sys
from typing import TYPE_CHECKING
import pytest
if TYPE_CHECKING:
from collections.abc import Callable
# Repo root is two levels up from this file (linux_configuration/tests/conftest.py).
_REPO_ROOT = Path(__file__).resolve().parents[2]
# Each standalone script directory whose Python modules these tests import.
_SCRIPT_DIRS = (
Path(__file__).resolve().parents[1]
/ "scripts"
/ "periodic_background"
/ "system-maintenance"
/ "bin", # usage_report modules
_REPO_ROOT / "meta" / "scripts", # validate_evidence, validate_contract
_REPO_ROOT / "phone_focus_mode" / "lib", # monitor_report
_REPO_ROOT / "phone_focus_mode", # strip_workout_hosts
_REPO_ROOT
/ "linux_configuration"
/ "scripts"
/ "single_use"
/ "utils", # fast_count
_REPO_ROOT
/ "linux_configuration"
/ "scripts"
/ "single_use"
/ "misc"
/ "testsAndMisc-bash"
/ "tools", # transcribe_fw and its helpers
)
for _script_dir in _SCRIPT_DIRS:
if str(_script_dir) not in sys.path:
sys.path.insert(0, str(_script_dir))
@pytest.fixture
def fake_whisper() -> type[FakeWhisper]:
"""The faster_whisper double, as a fixture (conftest is not importable)."""
return FakeWhisper
@pytest.fixture
def importer() -> Callable[[dict[str, object]], object]:
"""The ``_try_import`` double factory, as a fixture."""
return fake_importer
class FakeWhisper:
"""Stand-in for faster_whisper, recording how a model was constructed.
``WhisperModel`` is upstream's CamelCase name, so it is exposed through
``__getattr__`` rather than defined as a method: defining it would mean
naming a non-PEP8 identifier here just to satisfy a third-party API.
"""
def __init__(self, error: Exception | None = None) -> None:
self.calls: list[dict[str, object]] = []
self._error = error
def _construct(self, name: str, **kwargs: object) -> object:
if self._error is not None:
raise self._error
self.calls.append({"name": name, **kwargs})
return object()
def __getattr__(self, attr: str) -> object:
if attr == "WhisperModel":
return self._construct
raise AttributeError(attr)
def fake_importer(available: dict[str, object]) -> object:
"""Return a ``_try_import`` double resolving only the named modules."""
def _try(name: str) -> object | None:
return available.get(name)
return _try