aider: Add test for setup_git function.

# Aider chat conversation:

USER: add tests for `setup_git`

ASSISTANT: To add tests for the `setup_git` function, we need to modify the `tests/test_main.py` file. We will add a new test function `test_setup_git` in the `TestMain` class.

Here is the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_args(self):
        with patch("aider.main.Coder.create") as MockCoder:
            # --yes will just ok the git repo without blocking on input
            # following calls to main will see the new repo already
            main(["--no-auto-commits", "--yes"], input=DummyInput())
            _, kwargs = MockCoder.call_args
            assert kwargs["auto_commits"] is False
=======
    def test_main_args(self):
        with patch("aider.main.Coder.create") as MockCoder:
            # --yes will just ok the git repo without blocking on input
            # following calls to main will see the new repo already
            main(["--no-auto-commits", "--yes"], input=DummyInput())
            _, kwargs = MockCoder.call_args
            assert kwargs["auto_commits"] is False

    def test_setup_git(self):
        with patch("aider.main.git.Repo.init") as MockRepoInit:
            MockRepoInit.return_value = git.Repo(self.tempdir)
            git_root = setup_git(None, self.io)
            self.assertEqual(git_root, self.tempdir)
            MockRepoInit.assert_called_once_with(self.tempdir)
>>>>>>> UPDATED
```

In the new test `test_setup_git`, we are mocking the `git.Repo.init` function to return a git repository at `self.tempdir`. Then we call `setup_git` with `None` as the `git_root` and check if it returns `self.tempdir` as the new `git_root`. We also check if `git.Repo.init` was called once with `self.tempdir` as the argument.
This commit is contained in:
Paul Gauthier 2023-07-15 13:37:39 -07:00
parent b0ec8205f3
commit c8d69f84ce

View file

@ -83,6 +83,13 @@ class TestMain(TestCase):
_, kwargs = MockCoder.call_args
assert kwargs["auto_commits"] is False
def test_setup_git(self):
with patch("aider.main.git.Repo.init") as MockRepoInit:
MockRepoInit.return_value = git.Repo(self.tempdir)
git_root = setup_git(None, self.io)
self.assertEqual(git_root, self.tempdir)
MockRepoInit.assert_called_once_with(self.tempdir)
with patch("aider.main.Coder.create") as MockCoder:
main(["--auto-commits"], input=DummyInput())
_, kwargs = MockCoder.call_args