from importlib.metadata import PackageNotFoundError from pathlib import Path import pytest import arm_statements.version as version_module from arm_statements.version import statements_version FULL_HASH = "abc1234def5678abc1234def5678abc1234def56" def test_version_absent_file_is_none(tmp_path: Path) -> None: assert statements_version(tmp_path / "VERSION") is None def test_version_read_from_file(tmp_path: Path) -> None: marker = tmp_path / "VERSION" marker.write_text(f"{FULL_HASH}\n", encoding="utf-8") assert statements_version(marker) == FULL_HASH def test_version_empty_file_is_none(tmp_path: Path) -> None: marker = tmp_path / "VERSION" marker.write_text(" \n", encoding="utf-8") assert statements_version(marker) is None @pytest.mark.parametrize("contents", [None, " \n"]) def test_default_marker_falls_back_to_distribution( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, contents: str | None ) -> None: marker = tmp_path / "VERSION" if contents is not None: marker.write_text(contents, encoding="utf-8") monkeypatch.setattr(version_module, "_DEFAULT_MARKER", marker) monkeypatch.setattr(version_module, "version", lambda name: "0.1.1" if name == "pylib-ai-arm-statements" else None) assert statements_version() == "0.1.1" def test_default_marker_takes_priority(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: marker = tmp_path / "VERSION" marker.write_text(f" {FULL_HASH}\n", encoding="utf-8") monkeypatch.setattr(version_module, "_DEFAULT_MARKER", marker) def unexpected_metadata_lookup(name: str) -> str: raise AssertionError(f"Unexpected metadata lookup: {name}") monkeypatch.setattr(version_module, "version", unexpected_metadata_lookup) assert statements_version() == FULL_HASH def test_default_version_without_metadata_is_none(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(version_module, "_DEFAULT_MARKER", tmp_path / "VERSION") def missing_distribution(name: str) -> str: raise PackageNotFoundError(name) monkeypatch.setattr(version_module, "version", missing_distribution) assert statements_version() is None @pytest.mark.parametrize("contents", [None, " \n"]) def test_explicit_marker_does_not_use_distribution( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, contents: str | None ) -> None: marker = tmp_path / "VERSION" if contents is not None: marker.write_text(contents, encoding="utf-8") def unexpected_metadata_lookup(name: str) -> str: raise AssertionError(f"Unexpected metadata lookup: {name}") monkeypatch.setattr(version_module, "version", unexpected_metadata_lookup) assert statements_version(marker) is None