From 640479dafb24621598ca08be99eca11c643b093f Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sun, 28 May 2023 16:59:41 -0700 Subject: [PATCH] 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`. --- tests/test_main.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index 481fa987f..1dc4d298d 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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