|
| 1 | +from typing import Optional |
| 2 | +from pathlib import Path |
| 3 | +import subprocess |
| 4 | +import pytest |
| 5 | +import re |
| 6 | + |
| 7 | +# DEFINE PATHS RELATIVE TO THIS TEST FILE tests/test_version.py |
| 8 | +ROOT_DIR = Path(__file__).parent.parent |
| 9 | +PYPROJECT_PATH = ROOT_DIR / "pyproject.toml" |
| 10 | +INIT_PATH = ROOT_DIR / "src" / "xulbux" / "__init__.py" |
| 11 | + |
| 12 | + |
| 13 | +def get_current_branch() -> Optional[str]: |
| 14 | + try: |
| 15 | + result = subprocess.run(["git", "branch", "--show-current"], capture_output=True, text=True, check=True) |
| 16 | + return result.stdout.strip() |
| 17 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 18 | + return None |
| 19 | + |
| 20 | + |
| 21 | +def get_file_version(file_path: Path, pattern: str) -> Optional[str]: |
| 22 | + if not file_path.exists(): |
| 23 | + return None |
| 24 | + |
| 25 | + with open(file_path, "r", encoding="utf-8") as f: |
| 26 | + content = f.read() |
| 27 | + match = re.search(pattern, content, re.MULTILINE) |
| 28 | + if match: |
| 29 | + return match.group(1) |
| 30 | + |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +################################################## VERSION CONSISTENCY TEST ################################################## |
| 35 | + |
| 36 | + |
| 37 | +def test_version_consistency(): |
| 38 | + """Verifies that the version numbers in `pyproject.toml` and `__init__.py` |
| 39 | + match the version specified in the current release branch name (`dev/1.X.Y`).""" |
| 40 | + # SKIP IF WE CAN'T DETERMINE THE BRANCH (DETACHED HEAD OR NOT A GIT REPO) |
| 41 | + if not (branch_name := get_current_branch()): |
| 42 | + pytest.skip("Could not determine git branch name") |
| 43 | + |
| 44 | + # SKIP IF BRANCH NAME DOESN'T MATCH RELEASE PATTERN dev/1.X.Y |
| 45 | + if not (branch_match := re.match(r"^dev/(1\.[0-9]+\.[0-9]+)$", branch_name)): |
| 46 | + pytest.skip(f"Current branch '{branch_name}' is not a release branch (dev/1.X.Y)") |
| 47 | + |
| 48 | + expected_version = branch_match.group(1) |
| 49 | + |
| 50 | + # EXTRACT VERSIONS |
| 51 | + pyproject_version = get_file_version(PYPROJECT_PATH, r'^version\s*=\s*"([^"]+)"') |
| 52 | + init_version = get_file_version(INIT_PATH, r'^__version__\s*=\s*"([^"]+)"') |
| 53 | + |
| 54 | + assert pyproject_version is not None, f"Could not find var 'version' in {PYPROJECT_PATH}" |
| 55 | + assert init_version is not None, f"Could not find var '__version__' in {INIT_PATH}" |
| 56 | + |
| 57 | + assert pyproject_version == expected_version, \ |
| 58 | + f"Hardcoded lib-version in pyproject.toml ({pyproject_version}) does not match branch version ({expected_version})" |
| 59 | + |
| 60 | + assert init_version == expected_version, \ |
| 61 | + f"Hardcoded lib-version in src/xulbux/__init__.py ({init_version}) does not match branch version ({expected_version})" |
0 commit comments