Coverage for tests / scripts / test_extract_version.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Tests for the extract-version utility script.""" 

2 

3from __future__ import annotations 

4 

5import subprocess 

6import sys 

7from pathlib import Path 

8 

9from assertpy import assert_that 

10 

11 

12def run(cmd: list[str], cwd: Path) -> subprocess.CompletedProcess[str]: 

13 """Run a command and capture output for assertions. 

14 

15 Args: 

16 cmd: Command and arguments to execute. 

17 cwd: Working directory for the command. 

18 

19 Returns: 

20 CompletedProcess[str]: Completed process with stdout/stderr. 

21 """ 

22 return subprocess.run(cmd, cwd=cwd, text=True, capture_output=True, check=False) 

23 

24 

25def test_extract_version_from_repo_root(tmp_path: Path) -> None: 

26 """Extract version with default file from repo root copy. 

27 

28 Args: 

29 tmp_path: Temporary directory provided by pytest. 

30 """ 

31 repo_root = Path(__file__).resolve().parents[2] 

32 src = repo_root / "pyproject.toml" 

33 dst = tmp_path / "pyproject.toml" 

34 dst.write_text(src.read_text(encoding="utf-8"), encoding="utf-8") 

35 script = repo_root / "scripts" / "utils" / "extract-version.py" 

36 result = run([sys.executable, str(script)], cwd=tmp_path) 

37 assert_that(result.returncode).is_equal_to(0), result.stderr 

38 assert_that(result.stdout.startswith("version=")).is_true() 

39 assert_that(len(result.stdout.strip().split("=", 1)[1]) > 0).is_true() 

40 

41 

42def test_extract_version_with_custom_file(tmp_path: Path) -> None: 

43 """Extract version when a custom TOML file is provided. 

44 

45 Args: 

46 tmp_path: Temporary directory provided by pytest. 

47 """ 

48 toml = tmp_path / "custom.toml" 

49 toml.write_text('\n[project]\nversion = "9.9.9"\n'.strip(), encoding="utf-8") 

50 repo_root = Path(__file__).resolve().parents[2] 

51 script = repo_root / "scripts" / "utils" / "extract-version.py" 

52 result = run([sys.executable, str(script), "--file", str(toml)], cwd=tmp_path) 

53 assert_that(result.returncode).is_equal_to(0), result.stderr 

54 assert_that(result.stdout.strip()).is_equal_to("version=9.9.9")