aider: Added a test to tests/test_main.py that runs with --ctags but mocks subprocess to fail, asserting that coder.use_ctags is false.

# 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`.
This commit is contained in:
Paul Gauthier 2023-05-28 16:59:41 -07:00
parent 130385584a
commit 640479dafb

View file

@ -1,5 +1,6 @@
import os
import tempfile
import subprocess
from unittest import TestCase
from unittest.mock import patch
from aider.main import main
@ -79,3 +80,10 @@ class TestMain(TestCase):
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