aider/tests/test_main.py
Paul Gauthier fd70d9d63b aider: Patched check_for_model_availability to always return True during setUp method execution.
# Context:
USER: in setup, patch coder.check_for_model_availability to return true
ASSISTANT: To patch the `check_for_model_availability` method in the `setUp` method of the `TestMain` class, you can use the `unittest.mock.patch` context manager. Here's the change you need to make in the `tests/test_main.py` file:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def setUp(self):
        os.environ["OPENAI_API_KEY"] = "deadbeef"
        self.original_cwd = os.getcwd()
        self.tempdir = tempfile.mkdtemp()
        os.chdir(self.tempdir)
=======
    from unittest.mock import MagicMock

    def setUp(self):
        os.environ["OPENAI_API_KEY"] = "deadbeef"
        self.original_cwd = os.getcwd()
        self.tempdir = tempfile.mkdtemp()
        os.chdir(self.tempdir)

        with patch("aider.coder.Coder.check_for_model_availability") as mock_check:
            mock_check.return_value = True
            self.mock_check = mock_check
>>>>>>> UPDATED
```

This will patch the `check_for_model_availability` method to always return `True` during the `setUp` method execution.
2023-06-01 08:42:18 -07:00

88 lines
3 KiB
Python

import os
import shutil
import subprocess
import tempfile
from io import StringIO
from unittest import TestCase
from unittest.mock import patch
from prompt_toolkit.input import create_input
from prompt_toolkit.output import DummyOutput
from aider.main import main
class TestMain(TestCase):
from unittest.mock import MagicMock
def setUp(self):
os.environ["OPENAI_API_KEY"] = "deadbeef"
self.original_cwd = os.getcwd()
self.tempdir = tempfile.mkdtemp()
os.chdir(self.tempdir)
with patch("aider.coder.Coder.check_for_model_availability") as mock_check:
mock_check.return_value = True
self.mock_check = mock_check
def tearDown(self):
os.chdir(self.original_cwd)
shutil.rmtree(self.tempdir)
def test_main_with_empty_dir_no_files_on_command(self):
pipe_input = create_input(StringIO(""))
main([], input=pipe_input, output=DummyOutput())
pipe_input.close()
def test_main_with_empty_dir_new_file(self):
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):
subprocess.run(["git", "init"])
subprocess.run(["git", "config", "user.email", "dummy@example.com"])
subprocess.run(["git", "config", "user.name", "Dummy User"])
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 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