|
1 | 1 | import git |
2 | 2 | import os.path as osp |
3 | | -from pathlib import PurePosixPath |
| 3 | +from pathlib import Path, PurePosixPath |
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | from git_draft.bots import Action, Bot, Toolbox |
7 | 7 | import git_draft.drafter as sut |
8 | 8 | from git_draft.store import Store |
9 | 9 |
|
10 | 10 |
|
| 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 | + |
11 | 47 | class _FakeBot(Bot): |
12 | 48 | def act(self, prompt: str, toolbox: Toolbox) -> Action: |
13 | 49 | toolbox.write_file(PurePosixPath("PROMPT"), prompt) |
|
0 commit comments