aider/tests/test_main.py
Paul Gauthier 10dc4f6557 aider: Replaced unittest.mock with MagicMock in test_main_no_auto_commits.
# Context:
USER: I ran this command:

 pytest tests/test_main.py

And got this output:

============================= test session starts ==============================
platform darwin -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/gauthier/Projects/aider
collected 4 items

tests/test_main.py F...                                                  [100%]

=================================== FAILURES ===================================
______________________ TestMain.test_main_no_auto_commits ______________________

self = <tests.test_main.TestMain testMethod=test_main_no_auto_commits>

    def test_main_no_auto_commits(self):
>       with unittest.mock.patch("aider.main.Coder") as MockCoder:
E       AttributeError: module 'unittest' has no attribute 'mock'

tests/test_main.py:38: AttributeError
=========================== short test summary info ============================
FAILED tests/test_main.py::TestMain::test_main_no_auto_commits - AttributeErr...
========================= 1 failed, 3 passed in 1.32s ==========================

ASSISTANT: Apologies for the oversight. It seems that the `unittest.mock` module is not available in your Python version. You can use the `mock` module instead, which is available as a separate package. First, you need to install the `mock` package using pip:

```
pip install mock
```

Then, update the import statement in the `tests/test_main.py` file to use the `mock` package:

```python
tests/test_main.py
<<<<<<< ORIGINAL
import os
import tempfile
import unittest
from unittest import TestCase
from aider.main import main
=======

^C KeyboardInterrupt
USER: would magicmock work?
ASSISTANT: Yes, `MagicMock` can be used in this case. It is a part of the `unittest.mock` module in Python 3.3 and later. Since you are using Python 3.11.3, you can use `MagicMock` directly from the `unittest.mock` module.

First, update the import statement in the `tests/test_main.py` file to import `MagicMock`:

```python
tests/test_main.py
<<<<<<< ORIGINAL
import os
import tempfile
import unittest
from unittest import TestCase
from aider.main import main
=======
import os
import tempfile
import unittest
from unittest import TestCase, MagicMock
from aider.main import main
>>>>>>> UPDATED
```

Then, update the test function `test_main_no_auto_commits` to use `MagicMock` instead of `unittest.mock.patch`:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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)
=======
    def test_main_no_auto_commits(self):
        with MagicMock() 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 should resolve the issue with the test.
2023-05-27 04:38:32 -07:00

47 lines
1.8 KiB
Python

import os
import tempfile
import unittest
from unittest import TestCase, 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_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 MagicMock() 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)