main
py 81 lines 2.31 KB
Raw
1 import subprocess
2 import sys
3 import types
4 from pathlib import Path
5
6
7 PROJECT_ROOT = Path(__file__).resolve().parents[1]
8 if str(PROJECT_ROOT) not in sys.path:
9 sys.path.insert(0, str(PROJECT_ROOT))
10
11 sys.modules["giturlparse"] = types.SimpleNamespace(
12 parse=lambda *args, **kwargs: types.SimpleNamespace(
13 owner="",
14 repo="",
15 name="",
16 valid=False,
17 )
18 )
19
20 from helpers import git
21
22
23 def run_git(repo_dir: Path, *args: str) -> str:
24 completed = subprocess.run(
25 ["git", "-C", str(repo_dir), *args],
26 check=True,
27 text=True,
28 capture_output=True,
29 )
30 return completed.stdout.strip()
31
32
33 def init_repo_with_tag(repo_dir: Path, branch: str) -> None:
34 run_git(repo_dir, "init")
35 run_git(repo_dir, "branch", "-m", branch)
36 run_git(repo_dir, "config", "user.name", "Test User")
37 run_git(repo_dir, "config", "user.email", "test@example.com")
38 (repo_dir / "tracked.txt").write_text("one\n", encoding="utf-8")
39 run_git(repo_dir, "add", "tracked.txt")
40 run_git(repo_dir, "commit", "-m", "initial")
41 run_git(repo_dir, "tag", "v1.9")
42
43
44 def add_commit(repo_dir: Path, content: str) -> None:
45 (repo_dir / "tracked.txt").write_text(content, encoding="utf-8")
46 run_git(repo_dir, "add", "tracked.txt")
47 run_git(repo_dir, "commit", "-m", "update")
48
49
50 def test_git_timestamp_is_utc_without_a_timezone_suffix():
51 assert git._format_git_timestamp(0) == "1970-01-01 00:00:00"
52
53
54 def test_sidebar_version_timestamp_stays_on_one_line():
55 sidebar_bottom = (
56 PROJECT_ROOT / "webui/components/sidebar/bottom/sidebar-bottom.html"
57 ).read_text(encoding="utf-8")
58
59 assert "white-space: nowrap;" in sidebar_bottom
60
61
62 def test_git_version_label_shows_commit_distance_on_development(tmp_path):
63 init_repo_with_tag(tmp_path, "development")
64 add_commit(tmp_path, "two\n")
65
66 info = git.get_repo_release_info(str(tmp_path))
67
68 assert info.release is not None
69 assert info.release.short_tag == "v1.9"
70 assert info.release.version == "D v1.9+1"
71
72
73 def test_git_version_label_hides_commit_distance_on_main(tmp_path):
74 init_repo_with_tag(tmp_path, "main")
75 add_commit(tmp_path, "two\n")
76
77 info = git.get_repo_release_info(str(tmp_path))
78
79 assert info.release is not None
80 assert info.release.short_tag == "v1.9"
81 assert info.release.version == "M v1.9"