Project: pygitweb

97c4910 """Tests for project label and commit log helpers."""
from __future__ import annotations
47a5dca import json
97c4910 from pathlib import Path
965624d from unittest.mock import MagicMock, patch
import pygit2
47a5dca import pytest
8614508 from pyratatui import Rect
97c4910
8614508 from pygittools.tui.keys import key
from pygittools.tui.log_list import CommitLogList
97c4910 from pygittools.tui.project_label import repo_project_label
fc2f2ce SIG = pygit2.Signature("t", "t@example.com", 0)
97c4910
92f699c def test_repo_project_label_falls_back_to_worktree(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 settings = tmp_path / "settings.json"
settings.write_text(json.dumps({"PROJECTROOT": str(tmp_path / "projects")}), encoding="utf-8")
monkeypatch.setenv("PYGITWEB_SETTINGS_CONFIG", str(settings))
repo_dir = tmp_path / "standalone"
repo_dir.mkdir()
pygit2.init_repository(str(repo_dir), bare=False)
assert repo_project_label(repo_dir) == str(repo_dir.resolve())
97c4910
92f699c def test_repo_project_label_uses_worktree_for_git_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 settings = tmp_path / "settings.json"
settings.write_text(json.dumps({"PROJECTROOT": str(tmp_path / "projects")}), encoding="utf-8")
monkeypatch.setenv("PYGITWEB_SETTINGS_CONFIG", str(settings))
repo_dir = tmp_path / "pygitweb"
repo_dir.mkdir()
repo = pygit2.init_repository(str(repo_dir), bare=False)
assert repo_project_label(Path(repo.path)) == str(repo_dir.resolve())
97c4910
def test_commit_log_list_loads_head_commit(tmp_path: Path) -> None:
73c9c30 repo = pygit2.init_repository(str(tmp_path / "repo"), bare=False)
tree = repo.index.write_tree()
repo.create_commit("HEAD", SIG, SIG, "feat: first", tree, [])
log = CommitLogList()
log.refresh(repo)
assert str(repo.head.target)[:7] in log.status_hint()
47a5dca
def test_commit_log_list_empty_repo(repo: pygit2.Repository) -> None:
73c9c30 log = CommitLogList()
log.refresh(repo)
assert log.status_hint() == "No commits — arrows move"
log.draw(MagicMock(), Rect(0, 0, 80, 5), highlight=True)
47a5dca
def test_commit_log_list_navigation(tmp_path: Path) -> None:
73c9c30 repo = pygit2.init_repository(str(tmp_path / "repo"), bare=False)
tree = repo.index.write_tree()
repo.create_commit("HEAD", SIG, SIG, "one", tree, [])
log = CommitLogList()
log.refresh(repo)
log.handle_nav_key(key("Down"), repo)
log.handle_nav_key(key("Up"), repo)
with patch.object(log, "refresh") as refresh:
log.handle_nav_key(key("x"), repo)
refresh.assert_not_called()
47a5dca
def test_load_entries_without_head(repo: pygit2.Repository) -> None:
73c9c30 from pygit2 import init_repository
fc2f2ce
73c9c30 from pygittools.tui.log_list import _load_entries
fc2f2ce
73c9c30 empty = init_repository(str(Path(repo.workdir).parent / "bare"), bare=True)
assert _load_entries(empty) == []
47a5dca
def test_repo_project_label_under_projectroot(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 monkeypatch.delenv("PYGITWEB_PROJECTROOT", raising=False)
root = tmp_path / "root"
project = root / "demo"
project.mkdir(parents=True)
pygit2.init_repository(str(project), bare=False)
settings = tmp_path / "settings.json"
settings.write_text(json.dumps({"PROJECTROOT": str(root)}), encoding="utf-8")
monkeypatch.setenv("PYGITWEB_SETTINGS_CONFIG", str(settings))
assert repo_project_label(project) == "demo"
47a5dca
def test_repo_project_label_oserror_falls_back(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 repo_dir = tmp_path / "standalone"
repo_dir.mkdir()
pygit2.init_repository(str(repo_dir), bare=False)
monkeypatch.setattr(
"pygittools.tui.project_label.load_pygitweb_layout",
lambda: (_ for _ in ()).throw(OSError("missing")),
)
assert repo_project_label(repo_dir) == str(repo_dir.resolve())
47a5dca
@pytest.fixture
def repo(tmp_path: Path) -> pygit2.Repository:
73c9c30 repo_path = tmp_path / "repo"
repo_path.mkdir()
return pygit2.init_repository(str(repo_path), bare=False)