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

# Context: USER: add a test to tests/test_main.py that runs with --ctags but mocks subprocess to fail. assert that coder.use_ctags is false ASSISTANT: To add a test that runs with `--ctags` but mocks `subprocess` to fail, you can follow these steps: 1. Import `subprocess` at the beginning of the `tests/test_main.py` file. 2. Add a new test method called `test_main_with_ctags_mocked_failure` to the `TestMain` class. 3. In the new test method, use the `unittest.mock.patch` context manager to mock the `subprocess.run` function to raise a `subprocess.CalledProcessError` exception. 4. Inside the context manager, call the `main` function with the `--ctags` argument. 5. Assert that `coder.use_ctags` is `False`. Here's the code change for `tests/test_main.py`: ```python tests/test_main.py <<<<<<< ORIGINAL import os import tempfile from unittest import TestCase from unittest.mock import patch from aider.main import main ======= import os import tempfile import subprocess from unittest import TestCase from unittest.mock import patch from aider.main import main >>>>>>> UPDATED ``` ```python tests/test_main.py <<<<<<< ORIGINAL with patch("aider.main.Coder") as MockCoder: main(["--dirty-commits"]) _, kwargs = MockCoder.call_args assert kwargs["dirty_commits"] is True ======= with patch("aider.main.Coder") as MockCoder: main(["--dirty-commits"]) _, kwargs = MockCoder.call_args assert kwargs["dirty_commits"] is True def test_main_with_ctags_mocked_failure(self): with patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, "ctags")): with patch("aider.main.Coder") as MockCoder: main(["--ctags"]) _, kwargs = MockCoder.call_args assert kwargs["use_ctags"] is False >>>>>>> UPDATED ``` This will add the requested test to `tests/test_main.py`.
89 lines
3.5 KiB
Python
89 lines
3.5 KiB
Python
import os
|
|
import tempfile
|
|
import subprocess
|
|
from unittest import TestCase
|
|
from unittest.mock import patch
|
|
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 setUp(self):
|
|
os.environ["OPENAI_API_KEY"] = "deadbeef"
|
|
|
|
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)
|
|
subprocess.run(["git", "config", "user.email", "dummy@example.com"], cwd=temp_dir)
|
|
subprocess.run(["git", "config", "user.name", "Dummy User"], 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_args(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
os.chdir(temp_dir)
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--no-auto-commits"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["auto_commits"] is False
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--auto-commits"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["auto_commits"] is True
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main([])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["dirty_commits"] is True
|
|
assert kwargs["auto_commits"] is True
|
|
assert kwargs["pretty"] is True
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--no-pretty"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["pretty"] is False
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--pretty"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["pretty"] is True
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--no-dirty-commits"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["dirty_commits"] is False
|
|
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--dirty-commits"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["dirty_commits"] is True
|
|
|
|
def test_main_with_ctags_mocked_failure(self):
|
|
with patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, "ctags")):
|
|
with patch("aider.main.Coder") as MockCoder:
|
|
main(["--ctags"])
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["use_ctags"] is False
|