mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-21 04:44:59 +00:00

# Context: USER: add a test which runs main with --no-auto-commits and patches Coder with a mock to see that it gets called with auto_commits=False ASSISTANT: To add the test, you need to modify the `tests/test_main.py` file. Here's the change: ```python tests/test_main.py <<<<<<< ORIGINAL 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): ======= 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_no_auto_commits(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir) pipe_input = create_input(StringIO("")) with unittest.mock.patch("aider.main.Coder") as MockCoder: main(["--no-auto-commits"], input=pipe_input, output=DummyOutput()) MockCoder.assert_called_with( unittest.mock.ANY, main_model=unittest.mock.ANY, fnames=unittest.mock.ANY, pretty=unittest.mock.ANY, show_diffs=unittest.mock.ANY, auto_commits=False, dry_run=unittest.mock.ANY, use_ctags=unittest.mock.ANY, verbose=unittest.mock.ANY, ) pipe_input.close() def test_main_with_empty_dir_new_file(self): >>>>>>> UPDATED ``` This new test case `test_main_with_no_auto_commits` creates a temporary directory, changes the current working directory to it, and then runs the `main` function with the `--no-auto-commits` flag. It also patches the `Coder` class with a mock to check if it gets called with `auto_commits=False`.
55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
from unittest import TestCase
|
|
from unittest.mock import MagicMock
|
|
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_no_auto_commits(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
os.chdir(temp_dir)
|
|
pipe_input = create_input(StringIO(""))
|
|
with unittest.mock.patch("aider.main.Coder") as MockCoder:
|
|
main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
|
|
MockCoder.assert_called_with(
|
|
unittest.mock.ANY,
|
|
main_model=unittest.mock.ANY,
|
|
fnames=unittest.mock.ANY,
|
|
pretty=unittest.mock.ANY,
|
|
show_diffs=unittest.mock.ANY,
|
|
auto_commits=False,
|
|
dry_run=unittest.mock.ANY,
|
|
use_ctags=unittest.mock.ANY,
|
|
verbose=unittest.mock.ANY,
|
|
)
|
|
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"))
|