aider/tests/test_main.py
Paul Gauthier 22482deedf aider: Added a new test to check if the --no-auto-commits flag is passed correctly to the Coder class.
# Context:
USER: add a test which runs main with --no-auto-commits and mocks Coder to see that it gets passed in correctly
ASSISTANT: To add the test, we'll modify the `tests/test_main.py` file. We'll create a new test function called `test_main_no_auto_commits` and use the `unittest.mock.patch` to mock the `Coder` class. Inside the test function, we'll run the `main` function with the `--no-auto-commits` flag and check if the `auto_commits` attribute of the mocked `Coder` instance is set to `False`.

Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_empty_git_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            subprocess.run(["git", "init"], cwd=temp_dir)
            pipe_input = create_input(StringIO(""))
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
            pipe_input.close()
            self.assertTrue(os.path.exists("foo.txt"))
=======
    def test_main_with_empty_git_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            subprocess.run(["git", "init"], cwd=temp_dir)
            pipe_input = create_input(StringIO(""))
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
            pipe_input.close()
            self.assertTrue(os.path.exists("foo.txt"))

    def test_main_no_auto_commits(self):
        with unittest.mock.patch("aider.main.Coder") as MockCoder:
            with tempfile.TemporaryDirectory() as temp_dir:
                os.chdir(temp_dir)
                subprocess.run(["git", "init"], cwd=temp_dir)
                pipe_input = create_input(StringIO(""))
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                pipe_input.close()

            mock_coder_instance = MockCoder.return_value
            self.assertFalse(mock_coder_instance.auto_commits)
>>>>>>> UPDATED
```

This will add the new test to check if the `--no-auto-commits` flag is passed correctly to the `Coder` class.
2023-05-27 04:36:14 -07:00

46 lines
1.8 KiB
Python

import os
import tempfile
from unittest import TestCase
from aider.main import main
import subprocess
from prompt_toolkit.input import create_input
from io import StringIO
from prompt_toolkit.output import DummyOutput
class TestMain(TestCase):
def test_main_with_empty_dir_no_files_on_command(self):
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
pipe_input = create_input(StringIO(""))
main([], input=pipe_input, output=DummyOutput())
pipe_input.close()
def test_main_with_empty_dir_new_file(self):
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
pipe_input = create_input(StringIO(""))
main(["foo.txt"], input=pipe_input, output=DummyOutput())
pipe_input.close()
self.assertTrue(os.path.exists("foo.txt"))
def test_main_with_empty_git_dir_new_file(self):
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
subprocess.run(["git", "init"], cwd=temp_dir)
pipe_input = create_input(StringIO(""))
main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
pipe_input.close()
self.assertTrue(os.path.exists("foo.txt"))
def test_main_no_auto_commits(self):
with unittest.mock.patch("aider.main.Coder") as MockCoder:
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
subprocess.run(["git", "init"], cwd=temp_dir)
pipe_input = create_input(StringIO(""))
main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
pipe_input.close()
mock_coder_instance = MockCoder.return_value
self.assertFalse(mock_coder_instance.auto_commits)