|
| 1 | +"""Shared test fixtures — in-memory sources, pre-built configs and snapshots. |
| 2 | +
|
| 3 | +All fixtures use RAM-only data so tests never hit the network or |
| 4 | +create temporary files on disk (unless explicitly needed via tmp_path). |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from datetime import datetime, timezone |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from regix.config import RegressionConfig |
| 15 | +from regix.models import Snapshot, SymbolMetrics |
| 16 | + |
| 17 | + |
| 18 | +# ── Reusable source code snippets ────────────────────────────────────────── |
| 19 | + |
| 20 | +SAMPLE_SOURCE_SIMPLE = '''\ |
| 21 | +import os |
| 22 | +
|
| 23 | +def greet(name): |
| 24 | + """Say hello.""" |
| 25 | + return f"Hello, {name}!" |
| 26 | +
|
| 27 | +class Greeter: |
| 28 | + """A greeter class.""" |
| 29 | + def __init__(self, prefix="Hi"): |
| 30 | + self.prefix = prefix |
| 31 | +
|
| 32 | + def say(self, name): |
| 33 | + """Say greeting.""" |
| 34 | + return f"{self.prefix}, {name}" |
| 35 | +''' |
| 36 | + |
| 37 | +SAMPLE_SOURCE_COMPLEX = '''\ |
| 38 | +import os |
| 39 | +import sys |
| 40 | +from pathlib import Path |
| 41 | +
|
| 42 | +
|
| 43 | +def compute(data, factor=1): |
| 44 | + """Process data with nested logic.""" |
| 45 | + result = [] |
| 46 | + for item in data: |
| 47 | + if item > 0: |
| 48 | + for sub in range(item): |
| 49 | + if sub % 2 == 0: |
| 50 | + result.append(sub * factor) |
| 51 | + else: |
| 52 | + result.append(sub + factor) |
| 53 | + else: |
| 54 | + result.append(0) |
| 55 | + return result |
| 56 | +
|
| 57 | +
|
| 58 | +def transform(items): |
| 59 | + total = 0 |
| 60 | + for item in items: |
| 61 | + total += item |
| 62 | + return total / len(items) if items else 0 |
| 63 | +
|
| 64 | +
|
| 65 | +class Handler: |
| 66 | + def __init__(self, config): |
| 67 | + self.config = config |
| 68 | + self._cache = {} |
| 69 | +
|
| 70 | + def process(self, value): |
| 71 | + if value in self._cache: |
| 72 | + return self._cache[value] |
| 73 | + result = self._compute(value) |
| 74 | + self._cache[value] = result |
| 75 | + return result |
| 76 | +
|
| 77 | + def _compute(self, value): |
| 78 | + return value * 2 + 1 |
| 79 | +''' |
| 80 | + |
| 81 | +SAMPLE_SOURCE_NO_DOCSTRINGS = '''\ |
| 82 | +def undocumented_a(): |
| 83 | + pass |
| 84 | +
|
| 85 | +def undocumented_b(x): |
| 86 | + return x + 1 |
| 87 | +
|
| 88 | +class Bare: |
| 89 | + def method(self): |
| 90 | + pass |
| 91 | +''' |
| 92 | + |
| 93 | + |
| 94 | +# ── Fixtures ─────────────────────────────────────────────────────────────── |
| 95 | + |
| 96 | +@pytest.fixture |
| 97 | +def default_config() -> RegressionConfig: |
| 98 | + """Pre-built default config — no file I/O.""" |
| 99 | + return RegressionConfig() |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def sample_sources() -> dict[str, str]: |
| 104 | + """In-memory source dict with 3 synthetic files.""" |
| 105 | + return { |
| 106 | + "simple.py": SAMPLE_SOURCE_SIMPLE, |
| 107 | + "complex.py": SAMPLE_SOURCE_COMPLEX, |
| 108 | + "bare.py": SAMPLE_SOURCE_NO_DOCSTRINGS, |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | +@pytest.fixture |
| 113 | +def sample_files(sample_sources: dict[str, str]) -> list[Path]: |
| 114 | + """File list matching sample_sources keys.""" |
| 115 | + return [Path(k) for k in sample_sources] |
| 116 | + |
| 117 | + |
| 118 | +@pytest.fixture |
| 119 | +def sample_snapshot(sample_sources: dict[str, str]) -> Snapshot: |
| 120 | + """A Snapshot built entirely in RAM from sample sources.""" |
| 121 | + symbols = [] |
| 122 | + for fname in sample_sources: |
| 123 | + symbols.append(SymbolMetrics(file=fname, symbol=None, cc=5, mi=50.0)) |
| 124 | + return Snapshot( |
| 125 | + ref="HEAD", |
| 126 | + commit_sha="abc1234", |
| 127 | + timestamp=datetime.now(timezone.utc), |
| 128 | + workdir=".", |
| 129 | + symbols=symbols, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.fixture |
| 134 | +def disk_sources(tmp_path: Path, sample_sources: dict[str, str]) -> tuple[Path, list[Path]]: |
| 135 | + """Write sample sources to tmp_path for backends that need disk. |
| 136 | +
|
| 137 | + Returns (workdir, file_list). |
| 138 | + """ |
| 139 | + files: list[Path] = [] |
| 140 | + for name, content in sample_sources.items(): |
| 141 | + p = tmp_path / name |
| 142 | + p.write_text(content, encoding="utf-8") |
| 143 | + files.append(Path(name)) |
| 144 | + return tmp_path, files |
0 commit comments