Commit graph

52 commits

Author SHA1 Message Date
Paul Gauthier
76eb9e020f more windows test improvements 2023-07-05 14:01:59 -07:00
Paul Gauthier
bbd68097bd tests run on windows now 2023-07-05 13:59:31 -07:00
Paul Gauthier
070f0a2919 refac 2023-06-20 17:46:47 -07:00
Paul Gauthier
f7ccdaa914 fixed tests 2023-06-20 17:31:17 -07:00
Paul Gauthier
2cc87a4afe aider: Added patch to setUp method and stored mock object as an instance variable.
# Context:
USER: how can i move the patch to setUp so that it is active for all tests?
ASSISTANT: To move the patch to the `setUp` method and make it active for all tests, you can use the `patch` as a context manager in the `setUp` method and store the mock object as an instance variable. Then, in the `tearDown` method, you can stop the patch. Here's how to do it:

```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)

    def tearDown(self):
        os.chdir(self.original_cwd)
        shutil.rmtree(self.tempdir)
=======
    def setUp(self):
        os.environ["OPENAI_API_KEY"] = "deadbeef"
        self.original_cwd = os.getcwd()
        self.tempdir = tempfile.mkdtemp()
        os.chdir(self.tempdir)
        self.patcher = patch("aider.main.Coder.check_model_availability")
        self.mock_check = self.patcher.start()
        self.mock_check.return_value = True

    def tearDown(self):
        os.chdir(self.original_cwd)
        shutil.rmtree(self.tempdir)
        self.patcher.stop()
>>>>>>> UPDATED
```

Now the patch is active for all tests, and you can remove the `with` statement from the `test_main_with_empty_dir_no_files_on_command` method:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_empty_dir_no_files_on_command(self):
        with patch("aider.main.Coder.check_model_availability") as mock_check:
            mock_check.return_value = True

            pipe_input = create_input(StringIO(""))
            main([], input=pipe_input, output=DummyOutput())
            pipe_input.close()
=======
    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()
>>>>>>> UPDATED
```
2023-06-01 08:54:15 -07:00
Paul Gauthier
a5414473d9 wip 2023-06-01 08:52:21 -07:00
Paul Gauthier
99693fe614 aider: Modified setUp method to patch check_model_availability with MagicMock object that always returns True.
# Context:
USER: the patch in `setUp` is not correcting forcing a return of true
ASSISTANT: To ensure that the patch in `setUp` correctly forces a return of `True`, you need to modify the `setUp` method in the `TestMain` class in the `tests/test_main.py` file. Here's the change:

```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)

        with patch("aider.main.Coder.check_model_availability") as self.mock_check:
            self.mock_check.return_value = True
        super().setUp()
=======
    def setUp(self):
        os.environ["OPENAI_API_KEY"] = "deadbeef"
        self.original_cwd = os.getcwd()
        self.tempdir = tempfile.mkdtemp()
        os.chdir(self.tempdir)

        self.mock_check = MagicMock(return_value=True)
        with patch("aider.main.Coder.check_model_availability", new=self.mock_check):
            super().setUp()
>>>>>>> UPDATED
```

This change ensures that the `check_model_availability` method is patched with a `MagicMock` object that always returns `True`.
2023-06-01 08:50:55 -07:00
Paul Gauthier
4774f0518c wip 2023-06-01 08:49:55 -07:00
Paul Gauthier
2b07a334cc aider: Set return value of check_model_availability to True during test setup.
# Context:
USER: the patch in `setUp` is not correcting forcing a return of true
ASSISTANT: To fix the patch in `setUp`, you need to start the patch and set the return value to `True`. Here's the change you need to make in the `tests/test_main.py` file:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    self.mock_check = patch("aider.main.Coder.check_model_availability")
    self.mock_check.return_value = True
=======
    with patch("aider.main.Coder.check_model_availability") as self.mock_check:
        self.mock_check.return_value = True
        super().setUp()
>>>>>>> UPDATED
```

This change will ensure that the `check_model_availability` method returns `True` during the test setup.
2023-06-01 08:49:39 -07:00
Paul Gauthier
c97a0ce30a wip 2023-06-01 08:48:55 -07:00
Paul Gauthier
f4bc22e4fc wip 2023-06-01 08:46:42 -07:00
Paul Gauthier
1b49ba6e91 aider: Fixed the setUp method in the TestMain class to correctly mock the check_for_model_availability method.
# Context:
USER: E           AttributeError: <class 'aider.coder.Coder'> does not have the attribute 'check_for_model_availability'
ASSISTANT: It seems like you are trying to access the `check_for_model_availability` method of the `Coder` class, but it is not defined as an instance method. To fix this issue, you need to move the `check_model_availability` method inside the `Coder` class and update the tests accordingly.

Here are the changes you need to make:

1. Move the `
^C KeyboardInterrupt
USER: fix the `setUp`
ASSISTANT: To fix the `setUp` method in the `TestMain` class, you need to update the way you are mocking the `check_for_model_availability` method. Here are the changes you need to make:

1. Modify the `setUp` method in the `TestMain` class to correctly mock the `check_for_model_availability` method of the `Coder` class.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    with patch("aider.coder.Coder.check_for_model_availability") as mock_check:
        mock_check.return_value = True
        self.mock_check = mock_check
=======
    with patch("aider.coder.Coder.check_model_availability") as mock_check:
        mock_check.return_value = True
        self.mock_check = mock_check
>>>>>>> UPDATED
```

This change will ensure that the `check_for_model_availability` method is correctly mocked during the tests.
2023-06-01 08:45:02 -07:00
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
Paul Gauthier
92cd2f3ce6 isort 2023-05-28 22:47:38 -07:00
Paul Gauthier
48e9df2282 move tempdir handling to setup/teardown 2023-05-28 22:42:25 -07:00
Paul Gauthier
e78ea4299f removed test 2023-05-28 17:04:15 -07:00
Paul Gauthier
3b23efa4d8 aider: Changed test_main_with_ctags_mocked_failure to run within a temporary directory.
# Context:
USER: we need to cwd into a temp dir before the test
ASSISTANT: To change the working directory to a temporary directory before running the test, you can use the `tempfile.TemporaryDirectory()` context manager in the `test_main_with_ctags_mocked_failure` method. Here's the updated code for `tests/test_main.py`:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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
=======
    def test_main_with_ctags_mocked_failure(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            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 change will ensure that the test is run within a temporary directory.
2023-05-28 17:01:06 -07:00
Paul Gauthier
640479dafb 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`.
2023-05-28 16:59:41 -07:00
Paul Gauthier
42e4e6b0ca aider: Added dummy values for user.email and user.name after running git init in test_main_with_empty_git_dir_new_file function.
# Context:
USER: after git init, git config user.email and user.name with dummy values
ASSISTANT: I understand that you want to set dummy values for `user.email` and `user.name` after running `git init`. I will make the necessary changes in the `test_main_with_empty_git_dir_new_file` function in the `tests/test_main.py` file.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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_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"))
>>>>>>> UPDATED
```

Now, after running `git init`, the test function will also set dummy values for `user.email` and `user.name`.
2023-05-27 14:06:09 -07:00
Paul Gauthier
0ec5c69fc4 aider: Set OPENAI_API_KEY environment variable to "deadbeef" for all tests.
# Context:
USER: set env OPENAI_API_KEY=deadbeef for all tests
ASSISTANT: To set the environment variable `OPENAI_API_KEY` for all tests, you can modify the `setUp` method in the `TestMain` class in the `tests/test_main.py` file. Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
class TestMain(TestCase):
    def test_main_with_empty_dir_no_files_on_command(self):
=======
class TestMain(TestCase):
    def setUp(self):
        os.environ["OPENAI_API_KEY"] = "deadbeef"

    def test_main_with_empty_dir_no_files_on_command(self):
>>>>>>> UPDATED
```

This will set the `OPENAI_API_KEY` environment variable to "deadbeef" before running each test.
2023-05-27 13:59:48 -07:00
Paul Gauthier
139a141be9 removed dotenv 2023-05-27 06:13:34 -07:00
Paul Gauthier
3e99b4f45c moved test_main_pretty_from_env to end of file 2023-05-27 05:20:10 -07:00
Paul Gauthier
89c412ccfe aider: Added a new test method test_main_pretty_from_env to check if the pretty option is set to False by creating a temporary directory, writing a .env file with AIDER_PRETTY=false, and then calling main with no arguments.
# Context:
USER: add a test like test_main_args. it should test pretty by making a .env file with AIDER_PRETTY=false and then call main with no args.
ASSISTANT: To add a new test for the `pretty` option, you can create a new test method in the `TestMain` class in the `tests/test_main.py` file. Here's the code for the new test:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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
=======
    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

    def test_main_pretty_from_env(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            with open(".env", "w") as env_file:
                env_file.write("AIDER_PRETTY=false\n")

            with patch("aider.main.Coder") as MockCoder:
                main([])
                _, kwargs = MockCoder.call_args
                assert kwargs["pretty"] is False
>>>>>>> UPDATED
```

This new test method, `test_main_pretty_from_env`, creates a temporary directory, writes a `.env` file with `AIDER_PRETTY=false`, and then calls `main` with no arguments. It checks if the `pretty` option is set to `False` as expected.
2023-05-27 05:18:26 -07:00
Paul Gauthier
4c76a8a242 Added --dirty-commits 2023-05-27 05:12:50 -07:00
Paul Gauthier
a24e150e49 more tests, including --pretty 2023-05-27 05:06:36 -07:00
Paul Gauthier
24e78229b2 cleaned up --no-autocommit test 2023-05-27 05:03:30 -07:00
Paul Gauthier
2bb1c34bd5 aider: Updated test_main_with_no_auto_commits to only check if auto_commits is set to False.
# Context:
USER: in test_main_with_empty_dir_new_file can we *just* check the auto_commits=False?
ASSISTANT: Sure, I'll update the `test_main_with_empty_dir_new_file` test to check if `auto_commits` is set to `False`.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["foo.txt"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock
^C KeyboardInterrupt
USER: in test_main_with_no_auto_commits can we *just* check the auto_commits=False?
ASSISTANT: Sure, I'll update the `test_main_with_no_auto_commits` test to only check if `auto_commits` is set to `False`.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,
                    verbose=unittest.mock.ANY,
                )
            pipe_input.close()
=======
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,

^C KeyboardInterrupt
USER: i want to avoid listing all the other args and ANY
ASSISTANT: I understand. I'll update the `test_main_with_no_auto_commits` test to only check if `auto_commits` is set to `False` without listing all the other arguments.

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,
                    verbose=unittest.mock.ANY,
                )
            pipe_input.close()
=======
    def test_main_with_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                _, kwargs = MockCoder.call_args
                assert kwargs["auto_commits"] is False
            pipe_input.close()
>>>>>>> UPDATED
```

This change will make the test only check if `auto_commits` is set to `False` without listing all the other arguments.
2023-05-27 05:00:50 -07:00
Paul Gauthier
01963c28f8 aider: Added a test which runs main with --no-auto-commits and patches Coder with a mock to see that it gets called with auto_commits=False.
# Context:
USER: add a test which runs main with --no-auto-commits and patches Coder with a mock to see that it gets called with auto_commits=False
ASSISTANT: To add the test, you need to modify the `tests/test_main.py` file. Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
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):
=======
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_no_auto_commits(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            pipe_input = create_input(StringIO(""))
            with unittest.mock.patch("aider.main.Coder") as MockCoder:
                main(["--no-auto-commits"], input=pipe_input, output=DummyOutput())
                MockCoder.assert_called_with(
                    unittest.mock.ANY,
                    main_model=unittest.mock.ANY,
                    fnames=unittest.mock.ANY,
                    pretty=unittest.mock.ANY,
                    show_diffs=unittest.mock.ANY,
                    auto_commits=False,
                    dry_run=unittest.mock.ANY,
                    use_ctags=unittest.mock.ANY,
                    verbose=unittest.mock.ANY,
                )
            pipe_input.close()

    def test_main_with_empty_dir_new_file(self):
>>>>>>> UPDATED
```

This new test case `test_main_with_no_auto_commits` creates a temporary directory, changes the current working directory to it, and then runs the `main` function with the `--no-auto-commits` flag. It also patches the `Coder` class with a mock to check if it gets called with `auto_commits=False`.
2023-05-27 04:56:32 -07:00
Paul Gauthier
a9cc332e2d Removed test for disabling auto commits. 2023-05-27 04:54:55 -07:00
Paul Gauthier
ed70a32671 Added import for MagicMock in test_main.py. 2023-05-27 04:39:47 -07:00
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
Paul Gauthier
ca752f37af aider: Import unittest module in tests/test_main.py.
# 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       NameError: name 'unittest' is not defined

tests/test_main.py:37: NameError
=========================== short test summary info ============================
FAILED tests/test_main.py::TestMain::test_main_no_auto_commits - NameError: n...
========================= 1 failed, 3 passed in 1.29s ==========================

ASSISTANT: It looks like the `unittest` module is not imported in the `tests/test_main.py` file. To fix this issue, you need to add the import statement for the `unittest` module at the beginning of the file.

Here's the change:

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

This will import the `unittest` module and resolve the `NameError`.
2023-05-27 04:36:54 -07:00
Paul Gauthier
22482deedf aider: Added a new test to check if the --no-auto-commits flag is passed correctly to the Coder class.
# Context:
USER: add a test which runs main with --no-auto-commits and mocks Coder to see that it gets passed in correctly
ASSISTANT: To add the test, we'll modify the `tests/test_main.py` file. We'll create a new test function called `test_main_no_auto_commits` and use the `unittest.mock.patch` to mock the `Coder` class. Inside the test function, we'll run the `main` function with the `--no-auto-commits` flag and check if the `auto_commits` attribute of the mocked `Coder` instance is set to `False`.

Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    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_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 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)
>>>>>>> UPDATED
```

This will add the new test to check if the `--no-auto-commits` flag is passed correctly to the `Coder` class.
2023-05-27 04:36:14 -07:00
Paul Gauthier
8913eb5066 aider: Removed unused import statement for 'sys' in tests/test_main.py.
# Context:
USER: tests/test_main.py:2:1: F401 'sys' imported but unused
import sys
^

ASSISTANT: To fix the unused import issue, you can remove the import statement for 'sys' in the `tests/test_main.py` file.

```python
tests/test_main.py
<<<<<<< ORIGINAL
import sys
=======
>>>>>>> UPDATED
```
2023-05-17 18:26:43 -07:00
Paul Gauthier
9a413d351d black formatting 2023-05-17 18:24:35 -07:00
Paul Gauthier
de69a3095a aider: Removed all the save_stdin stuff.
# Context:
USER: remove all the save_stdin stuff
ASSISTANT: ```python
tests/test_main.py
<<<<<<< ORIGINAL
            save_stdin = sys.stdin
            sys.stdin = pipe_input
            main([], input=pipe_input, output=DummyOutput())
            sys.stdin = save_stdin
=======
            main([], input=pipe_input, output=DummyOutput())
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            save_stdin = sys.stdin
            sys.stdin = pipe_input
            main(["foo.txt"], input=pipe_input, output=DummyOutput())
            sys.stdin = save_stdin
=======
            main(["foo.txt"], input=pipe_input, output=DummyOutput())
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            save_stdin = sys.stdin
            sys.stdin = pipe_input
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
            sys.stdin = save_stdin
=======
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
>>>>>>> UPDATED
```
2023-05-17 16:13:01 -07:00
Paul Gauthier
4bb043f5c0 aider: Replaced the usage of create_pipe_input() with create_input(StringIO('')) in the test cases.
# Context:
USER: try using create_input(StringIO('')) in the tests
ASSISTANT: I understand that you want to use `create_input(StringIO(''))` in the tests. However, I need more information about which test cases you want to apply this change to and how you want to use it. Please provide more details or examples.
USER: the ones that currently use create_pipe_input()
ASSISTANT: I see that you want to replace `create_pipe_input()` with `create_input(StringIO(''))` in the test cases. Here are the changes for the `tests/test_main.py` file:

```python
tests/test_main.py
<<<<<<< ORIGINAL
from prompt_toolkit.input import create_pipe_input
=======
from prompt_toolkit.input import create_input
from io import StringIO
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            pipe_input = create_pipe_input()
=======
            pipe_input = create_input(StringIO(''))
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            pipe_input = create_pipe_input()
=======
            pipe_input = create_input(StringIO(''))
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            pipe_input = create_pipe_input()
=======
            pipe_input = create_input(StringIO(''))
>>>>>>> UPDATED
```

These changes will replace the usage of `create_pipe_input()` with `create_input(StringIO(''))` in the test cases.
2023-05-13 07:53:28 -07:00
Paul Gauthier
6724fcd897 Added import statement for sys module in test_main.py. 2023-05-13 07:09:50 -07:00
Paul Gauthier
c177e29899 aider: Replaced usage of /dev/null with prompt_toolkit's input mocking functions in tests.
# Context:
USER: instead of using /dev/null to send no output in the main tests, can we use the prompt_toolkit input mocking functions?
ASSISTANT: Yes, we can use the `prompt_toolkit.input` module to mock the input in the tests. Here's how to modify the `tests/test_main.py` file:

```python
tests/test_main.py
<<<<<<< ORIGINAL
import os
import sys
import tempfile
from unittest import TestCase
from aider.main import main
import subprocess
=======
import os
import tempfile
from unittest import TestCase
from aider.main import main
import subprocess
from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.output import DummyOutput
>>>>>>> UPDATED
```

Now, update the tests to use the `create_pipe_input` and `DummyOutput`:

```python
tests/test_main.py
<<<<<<< ORIGINAL
            with open(os.devnull, "r") as dev_null:
                save_stdin = sys.stdin
                sys.stdin = dev_null
                main([])
                sys.stdin = save_stdin
=======
            pipe_input = create_pipe_input()
            save_stdin = sys.stdin
            sys.stdin = pipe_input
            main([], input=pipe_input, output=DummyOutput())
            sys.stdin = save_stdin
            pipe_input.close()
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            with open(os.devnull, "r") as dev_null:
                save_stdin = sys.stdin
                sys.stdin = dev_null
                main(["foo.txt"])
                sys.stdin = save_stdin
                self.assertTrue(os.path.exists("foo.txt"))
=======
            pipe_input = create_pipe_input()
            save_stdin = sys.stdin
            sys.stdin = pipe_input
            main(["foo.txt"], input=pipe_input, output=DummyOutput())
            sys.stdin = save_stdin
            pipe_input.close()
            self.assertTrue(os.path.exists("foo.txt"))
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
            with open(os.devnull, "r") as dev_null:
                save_stdin = sys.stdin
                sys.stdin = dev_null
                main(["--yes", "foo.txt"])
                sys.stdin = save_stdin
                self.assertTrue(os.path.exists("foo.txt"))
=======
            pipe_input = create_pipe_input()
            save_stdin = sys.stdin
            sys.stdin = pipe_input
            main(["--yes", "foo.txt"], input=pipe_input, output=DummyOutput())
            sys.stdin = save_stdin
            pipe_input.close()
            self.assertTrue(os.path.exists("foo.txt"))
>>>>>>> UPDATED
```

These changes will replace the usage of `/dev/null` with `prompt_toolkit`'s input mocking functions.
2023-05-13 07:09:03 -07:00
Paul Gauthier
5e593eb631 handle no args properly 2023-05-11 20:56:09 -07:00
Paul Gauthier
d44f732d3b main should use argv if args not provided 2023-05-11 20:34:46 -07:00
Paul Gauthier
3b0c5132f2 aider: Passed temp_dir as the cwd to subprocess in test_main_with_empty_git_dir_new_file.
# Context:
USER: pass temp_dir as the cwd to subprocess in test_main_with_empty_git_dir_new_file
ASSISTANT: Here's the change to pass `temp_dir` as the `cwd` to `subprocess` in the `test_main_with_empty_git_dir_new_file` function:

```python
tests/test_main.py
<<<<<<< ORIGINAL
            subprocess.run(["git", "init"])
=======
            subprocess.run(["git", "init"], cwd=temp_dir)
>>>>>>> UPDATED
```
2023-05-11 17:52:34 -07:00
Paul Gauthier
8a60462167 finished adding --yes 2023-05-11 17:51:50 -07:00
Paul Gauthier
09fbbc0dae wip: Added import subprocess to test_main.py. 2023-05-11 17:39:15 -07:00
Paul Gauthier
fa2f76c107 aider: Added subprocess module to run git init in the temporary directory before calling main in test_main_with_empty_git_dir_new_file.
# Context:
USER: in test_main_with_empty_git_dir_new_file, run git init in the new dir before calling main
ASSISTANT: To run `git init` in the new directory before calling `main`, you can use the `subprocess` module to execute the command. Here's the change you need to make in the `test_main_with_empty_git_dir_new_file` function:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_empty_git_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            with open(os.devnull, 'r') as dev_null:
                save_stdin = sys.stdin
                sys.stdin = dev_null
                main(['foo.txt'])
                sys.stdin = save_stdin
=======
    import subprocess

    def test_main_with_empty_git_dir_new_file(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            os.chdir(temp_dir)
            subprocess.run(["git", "init"])
            with open(os.devnull, 'r') as dev_null:
                save_stdin = sys.stdin
                sys.stdin = dev_null
                main(['foo.txt'])
                sys.stdin = save_stdin
>>>>>>> UPDATED
```

This change imports the `subprocess` module and adds the `subprocess.run(["git", "init"])` line to run `git init` in the temporary directory before calling `main`.
2023-05-11 17:36:33 -07:00
Paul Gauthier
7a29a2911a aider: Added assertion to confirm that foo.txt exists after main exits in test_main_with_empty_dir_new_file.
# Context:
USER: in test_main_with_empty_dir_new_file, confirm that foo.txt exists after main exists
ASSISTANT: To confirm that `foo.txt` exists after `main` exits, you can add an assertion in the `test_main_with_empty_dir_new_file` test case. Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
                main(['foo.txt'])
                sys.stdin = save_stdin
=======
                main(['foo.txt'])
                sys.stdin = save_stdin
                self.assertTrue(os.path.exists('foo.txt'))
>>>>>>> UPDATED
```
2023-05-11 17:34:35 -07:00
Paul Gauthier
e500f8e37f wip: Refactor main function to include tests for empty directory with no files and new file creation. 2023-05-11 17:33:59 -07:00
Paul Gauthier
00b0369282 aider: Replaced usage of redirect_stdin with closing stdin before calling main.
# Context:
USER: ImportError: cannot import name 'redirect_stdin' from 'contextlib'. Maybe just close stdin before calling main?
ASSISTANT: To fix the ImportError, we can replace the usage of `redirect_stdin` with closing stdin before calling `main`. Here's the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
from contextlib import redirect_stdin
=======
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
    with open(os.devnull, 'r') as dev_null:
        with redirect_stdin(dev_null):
                    main()
=======
    with open(os.devnull, 'r') as dev_null:
        sys.stdin.close()
        main()
>>>>>>> UPDATED
```
2023-05-11 17:26:48 -07:00
Paul Gauthier
ac8376c5bc wip: Changed import from redirect_stdout to redirect_stdin in test_main.py. 2023-05-11 17:25:32 -07:00
Paul Gauthier
fe5e313a75 aider: Replaced redirect_stdout with redirect_stdin and opened os.devnull in read mode ('r') instead of write mode ('w').
# Context:
USER: stdin not stdout
ASSISTANT: I apologize for the confusion. To redirect stdin instead of stdout, we need to use `contextlib.redirect_stdin` instead of `contextlib.redirect_stdout`. Here's the updated code change for tests/test_main.py:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    with open(os.devnull, 'w') as dev_null:
        with redirect_stdout(dev_null):
=======
    with open(os.devnull, 'r') as dev_null:
        with redirect_stdin(dev_null):
>>>>>>> UPDATED
            main()
```

This code change replaces `redirect_stdout` with `redirect_stdin` and opens os.devnull in read mode ('r') instead of write mode ('w').
2023-05-11 17:24:57 -07:00