|
| 1 | +import importlib |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | + |
| 5 | +def test_get_version_uses_package_metadata(monkeypatch): |
| 6 | + # Force VERSION file to be treated as missing |
| 7 | + orig_exists = Path.exists |
| 8 | + |
| 9 | + def fake_exists(self): # type: ignore[no-redef] |
| 10 | + return False if self.name == "VERSION" else orig_exists(self) |
| 11 | + |
| 12 | + monkeypatch.setattr(Path, "exists", fake_exists, raising=False) |
| 13 | + |
| 14 | + # Patch importlib.metadata.version to return a known value |
| 15 | + import importlib.metadata as im |
| 16 | + |
| 17 | + monkeypatch.setattr(im, "version", lambda name: "9.9.9") |
| 18 | + |
| 19 | + from app import _version as ver |
| 20 | + |
| 21 | + assert ver.get_version() == "9.9.9" |
| 22 | + |
| 23 | + |
| 24 | +def test_get_version_fallback_to_default(monkeypatch): |
| 25 | + # Force VERSION file to be treated as missing |
| 26 | + orig_exists = Path.exists |
| 27 | + |
| 28 | + def fake_exists(self): # type: ignore[no-redef] |
| 29 | + return False if self.name == "VERSION" else orig_exists(self) |
| 30 | + |
| 31 | + monkeypatch.setattr(Path, "exists", fake_exists, raising=False) |
| 32 | + |
| 33 | + # Patch importlib.metadata.version to raise, triggering default fallback |
| 34 | + import importlib.metadata as im |
| 35 | + |
| 36 | + def raise_err(_): |
| 37 | + raise RuntimeError("no package metadata") |
| 38 | + |
| 39 | + monkeypatch.setattr(im, "version", raise_err) |
| 40 | + |
| 41 | + from app import _version as ver |
| 42 | + |
| 43 | + assert ver.get_version() == "0.0.0" |
| 44 | + |
0 commit comments