1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import annotations
import subprocess
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from pygit2 import Repository, init_repository
from pygit2.enums import FileStatus
from pygittools.tui.worktree import (
categorize_status,
commit_staged,
stage_path,
stage_paths,
unstage_path,
unstage_paths,
)
@pytest.fixture
def repo(tmp_path: Path) -> Repository:
repo_path = tmp_path / "repo"
repo_path.mkdir()
return init_repository(str(repo_path), bare=False)
def _init_nested_repo(path: Path) -> None:
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,
)
def test_stage_path_accepts_untracked_directory_trailing_slash(repo: Repository) -> None:
workdir = Path(repo.workdir)
_init_nested_repo(workdir / "nested")
_, _, untracked = categorize_status(repo)
assert untracked == ["nested/"]
stage_path(repo, untracked[0])
staged, _, remaining = categorize_status(repo)
assert staged == ["nested"]
assert remaining == []
def test_stage_paths_accepts_untracked_directory_trailing_slash(repo: Repository) -> None:
workdir = Path(repo.workdir)
_init_nested_repo(workdir / "pkg")
(workdir / "loose.txt").write_text("x\n", encoding="utf-8")
_, _, untracked = categorize_status(repo)
assert set(untracked) == {"pkg/", "loose.txt"}
stage_paths(repo, untracked)
status = repo.status()
assert status["pkg"] & FileStatus.INDEX_NEW
assert status["loose.txt"] & FileStatus.INDEX_NEW
def test_stage_paths_stages_deleted_files(committed_repo: Repository) -> None:
workdir = Path(committed_repo.workdir)
(workdir / "tracked.txt").unlink()
_, unstaged, _ = categorize_status(committed_repo)
assert "tracked.txt" in unstaged
stage_paths(committed_repo, unstaged)
status = committed_repo.status()
assert status["tracked.txt"] & FileStatus.INDEX_DELETED
def test_stage_path_stages_deleted_file(committed_repo: Repository) -> None:
workdir = Path(committed_repo.workdir)
(workdir / "tracked.txt").unlink()
stage_path(committed_repo, "tracked.txt")
status = committed_repo.status()
assert status["tracked.txt"] & FileStatus.INDEX_DELETED
def test_stage_paths_noop_for_empty_list(repo: Repository) -> None:
stage_paths(repo, [])
def test_unstage_tracked_file(committed_repo: Repository) -> None:
stage_path(committed_repo, "tracked.txt")
unstage_path(committed_repo, "tracked.txt")
staged, _, _ = categorize_status(committed_repo)
assert "tracked.txt" not in staged
def test_unstage_new_file_removes_from_index(committed_repo: Repository) -> None:
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
def test_unstage_paths_batch(committed_repo: Repository) -> None:
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
def test_commit_staged_validates_message(committed_repo: Repository) -> None:
ok, error = commit_staged(committed_repo, " ")
assert ok is False
assert error == "Commit message is empty"
def test_commit_staged_requires_staged_files(committed_repo: Repository) -> None:
ok, error = commit_staged(committed_repo, "msg")
assert ok is False
assert error == "Nothing staged to commit"
def test_commit_staged_success(committed_repo: Repository) -> None:
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 == ""
def test_commit_staged_surfaces_git_error(committed_repo: Repository, monkeypatch: pytest.MonkeyPatch) -> None:
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"