From c8d69f84ce24bc040f818477166dfe2ce411db82 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 15 Jul 2023 13:37:39 -0700 Subject: [PATCH] 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. --- tests/test_main.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index c8fdf9406..3ebc6869c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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