|
| 1 | +# tests/conftest.py |
| 2 | +""" |
| 3 | +Shared test fixture for pocket-build. |
| 4 | +
|
| 5 | +This lets all tests transparently run against both: |
| 6 | +1. The modular package (`src/pocket_build`) |
| 7 | +2. The bundled single-file script (`bin/pocket-build.py`) |
| 8 | +
|
| 9 | +Each test receives a `pocket_build_env` fixture that behaves like the module. |
| 10 | +
|
| 11 | +If the bundled script is missing or older than the source files, |
| 12 | +it is automatically rebuilt using `dev/make_script.py`. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import importlib.util |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | +from typing import Any, Dict, Generator, List, Optional, Protocol |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +# Ensure the src/ folder is on sys.path (so "import pocket_build" works) |
| 26 | +ROOT = Path(__file__).resolve().parent.parent |
| 27 | +SRC_DIR = ROOT / "src" |
| 28 | +if SRC_DIR.exists(): |
| 29 | + sys.path.insert(0, str(SRC_DIR)) |
| 30 | + |
| 31 | +# ruff: noqa: E402 — import after sys.path modification |
| 32 | +from pocket_build.types import BuildConfig |
| 33 | + |
| 34 | +# --- Prevent pytest from scanning build output folders --- |
| 35 | +# These directories can contain auto-generated code or duplicated tests. |
| 36 | +BAD_DIRS = ["dist", "tmp-dist", "bin"] |
| 37 | + |
| 38 | +for bad in BAD_DIRS: |
| 39 | + bad_path = Path(bad).resolve() |
| 40 | + sys.path = [p for p in sys.path if bad not in p] |
| 41 | + |
| 42 | + |
| 43 | +# ------------------------------------------------------------ |
| 44 | +# 🧩 Protocol for type safety & editor autocompletion |
| 45 | +# ------------------------------------------------------------ |
| 46 | +class PocketBuildLike(Protocol): |
| 47 | + """Subset of functions shared by both implementations.""" |
| 48 | + |
| 49 | + # --- utils --- |
| 50 | + def load_jsonc(self, path: Path) -> Dict[str, Any]: ... |
| 51 | + def is_excluded( |
| 52 | + self, |
| 53 | + path: Path, |
| 54 | + exclude_patterns: List[str], |
| 55 | + root: Path, |
| 56 | + ) -> bool: ... |
| 57 | + |
| 58 | + # --- config --- |
| 59 | + def parse_builds(self, raw_config: Dict[str, Any]) -> List[BuildConfig]: ... |
| 60 | + |
| 61 | + # --- build --- |
| 62 | + def copy_file(self, src: Path, dest: Path, root: Path) -> None: ... |
| 63 | + def copy_directory( |
| 64 | + self, |
| 65 | + src: Path, |
| 66 | + dest: Path, |
| 67 | + exclude_patterns: List[str], |
| 68 | + root: Path, |
| 69 | + ) -> None: ... |
| 70 | + def copy_item( |
| 71 | + self, |
| 72 | + src: Path, |
| 73 | + dest: Path, |
| 74 | + exclude_patterns: List[str], |
| 75 | + root: Path, |
| 76 | + ) -> None: ... |
| 77 | + def run_build( |
| 78 | + self, |
| 79 | + build_cfg: BuildConfig, # ✅ use the proper TypedDict |
| 80 | + config_dir: Path, |
| 81 | + out_override: Optional[str], |
| 82 | + ) -> None: ... |
| 83 | + |
| 84 | + # --- CLI --- |
| 85 | + def main(self, argv: Optional[List[str]] = None) -> int: ... |
| 86 | + |
| 87 | + |
| 88 | +def pytest_ignore_collect(collection_path: Path, config): # type: ignore[override] |
| 89 | + """ |
| 90 | + Hook called by pytest for each discovered path. |
| 91 | + Returning True tells pytest to skip collecting tests from it. |
| 92 | + """ |
| 93 | + for bad in BAD_DIRS: |
| 94 | + if bad in str(collection_path): |
| 95 | + return True |
| 96 | + return False |
| 97 | + |
| 98 | + |
| 99 | +# ------------------------------------------------------------ |
| 100 | +# ⚙️ Auto-build helper for bundled script |
| 101 | +# ------------------------------------------------------------ |
| 102 | +def ensure_bundled_script_up_to_date(root: Path) -> Path: |
| 103 | + """Rebuild `bin/pocket-build.py` if missing or older than source files.""" |
| 104 | + bin_path = root / "bin" / "pocket-build.py" |
| 105 | + src_dir = root / "src" / "pocket_build" |
| 106 | + builder = root / "dev" / "make_script.py" |
| 107 | + |
| 108 | + # If the output file doesn't exist or is older than any source file → rebuild. |
| 109 | + needs_rebuild = not bin_path.exists() |
| 110 | + if not needs_rebuild: |
| 111 | + bin_mtime = bin_path.stat().st_mtime |
| 112 | + for src_file in src_dir.rglob("*.py"): |
| 113 | + if src_file.stat().st_mtime > bin_mtime: |
| 114 | + needs_rebuild = True |
| 115 | + break |
| 116 | + |
| 117 | + if needs_rebuild: |
| 118 | + print("⚙️ Rebuilding single-file bundle (make_script.py)...") |
| 119 | + subprocess.run([sys.executable, str(builder)], check=True) |
| 120 | + assert bin_path.exists(), "❌ Failed to generate bundled script." |
| 121 | + |
| 122 | + return bin_path |
| 123 | + |
| 124 | + |
| 125 | +# ------------------------------------------------------------ |
| 126 | +# 🔁 Fixture: load either the package or the bundled script |
| 127 | +# ------------------------------------------------------------ |
| 128 | +@pytest.fixture(scope="session", params=["module", "singlefile"]) |
| 129 | +def pocket_build_env( |
| 130 | + request: pytest.FixtureRequest, |
| 131 | +) -> Generator[PocketBuildLike, None, None]: |
| 132 | + """Yield a loaded pocket_build environment (module or bundled single-file).""" |
| 133 | + root = Path(__file__).resolve().parent.parent |
| 134 | + |
| 135 | + if request.param == "module": |
| 136 | + import pocket_build as mod |
| 137 | + |
| 138 | + yield mod # type: ignore[return-value] |
| 139 | + |
| 140 | + else: |
| 141 | + bin_path = ensure_bundled_script_up_to_date(root) |
| 142 | + |
| 143 | + spec = importlib.util.spec_from_file_location("pocket_build_single", bin_path) |
| 144 | + assert spec and spec.loader, f"Failed to load spec from {bin_path}" |
| 145 | + mod = importlib.util.module_from_spec(spec) |
| 146 | + sys.modules["pocket_build_single"] = mod |
| 147 | + spec.loader.exec_module(mod) |
| 148 | + yield mod # type: ignore[return-value] |
0 commit comments