Skip to content

Commit 8e2a9ab

Browse files
committed
tests: add drafter tests
1 parent 2d08434 commit 8e2a9ab

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist/
22
docs/_*
3+
htmlcov/
34
.coverage

src/git_draft/drafter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __init__(self, repo: git.Repo, hook: OperationHook | None) -> None:
6363
@override
6464
def _list(self) -> Sequence[PurePosixPath]:
6565
# Show staged files.
66-
return self._repo.git.ls_files()
66+
return self._repo.git.ls_files().splitlines()
6767

6868
@override
6969
def _read(self, path: PurePosixPath) -> str:

tests/git_draft/drafter_test.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
11
import git
22
import os.path as osp
3-
from pathlib import PurePosixPath
3+
from pathlib import Path, PurePosixPath
44
import pytest
55

66
from git_draft.bots import Action, Bot, Toolbox
77
import git_draft.drafter as sut
88
from git_draft.store import Store
99

1010

11+
class TestToolbox:
12+
@pytest.fixture(autouse=True)
13+
def setup(self, repo: git.Repo) -> None:
14+
self._toolbox = sut._Toolbox(repo, None)
15+
16+
def test_list_files(self, repo: git.Repo) -> None:
17+
assert self._toolbox.list_files() == []
18+
names = set(["one.txt", "two.txt"])
19+
for name in names:
20+
with open(Path(repo.working_dir, name), "w") as f:
21+
f.write("ok")
22+
repo.git.add(all=True)
23+
assert set(self._toolbox.list_files()) == names
24+
25+
def test_read_file(self, repo: git.Repo) -> None:
26+
with open(Path(repo.working_dir, "one"), "w") as f:
27+
f.write("ok")
28+
29+
path = PurePosixPath("one")
30+
with pytest.raises(git.GitCommandError):
31+
assert self._toolbox.read_file(path) == ""
32+
33+
repo.git.add(all=True)
34+
assert self._toolbox.read_file(path) == "ok"
35+
36+
def test_write_file(self, repo: git.Repo) -> None:
37+
self._toolbox.write_file(PurePosixPath("one"), "hi")
38+
39+
path = Path(repo.working_dir, "one")
40+
assert not path.exists()
41+
42+
repo.git.checkout_index(all=True)
43+
with open(path) as f:
44+
assert f.read() == "hi"
45+
46+
1147
class _FakeBot(Bot):
1248
def act(self, prompt: str, toolbox: Toolbox) -> Action:
1349
toolbox.write_file(PurePosixPath("PROMPT"), prompt)

0 commit comments

Comments
 (0)