aider/tests/test_coder.py
Paul Gauthier 0fc40df8e6 aider: Replaced manual setting of abs_fnames with a mocked git.Repo object that has its ls_files method return the desired file paths.
# Context:
USER: don't set the files in abs_fnames. patch self.repo.git.ls_files to return them
ASSISTANT: To patch `self.repo.git.ls_files` to return the files in `abs_fnames`, we will modify the `test_check_for_file_mentions` function in the `tests/test_coder.py` file.

```python
tests/test_coder.py
<<<<<<< ORIGINAL
        # Initialize the Coder object with the mocked IO
        coder = Coder(io=mock_io, openai_api_key="fake_key")

        # Add sample files to the Coder object
        coder.abs_fnames = {
            "/path/to/repo/file1.txt",
            "/path/to/repo/file2.py",
        }
        coder.root = "/path/to/repo"
=======
        # Mock the git repo
        mock_repo = MagicMock()
        mock_repo.git.ls_files.return_value = "file1.txt\nfile2.py"

        # Initialize the Coder object with the mocked IO and mocked repo
        coder = Coder(io=mock_io, openai_api_key="fake_key", repo=mock_repo)
        coder.root = "/path/to/repo"
>>>>>>> UPDATED
```

This change replaces the manual setting of `abs_fnames` with a mocked `git.Repo` object that has its `ls_files` method return the desired file paths.
2023-05-27 08:26:16 -07:00

27 lines
966 B
Python

import unittest
from unittest.mock import MagicMock
from aider.coder import Coder
class TestCoder(unittest.TestCase):
def test_check_for_file_mentions(self):
# Mock the IO object
mock_io = MagicMock()
mock_io.get_input.return_value = "Please check file1.txt and file2.py"
mock_io.confirm_ask.return_value = True
# Mock the git repo
mock_repo = MagicMock()
mock_repo.git.ls_files.return_value = "file1.txt\nfile2.py"
# Initialize the Coder object with the mocked IO and mocked repo
coder = Coder(io=mock_io, openai_api_key="fake_key", repo=mock_repo)
coder.root = "/path/to/repo"
# Call the check_for_file_mentions method
result = coder.check_for_file_mentions("Please check file1.txt and file2.py")
# Check if the result is as expected
self.assertEqual(result, "Added files: file1.txt, file2.py")
if __name__ == "__main__":
unittest.main()