Project: pygitweb

1f4f601 from __future__ import annotations
import subprocess
from pathlib import Path
47a5dca from unittest.mock import MagicMock
import pytest
from pygit2 import Repository, init_repository
from pygit2.enums import FileStatus
47a5dca from pygittools.tui.worktree import (
73c9c30 categorize_status,
commit_staged,
stage_path,
stage_paths,
unstage_path,
unstage_paths,
47a5dca )
1f4f601
@pytest.fixture
def repo(tmp_path: Path) -> Repository:
73c9c30 repo_path = tmp_path / "repo"
repo_path.mkdir()
return init_repository(str(repo_path), bare=False)
1f4f601
def _init_nested_repo(path: Path) -> None:
73c9c30 path.mkdir()
subprocess.run(["git", "init"], cwd=path, check=True, capture_output=True)
(path / "hello.txt").write_text("hello\n", encoding="utf-8")
subprocess.run(["git", "-C", str(path), "add", "hello.txt"], check=True, capture_output=True)
subprocess.run(
[
"git",
"-C",
str(path),
"-c",
"user.email=alice@example.com",
"-c",
"user.name=alice",
"commit",
"-m",
"init",
],
check=True,
capture_output=True,
)
1f4f601
def test_stage_path_accepts_untracked_directory_trailing_slash(repo: Repository) -> None:
73c9c30 workdir = Path(repo.workdir)
_init_nested_repo(workdir / "nested")
1f4f601
73c9c30 _, _, untracked = categorize_status(repo)
assert untracked == ["nested/"]
1f4f601
73c9c30 stage_path(repo, untracked[0])
1f4f601
73c9c30 staged, _, remaining = categorize_status(repo)
assert staged == ["nested"]
assert remaining == []
1f4f601
def test_stage_paths_accepts_untracked_directory_trailing_slash(repo: Repository) -> None:
73c9c30 workdir = Path(repo.workdir)
_init_nested_repo(workdir / "pkg")
(workdir / "loose.txt").write_text("x\n", encoding="utf-8")
1f4f601
73c9c30 _, _, untracked = categorize_status(repo)
assert set(untracked) == {"pkg/", "loose.txt"}
1f4f601
73c9c30 stage_paths(repo, untracked)
1f4f601
73c9c30 status = repo.status()
assert status["pkg"] & FileStatus.INDEX_NEW
assert status["loose.txt"] & FileStatus.INDEX_NEW
47a5dca
c7ce784 def test_stage_paths_stages_deleted_files(committed_repo: Repository) -> None:
73c9c30 workdir = Path(committed_repo.workdir)
(workdir / "tracked.txt").unlink()
c7ce784
73c9c30 _, unstaged, _ = categorize_status(committed_repo)
assert "tracked.txt" in unstaged
c7ce784
73c9c30 stage_paths(committed_repo, unstaged)
c7ce784
73c9c30 status = committed_repo.status()
assert status["tracked.txt"] & FileStatus.INDEX_DELETED
c7ce784
def test_stage_path_stages_deleted_file(committed_repo: Repository) -> None:
73c9c30 workdir = Path(committed_repo.workdir)
(workdir / "tracked.txt").unlink()
c7ce784
73c9c30 stage_path(committed_repo, "tracked.txt")
c7ce784
73c9c30 status = committed_repo.status()
assert status["tracked.txt"] & FileStatus.INDEX_DELETED
c7ce784
47a5dca def test_stage_paths_noop_for_empty_list(repo: Repository) -> None:
73c9c30 stage_paths(repo, [])
47a5dca
def test_unstage_tracked_file(committed_repo: Repository) -> None:
73c9c30 stage_path(committed_repo, "tracked.txt")
unstage_path(committed_repo, "tracked.txt")
staged, _, _ = categorize_status(committed_repo)
assert "tracked.txt" not in staged
47a5dca
ece926d def test_unstage_new_file_on_unborn_head(repo: Repository) -> None:
73c9c30 path = Path(repo.workdir) / "hello.txt"
path.write_text("hello\n", encoding="utf-8")
stage_path(repo, "hello.txt")
unstage_path(repo, "hello.txt")
staged, _, untracked = categorize_status(repo)
assert staged == []
assert "hello.txt" in untracked
ece926d
47a5dca def test_unstage_new_file_removes_from_index(committed_repo: Repository) -> None:
73c9c30 path = Path(committed_repo.workdir) / "new.txt"
path.write_text("x\n", encoding="utf-8")
stage_path(committed_repo, "new.txt")
unstage_path(committed_repo, "new.txt")
staged, _, untracked = categorize_status(committed_repo)
assert "new.txt" not in staged
assert "new.txt" in untracked
47a5dca
def test_unstage_paths_batch(committed_repo: Repository) -> None:
73c9c30 path = Path(committed_repo.workdir) / "new.txt"
path.write_text("x\n", encoding="utf-8")
stage_path(committed_repo, "new.txt")
unstage_paths(committed_repo, ["new.txt", "tracked.txt"])
staged, unstaged, _ = categorize_status(committed_repo)
assert not staged
47a5dca
def test_commit_staged_validates_message(committed_repo: Repository) -> None:
73c9c30 ok, error = commit_staged(committed_repo, " ")
assert ok is False
assert error == "Commit message is empty"
47a5dca
def test_commit_staged_requires_staged_files(committed_repo: Repository) -> None:
73c9c30 ok, error = commit_staged(committed_repo, "msg")
assert ok is False
assert error == "Nothing staged to commit"
47a5dca
def test_commit_staged_success(committed_repo: Repository) -> None:
73c9c30 path = Path(committed_repo.workdir) / "new.txt"
path.write_text("x\n", encoding="utf-8")
stage_path(committed_repo, "new.txt")
ok, error = commit_staged(committed_repo, "add file")
assert ok is True
assert error == ""
47a5dca
90ea48d def test_commit_amend_success(committed_repo: Repository) -> None:
73c9c30 ok, error = commit_staged(committed_repo, "initial\n\nbody", amend=True)
assert ok is True
assert error == ""
90ea48d
def test_commit_amend_requires_head(repo: Repository) -> None:
73c9c30 ok, error = commit_staged(repo, "msg", amend=True)
assert ok is False
assert error == "Nothing to amend"
90ea48d
47a5dca def test_commit_staged_surfaces_git_error(committed_repo: Repository, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 path = Path(committed_repo.workdir) / "new.txt"
path.write_text("x\n", encoding="utf-8")
stage_path(committed_repo, "new.txt")
result = MagicMock(returncode=1, stderr="", stdout="")
monkeypatch.setattr("pygittools.tui.worktree.subprocess.run", lambda *a, **k: result)
ok, error = commit_staged(committed_repo, "msg")
assert ok is False
assert error == "Commit failed"