Commit graph

80 commits

Author SHA1 Message Date
Paul Gauthier
5dd7287ee3 fixed test for windows 2023-11-14 13:19:57 -08:00
Paul Gauthier
6c77c32d06 aider: Refactored test_check_gitignore to use a temporary git config file and set the GIT_CONFIG_GLOBAL environment variable during the test execution.
# Aider chat conversation:

USER: in `test_check_gitignore` can we have gitpython not use the global gitignore?

ASSISTANT: To ensure that GitPython does not use the global gitignore, we can set the `GIT_CONFIG_GLOBAL` environment variable to point to a non-existent file or an empty temporary file during the test execution. This will effectively ignore the global gitignore settings.

We will modify the `test_check_gitignore` method in the `tests/test_main.py` file to set the `GIT_CONFIG_GLOBAL` environment variable before the test runs and reset it after the test is completed.

Here are the *SEARCH/REPLACE* blocks:

tests/test_main.py
```python
<<<<<<< SEARCH
    def test_check_gitignore(self):
        make_repo()
        io = InputOutput(pretty=False, yes=True)
        cwd = Path.cwd()
        gitignore = cwd / ".gitignore"

        self.assertFalse(gitignore.exists())
        check_gitignore(cwd, io)
        self.assertTrue(gitignore.exists())

        self.assertEqual(".aider*", gitignore.read_text().splitlines()[0])

        gitignore.write_text("one\ntwo\n")
        check_gitignore(cwd, io)
        self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text())
=======
    def test_check_gitignore(self):
        with tempfile.NamedTemporaryFile() as temp_gitconfig:
            os.environ['GIT_CONFIG_GLOBAL'] = temp_gitconfig.name
            make_repo()
            io = InputOutput(pretty=False, yes=True)
            cwd = Path.cwd()
            gitignore = cwd / ".gitignore"

            self.assertFalse(gitignore.exists())
            check_gitignore(cwd, io)
            self.assertTrue(gitignore.exists())

            self.assertEqual(".aider*", gitignore.read_text().splitlines()[0])

            gitignore.write_text("one\ntwo\n")
            check_gitignore(cwd, io)
            self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text())
            del os.environ['GIT_CONFIG_GLOBAL']
>>>>>>> REPLACE
```

This change will create a temporary file to act as a dummy global git configuration file and set the `GIT_CONFIG_GLOBAL` environment variable to its path. After the test, it will delete the environment variable to avoid affecting other tests or operations.
2023-11-14 13:05:47 -08:00
Paul Gauthier
6791bc19c6 added test for --encoding 2023-10-20 08:04:02 -07:00
Paul Gauthier
5c538327c3 update tests 2023-08-19 07:23:00 -07:00
Paul Gauthier
c8f1c0609d tests and bugfix 2023-07-24 17:47:53 -03:00
Paul Gauthier
9f8ea93ce6 AiderRepo -> GitRepo 2023-07-21 16:08:27 -03:00
Paul Gauthier
7c1e3c1b6a cleanup 2023-07-21 16:00:19 -03:00
Paul Gauthier
c4a07f1f84 aider: The real get_commit_message is being replaced with a mock function in the test_main_with_empty_git_dir_new_file test.
# Aider chat conversation:

USER: the real `get_commit_message` is being called in `test_main_with_empty_git_dir_new_file`

ASSISTANT: To prevent the real `get_commit_message` from being called during the test `test_main_with_empty_git_dir_new_file`, we can use the `unittest.mock.patch` decorator to replace it with a mock function.

Here's how to do it:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_with_empty_git_dir_new_file(self):
        make_repo()
        main(["--yes", "foo.txt"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
=======
    @patch("aider.repo.AiderRepo.get_commit_message", return_value="mock commit message")
    def test_main_with_empty_git_dir_new_file(self, _):
        make_repo()
        main(["--yes", "foo.txt"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
>>>>>>> UPDATED
```

This change will replace the `get_commit_message` method with a mock that always returns "mock commit message" during the execution of `test_main_with_empty_git_dir_new_file`.
2023-07-21 15:59:13 -03:00
Paul Gauthier
dfb106d384 aider: Refactor the test_main_with_empty_dir_new_file function in tests/test_main.py to correctly mock the get_commit_message function.
# Aider chat conversation:

USER: I ran this command:

 pytest tests/test_main.py  -k test_main_with_empty_git_dir_new_file

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
configfile: pytest.ini
collected 9 items / 8 deselected / 1 selected

tests/test_main.py F                                                     [100%]

=================================== FAILURES ===================================
________________ TestMain.test_main_with_empty_git_dir_new_file ________________

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

    def test_main_with_empty_git_dir_new_file(self):
        make_repo()
>       main(["--yes", "foo.txt"], input=DummyInput(), output=DummyOutput())

tests/test_main.py:44:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
aider/main.py:452: in main
    coder.dirty_commit()
aider/coders/base_coder.py:850: in dirty_commit
    self.repo.commit(message=message)
aider/repo.py:64: in commit
    commit_message = self.get_commit_message(diffs, context)
aider/repo.py:110: in get_commit_message
    _hash, response = send_with_retries(
.venv/lib/python3.11/site-packages/backoff/_sync.py:105: in retry
    ret = target(*args, **kwargs)
aider/sendchat.py:43: in send_with_retries
    res = openai.ChatCompletion.create(**kwargs)
.venv/lib/python3.11/site-packages/openai/api_resources/chat_completion.py:25: in create
    return super().create(*args, **kwargs)
.venv/lib/python3.11/site-packages/openai/api_resources/abstract/engine_api_resource.py:153: in create
    response, _, api_key = requestor.request(
.venv/lib/python3.11/site-packages/openai/api_requestor.py:298: in request
    resp, got_stream = self._interpret_response(result, stream)
.venv/lib/python3.11/site-packages/openai/api_requestor.py:700: in _interpret_response
    self._interpret_response_line(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <openai.api_requestor.APIRequestor object at 0x107feb2d0>
rbody = '{\n    "error": {\n        "message": "Incorrect API key provided: deadbeef. You can find your API key at https://pla...ys.",\n        "type": "invalid_request_error",\n        "param": null,\n        "code": "invalid_api_key"\n    }\n}\n'
rcode = 401
rheaders = {'Date': 'Fri, 21 Jul 2023 18:57:18 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '258', ...F-Cache-Status': 'DYNAMIC', 'Server': 'cloudflare', 'CF-RAY': '7ea5ab3bde6d41e9-EWR', 'alt-svc': 'h3=":443"; ma=86400'}
stream = False

    def _interpret_response_line(
        self, rbody: str, rcode: int, rheaders, stream: bool
    ) -> OpenAIResponse:
        # HTTP 204 response code does not have any content in the body.
        if rcode == 204:
            return OpenAIResponse(None, rheaders)

        if rcode == 503:
            raise error.ServiceUnavailableError(
                "The server is overloaded or not ready yet.",
                rbody,
                rcode,
                headers=rheaders,
            )
        try:
            if 'text/plain' in rheaders.get('Content-Type', ''):
                data = rbody
            else:
                data = json.loads(rbody)
        except (JSONDecodeError, UnicodeDecodeError) as e:
            raise error.APIError(
                f"HTTP code {rcode} from API ({rbody})", rbody, rcode, headers=rheaders
            ) from e
        resp = OpenAIResponse(data, rheaders)
        # In the future, we might add a "status" parameter to errors
        # to better handle the "error while streaming" case.
        stream_error = stream and "error" in resp.data
        if stream_error or not 200 <= rcode < 300:
>           raise self.handle_error_response(
                rbody, rcode, resp.data, rheaders, stream_error=stream_error
            )
E           openai.error.AuthenticationError: Incorrect API key provided: deadbeef. You can find your API key at https://platform.openai.com/account/api-keys.

.venv/lib/python3.11/site-packages/openai/api_requestor.py:763: AuthenticationError
----------------------------- Captured stdout call -----------------------------
Aider v0.9.1-dev
Add .aider* to .gitignore (recommended)? yes
Added .aider* to .gitignore
Model: gpt-4
Creating empty file foo.txt
Git repo: .git
Repo-map: universal-ctags using 1024 tokens
Added foo.txt to the chat.
Adding foo.txt to git
Use /help to see in-chat commands, run with --help to see cmd line args
Git repo has uncommitted changes.

Commit before the chat proceeds [y/n/commit message]? yes
=========================== short test summary info ============================
FAILED tests/test_main.py::TestMain::test_main_with_empty_git_dir_new_file - ...
======================= 1 failed, 8 deselected in 1.53s ========================

ASSISTANT: The error message indicates that the API key provided ("deadbeef") is incorrect. You need to provide a valid OpenAI API key.

The API key is set in the `setUp
^C KeyboardInterrupt

USER: it's not supposed to be calling the real `get_commit_message`

ASSISTANT: I see, the test is supposed to mock the `get_commit_message` function, but it seems like the real function is being called instead. This could be due to the mock not being applied correctly.

In the `test_main_with_empty_git_dir_new_file` function in the `tests/test_main.py` file, the `get_commit_message` function is being patched. However, it seems like the patch is not being applied correctly.

Let's modify the test to ensure that the `get_commit_message` function is being mocked correctly.

Here is the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    @patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
    def test_main_with_empty_dir_new_file(self, get_commit_message):
        main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
=======
    @patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
    def test_main_with_empty_dir_new_file(self, _):
        main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
>>>>>>> UPDATED
```

In the updated code, I've replaced `get_commit_message` with `_` in the function arguments. This is a common convention in Python when the variable is not going to be used. This change ensures that the mock object is not being confused with the actual function.
2023-07-21 15:58:03 -03:00
Paul Gauthier
6d22dd06f8 aider: Refactored test_main_with_empty_dir_new_file to use @patch decorator to mock get_commit_message method.
# Aider chat conversation:

USER: @patch("aider.repo.AiderRepo.get_commit_message") is not patching it

ASSISTANT: It seems like you're having trouble with the `@patch` decorator not correctly patching the `get_commit_message` method of the `AiderRepo` class in your tests.

To help you with this, I need to understand the context better. Could you please provide more details about the issue? Are you getting any error messages? If so, could you share them?

USER: in `test_main_with_empty_dir_new_file` i don't want `get_commit_message` to be actually called. just to return a mock value

ASSISTANT: The `@patch` decorator from the `unittest.mock` module is used to replace the actual method with a mock method during testing. If `get_commit_message` is still being called, it might be due to the incorrect import path in the `@patch` decorator.

In your case, the `@patch` decorator should be used as follows:

```python
@patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
```

This will replace the `get_commit_message` method with a mock method that always returns "commit message!".

Here is the updated `test_main_with_empty_dir_new_file` method:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    @patch("aider.repo.AiderRepo.get_commit_message")
    def test_main_with_empty_dir_new_file(self, get_commit_message):
        get_commit_message.return_value = "commit message!"
        main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
=======
    @patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
    def test_main_with_empty_dir_new_file(self, get_commit_message):
        main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
>>>>>>> UPDATED
```

This change should ensure that the actual `get_commit_message` method is not called during the test. Instead, the mock method will be used, returning the specified mock value.
2023-07-21 15:56:27 -03:00
Paul Gauthier
180ca7c464 test_main_git_ignore 2023-07-16 12:56:55 -03:00
Paul Gauthier
0f14947a4c test_check_gitignore 2023-07-16 12:49:27 -03:00
Paul Gauthier
1e0443cc86 better test 2023-07-16 12:36:19 -03:00
Paul Gauthier
7d8069d778 test_setup_git works 2023-07-16 12:33:56 -03:00
Paul Gauthier
7096f7287c simplify with make_repo 2023-07-16 12:30:40 -03:00
Paul Gauthier
108abe4a49 aider: Replace all subprocess.run(git calls that are making new repos and setting config with make_repo calls.
# Aider chat conversation:

USER: replace all the `subprocess.run(git` calls that are making new repos and setting config with `make_repo` calls

ASSISTANT: To replace all the `subprocess.run(git` calls that are making new repos and setting config with `make_repo` calls, we need to modify the `tests/test_main.py` file.

Here are the changes:

```python
tests/test_main.py
<<<<<<< ORIGINAL
        subprocess.run(["git", "init"])
        subprocess.run(["git", "config", "user.email", "dummy@example.com"])
        subprocess.run(["git", "config", "user.name", "Dummy User"])
=======
        from tests.utils import make_repo
        make_repo()
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
        subprocess.run(["git", "init"])
        subprocess.run(["git", "config", "user.email", "dummy@example.com"])
        subprocess.run(["git", "config", "user.name", "Dummy User"])
=======
        from tests.utils import make_repo
        make_repo()
>>>>>>> UPDATED
```

```python
tests/test_main.py
<<<<<<< ORIGINAL
        subprocess.run(["git", "init"])
        subprocess.run(["git", "config", "user.email", "dummy@example.com"])
        subprocess.run(["git", "config", "user.name", "Dummy User"])
=======
        from tests.utils import make_repo
        make_repo()
>>>>>>> UPDATED
```

Please note that you need to import the `make_repo` function from `tests.utils` in each test where it's used.
2023-07-16 12:26:31 -03:00
Paul Gauthier
50c8a96edd wip 2023-07-16 08:18:05 -03:00
Paul Gauthier
c8d69f84ce aider: Add test for setup_git function.
# Aider chat conversation:

USER: add tests for `setup_git`

ASSISTANT: To add tests for the `setup_git` function, we need to modify the `tests/test_main.py` file. We will add a new test function `test_setup_git` in the `TestMain` class.

Here is the change:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    def test_main_args(self):
        with patch("aider.main.Coder.create") as MockCoder:
            # --yes will just ok the git repo without blocking on input
            # following calls to main will see the new repo already
            main(["--no-auto-commits", "--yes"], input=DummyInput())
            _, kwargs = MockCoder.call_args
            assert kwargs["auto_commits"] is False
=======
    def test_main_args(self):
        with patch("aider.main.Coder.create") as MockCoder:
            # --yes will just ok the git repo without blocking on input
            # following calls to main will see the new repo already
            main(["--no-auto-commits", "--yes"], input=DummyInput())
            _, kwargs = MockCoder.call_args
            assert kwargs["auto_commits"] is False

    def test_setup_git(self):
        with patch("aider.main.git.Repo.init") as MockRepoInit:
            MockRepoInit.return_value = git.Repo(self.tempdir)
            git_root = setup_git(None, self.io)
            self.assertEqual(git_root, self.tempdir)
            MockRepoInit.assert_called_once_with(self.tempdir)
>>>>>>> UPDATED
```

In the new test `test_setup_git`, we are mocking the `git.Repo.init` function to return a git repository at `self.tempdir`. Then we call `setup_git` with `None` as the `git_root` and check if it returns `self.tempdir` as the new `git_root`. We also check if `git.Repo.init` was called once with `self.tempdir` as the argument.
2023-07-15 13:37:39 -07:00
Paul Gauthier
f98ab4005d fix tests 2023-07-07 11:29:56 -07:00
Paul Gauthier
1654ee422f add test for conf yml 2023-07-07 09:07:02 -07:00
Paul Gauthier
e996c56a51 more robust get_tracked_files with pathlib 2023-07-05 16:55:37 -07:00
Paul Gauthier
c6696e7245 Added a test case to fail if git posix paths are not turned into windows paths 2023-07-05 16:53:43 -07:00
Paul Gauthier
55b1634811 added debug output 2023-07-05 15:08:11 -07:00
Paul Gauthier
f3af05f3ee simpler, faster run just failing test 2023-07-05 14:57:35 -07:00
Paul Gauthier
e107819085 chdir inside test 2023-07-05 14:48:46 -07:00
Paul Gauthier
08230a0262 undo PYTHONIOENCODING 2023-07-05 14:34:07 -07:00
Paul Gauthier
6746da5539 try and stop rich.Console().rule() from throwing unicode errors in github ci 2023-07-05 14:27:41 -07:00
Paul Gauthier
b31dc2ac71 Use DummyInput in tests 2023-07-05 14:20:28 -07:00
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