|
| 1 | +"""Tests that production runtime dependencies are declared and importable.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import tomllib |
| 9 | +from typing import Final |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +_REPO_ROOT = Path(__file__).resolve().parent.parent |
| 14 | +_PYPROJECT = _REPO_ROOT / "pyproject.toml" |
| 15 | +_REQUIREMENTS = _REPO_ROOT / "requirements.txt" |
| 16 | + |
| 17 | +# Import-time runtime dependencies required for lint_batch_file to work. |
| 18 | +_REQUIRED_RUNTIME_PACKAGES: Final[tuple[str, ...]] = ( |
| 19 | + "charset_normalizer", |
| 20 | + "antlr4-python3-runtime", |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _read_pyproject_dependencies() -> list[str]: |
| 25 | + data = tomllib.loads(_PYPROJECT.read_text(encoding="utf-8")) |
| 26 | + deps = data["project"]["dependencies"] |
| 27 | + assert isinstance(deps, list) |
| 28 | + return [str(dep) for dep in deps] |
| 29 | + |
| 30 | + |
| 31 | +def _read_requirements_packages() -> set[str]: |
| 32 | + packages: set[str] = set() |
| 33 | + for line in _REQUIREMENTS.read_text(encoding="utf-8").splitlines(): |
| 34 | + stripped = line.strip() |
| 35 | + if not stripped or stripped.startswith("#"): |
| 36 | + continue |
| 37 | + packages.add(stripped.split()[0]) |
| 38 | + return packages |
| 39 | + |
| 40 | + |
| 41 | +class TestRuntimeDependencies: |
| 42 | + """Ensure production installs include every import-time dependency.""" |
| 43 | + |
| 44 | + def test_pyproject_declares_required_runtime_packages(self) -> None: |
| 45 | + """pyproject.toml must list all packages needed at import/lint time.""" |
| 46 | + declared = {dep.split("[")[0] for dep in _read_pyproject_dependencies()} |
| 47 | + for package in _REQUIRED_RUNTIME_PACKAGES: |
| 48 | + assert ( |
| 49 | + package in declared |
| 50 | + ), f"{package} missing from pyproject.toml [project].dependencies" |
| 51 | + |
| 52 | + def test_requirements_txt_matches_pyproject_runtime_deps(self) -> None: |
| 53 | + """requirements.txt must mirror pyproject.toml runtime dependencies.""" |
| 54 | + pyproject_deps = {dep.split("[")[0] for dep in _read_pyproject_dependencies()} |
| 55 | + requirements_deps = _read_requirements_packages() |
| 56 | + assert pyproject_deps == requirements_deps |
| 57 | + |
| 58 | + @pytest.mark.slow |
| 59 | + def test_minimal_editable_install_imports_lint_api(self, tmp_path: Path) -> None: |
| 60 | + """pip install -e . without [dev] must expose lint_batch_file.""" |
| 61 | + venv_dir = tmp_path / "minimal-venv" |
| 62 | + subprocess.run( |
| 63 | + [sys.executable, "-m", "venv", str(venv_dir)], |
| 64 | + check=True, |
| 65 | + timeout=120, |
| 66 | + ) |
| 67 | + if sys.platform == "win32": |
| 68 | + python = venv_dir / "Scripts" / "python.exe" |
| 69 | + pip = venv_dir / "Scripts" / "pip.exe" |
| 70 | + else: |
| 71 | + python = venv_dir / "bin" / "python" |
| 72 | + pip = venv_dir / "bin" / "pip" |
| 73 | + |
| 74 | + subprocess.run( |
| 75 | + [str(pip), "install", "-q", "-e", str(_REPO_ROOT)], |
| 76 | + check=True, |
| 77 | + timeout=300, |
| 78 | + ) |
| 79 | + result = subprocess.run( |
| 80 | + [ |
| 81 | + str(python), |
| 82 | + "-c", |
| 83 | + "from blinter import lint_batch_file; print('ok')", |
| 84 | + ], |
| 85 | + check=False, |
| 86 | + capture_output=True, |
| 87 | + text=True, |
| 88 | + timeout=60, |
| 89 | + ) |
| 90 | + assert result.returncode == 0, result.stderr or result.stdout |
0 commit comments