|
| 1 | +"""Tests for PyPI packaging: version consistency and isolated installation.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import venv |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +pytestmark = pytest.mark.slow |
| 12 | + |
| 13 | + |
| 14 | +def _project_root() -> Path: |
| 15 | + """Return the repository root (contains pyproject.toml).""" |
| 16 | + root = Path(__file__).resolve().parent.parent |
| 17 | + assert (root / "pyproject.toml").exists() |
| 18 | + return root |
| 19 | + |
| 20 | + |
| 21 | +class TestVersionConsistency: |
| 22 | + """Ensure the package exposes a consistent version.""" |
| 23 | + |
| 24 | + def test_version_is_string(self) -> None: |
| 25 | + from pyfuse.core.version import _VERSION |
| 26 | + |
| 27 | + assert isinstance(_VERSION, str) |
| 28 | + assert _VERSION # non-empty |
| 29 | + |
| 30 | + def test_init_exports_version(self) -> None: |
| 31 | + import pyfuse |
| 32 | + |
| 33 | + assert hasattr(pyfuse, "__version__") |
| 34 | + assert isinstance(pyfuse.__version__, str) |
| 35 | + assert pyfuse.__version__ # non-empty |
| 36 | + |
| 37 | + def test_pyproject_matches_fallback(self) -> None: |
| 38 | + """The fallback version in version.py must match pyproject.toml.""" |
| 39 | + try: |
| 40 | + import tomllib |
| 41 | + except ModuleNotFoundError: |
| 42 | + import tomli as tomllib # type: ignore[no-redef] |
| 43 | + |
| 44 | + from pyfuse.core.version import _FALLBACK_VERSION |
| 45 | + |
| 46 | + pyproject = _project_root() / "pyproject.toml" |
| 47 | + with open(pyproject, "rb") as f: |
| 48 | + data = tomllib.load(f) |
| 49 | + assert data["project"]["version"] == _FALLBACK_VERSION |
| 50 | + |
| 51 | + |
| 52 | +class TestIsolatedInstallation: |
| 53 | + """Install pyfuse in a temporary venv and verify it works.""" |
| 54 | + |
| 55 | + def test_install_and_import_no_extras(self, tmp_path: Path) -> None: |
| 56 | + """Package installs and imports without optional dependencies.""" |
| 57 | + venv_dir = tmp_path / "venv" |
| 58 | + venv.create(str(venv_dir), with_pip=True) |
| 59 | + |
| 60 | + if sys.platform == "win32": |
| 61 | + python = str(venv_dir / "Scripts" / "python.exe") |
| 62 | + else: |
| 63 | + python = str(venv_dir / "bin" / "python") |
| 64 | + |
| 65 | + root = _project_root() |
| 66 | + |
| 67 | + # Install the package (no extras) |
| 68 | + result = subprocess.run( |
| 69 | + [python, "-m", "pip", "install", str(root), "--quiet"], |
| 70 | + capture_output=True, |
| 71 | + text=True, |
| 72 | + timeout=120, |
| 73 | + ) |
| 74 | + assert result.returncode == 0, f"pip install failed:\n{result.stderr}" |
| 75 | + |
| 76 | + # Verify import works |
| 77 | + result = subprocess.run( |
| 78 | + [ |
| 79 | + python, "-c", |
| 80 | + "import pyfuse; " |
| 81 | + "print(pyfuse.__version__); " |
| 82 | + "print(pyfuse.serialize); " |
| 83 | + "print(pyfuse.trace); " |
| 84 | + "print(pyfuse.get_graph()); " |
| 85 | + ], |
| 86 | + capture_output=True, |
| 87 | + text=True, |
| 88 | + timeout=30, |
| 89 | + ) |
| 90 | + assert result.returncode == 0, ( |
| 91 | + f"Import failed:\nstdout: {result.stdout}\nstderr: {result.stderr}" |
| 92 | + ) |
| 93 | + |
| 94 | + lines = result.stdout.strip().splitlines() |
| 95 | + # First line is the version |
| 96 | + assert lines[0] # non-empty version string |
| 97 | + |
| 98 | + def test_install_version_matches(self, tmp_path: Path) -> None: |
| 99 | + """Installed package version matches pyproject.toml.""" |
| 100 | + try: |
| 101 | + import tomllib |
| 102 | + except ModuleNotFoundError: |
| 103 | + import tomli as tomllib # type: ignore[no-redef] |
| 104 | + |
| 105 | + venv_dir = tmp_path / "venv" |
| 106 | + venv.create(str(venv_dir), with_pip=True) |
| 107 | + |
| 108 | + if sys.platform == "win32": |
| 109 | + python = str(venv_dir / "Scripts" / "python.exe") |
| 110 | + else: |
| 111 | + python = str(venv_dir / "bin" / "python") |
| 112 | + |
| 113 | + root = _project_root() |
| 114 | + |
| 115 | + pyproject = root / "pyproject.toml" |
| 116 | + with open(pyproject, "rb") as f: |
| 117 | + expected_version = tomllib.load(f)["project"]["version"] |
| 118 | + |
| 119 | + # Install |
| 120 | + result = subprocess.run( |
| 121 | + [python, "-m", "pip", "install", str(root), "--quiet"], |
| 122 | + capture_output=True, |
| 123 | + text=True, |
| 124 | + timeout=120, |
| 125 | + ) |
| 126 | + assert result.returncode == 0, f"pip install failed:\n{result.stderr}" |
| 127 | + |
| 128 | + # Check version |
| 129 | + result = subprocess.run( |
| 130 | + [python, "-c", "import pyfuse; print(pyfuse.__version__)"], |
| 131 | + capture_output=True, |
| 132 | + text=True, |
| 133 | + timeout=30, |
| 134 | + ) |
| 135 | + assert result.returncode == 0 |
| 136 | + assert result.stdout.strip() == expected_version |
| 137 | + |
| 138 | + def test_cli_entrypoint(self, tmp_path: Path) -> None: |
| 139 | + """The ``pyfuse`` CLI entry point is installed and functional.""" |
| 140 | + venv_dir = tmp_path / "venv" |
| 141 | + venv.create(str(venv_dir), with_pip=True) |
| 142 | + |
| 143 | + if sys.platform == "win32": |
| 144 | + python = str(venv_dir / "Scripts" / "python.exe") |
| 145 | + else: |
| 146 | + python = str(venv_dir / "bin" / "python") |
| 147 | + |
| 148 | + root = _project_root() |
| 149 | + |
| 150 | + result = subprocess.run( |
| 151 | + [python, "-m", "pip", "install", str(root), "--quiet"], |
| 152 | + capture_output=True, |
| 153 | + text=True, |
| 154 | + timeout=120, |
| 155 | + ) |
| 156 | + assert result.returncode == 0, f"pip install failed:\n{result.stderr}" |
| 157 | + |
| 158 | + # Run ``pyfuse info`` via the entry point |
| 159 | + result = subprocess.run( |
| 160 | + [python, "-m", "pyfuse", "info"], |
| 161 | + capture_output=True, |
| 162 | + text=True, |
| 163 | + timeout=30, |
| 164 | + ) |
| 165 | + assert result.returncode == 0 |
| 166 | + assert "pyfuse" in result.stdout |
0 commit comments