Commit graph

1119 commits

Author SHA1 Message Date
Paul Gauthier
e74bfc21d0 Merge branch 'main' into refactor-repo 2023-07-21 15:46:31 -03:00
Paul Gauthier
dfd92073f4 Handle files in the git repo which have been deleted but not committed, with test 2023-07-21 15:41:57 -03:00
Paul Gauthier
1fcb090ff6 Refactor coder.get_tracked_files to coder.repo.get_tracked_files in test_coder.py and add test_get_commit_message_no_strip_unmatched_quotes in test_repo.py. 2023-07-21 14:36:26 -03:00
Paul Gauthier
e9090f8d61 Refactored creating a Coder object and getting tracked files in test_repo.py. 2023-07-21 14:32:20 -03:00
Paul Gauthier
b9dc419f19 Refactor test_get_commit_message_strip_quotes to use a mocked send_with_retries function and set the return value of the mocked function. 2023-07-21 14:30:33 -03:00
Paul Gauthier
1733fe4c90 aider: To match the return value of mock_send with commit_message = response.choices[0].message.content, modify the return value of mock_send in the test_get_commit_message function in the tests/test_repo.py file.
# Aider chat conversation:

USER: in `test_get_commit_message`, the mock_send return value needs to match the commit_message = response.choices[0].message.content

ASSISTANT: To match the return value of `mock_send` with `commit_message = response.choices[0].message.content`, we need to modify the return value of `mock_send` in the `test_get_commit_message` function in the `tests/test_repo.py` file.

The `send_with_retries` function returns a tuple where the second element is the response from the OpenAI API. The response is expected to be an object with a `choices` attribute, which is a list of objects, each with a `message` attribute, which in turn has a `content` attribute.

So, we need to return a mock response object that has these attributes. Here's how to do it:

```python
tests/test_repo.py
<<<<<<< ORIGINAL
        mock_send.return_value = (
            None,
            "a good commit message"
        )
=======
        mock_response = MagicMock()
        mock_response.choices = [MagicMock()]
        mock_response.choices[0].message.content = "a good commit message"
        mock_send.return_value = (
            None,
            mock_response
        )
>>>>>>> UPDATED
```

This change will make the `mock_send` function return a mock response object that has the same structure as the actual response from the OpenAI API, allowing the `get_commit_message` method to correctly extract the commit message.
2023-07-21 14:29:15 -03:00
Paul Gauthier
a789de9433 Refactor test_get_commit_message to use the correct import and handle the return value of mock_send correctly. 2023-07-21 14:27:31 -03:00
Paul Gauthier
6f3b498228 aider: Set the return value of the mocked function aider.sendchat.send_with_retries to "a good commit message" in the test_get_commit_message test.
# Aider chat conversation:

USER: do the todo

ASSISTANT: To complete the TODO, we need to mock the return value of the function `aider.sendchat.send_with_retries`. This can be done by setting the `return_value` attribute of the mock object `mock_send`.

Here is the change:

```python
tests/test_repo.py
<<<<<<< ORIGINAL
    @patch("aider.sendchat.send_with_retries")
    def test_get_commit_message(self, mock_send):
        # TODO: make aider.sendchat.send_with_retries return "a good commit message"

        repo = AiderRepo(InputOutput(), None)
        # Call the get_commit_message method with dummy diff and context
        result = repo.get_commit_message("dummy diff", "dummy context")

        # Assert that the returned message is the expected one
        self.assertEqual(result, "a good commit_message")
=======
    @patch("aider.sendchat.send_with_retries")
    def test_get_commit_message(self, mock_send):
        # Set the return value of the mocked function
        mock_send.return_value = "a good commit message"

        repo = AiderRepo(InputOutput(), None)
        # Call the get_commit_message method with dummy diff and context
        result = repo.get_commit_message("dummy diff", "dummy context")

        # Assert that the returned message is the expected one
        self.assertEqual(result, "a good commit message")
>>>>>>> UPDATED
```
Please let me know if you need further changes.
2023-07-21 14:24:49 -03:00
Paul Gauthier
ebd8d9725b wip 2023-07-21 14:24:19 -03:00
Paul Gauthier
28578217da move tests to test_sendchat.py 2023-07-21 14:17:14 -03:00
Paul Gauthier
60e49ae40c added test_replace_part_with_missing_varied_leading_whitespace 2023-07-18 15:13:14 -03:00
Paul Gauthier
77f8be10b1 restore test_cmd_tokens 2023-07-17 16:01:37 -03:00
Paul Gauthier
fd6ceb167c fixed test 2023-07-17 16:00:29 -03:00
Paul Gauthier
8dcff175bc aider: Added a test for cmd_git that creates a temporary git repository, adds a file, runs the cmd_git method with the arguments "commit -a -m msg", and confirms that the file is in the repository.
# Aider chat conversation:

USER: write a test for `cmd_git`. use `GitTemporaryDirectory` to make a temp git repo. add a file. then run `/git commit -a -m msg` and confirm that the file is in the repo

ASSISTANT: To write a test for `cmd_git`, we need to modify the `tests/test_commands.py` file.

Here are the steps we need to follow:

1. Import `GitTemporaryDirectory` from `tests.utils`.
2. Create a new test method `test_cmd_git`.
3. Inside this method, create a temporary git repository using `GitTemporaryDirectory`.
4. Add a file to the repository.
5. Run the `cmd_git` method with the arguments `"commit -a -m msg"`.
6. Check if the file has been committed to the repository.

Here is the *edit block* for these changes:

<source>python
tests/test_commands.py
<<<<<<< ORIGINAL
from unittest import TestCase

from aider import models
from aider.coders import Coder
from aider.commands import Commands
from aider.dump import dump  # noqa: F401
from aider.io import InputOutput
=======
from unittest import TestCase
from tests.utils import GitTemporaryDirectory

from aider import models
from aider.coders import Coder
from aider.commands import Commands
from aider.dump import dump  # noqa: F401
from aider.io import InputOutput
>>>>>>> UPDATED
</source>

<source>python
tests/test_commands.py
<<<<<<< ORIGINAL
    def test_cmd_tokens(self):
        # Initialize the Commands and InputOutput objects
        io = InputOutput(pretty=False, yes=True)

        coder = Coder.create(models.GPT35, None, io)
        commands = Commands(io, coder)

        commands.cmd_add("foo.txt bar.txt")

        # Redirect the standard output to an instance of io.StringIO
        stdout = StringIO()
        sys.stdout = stdout

        commands.cmd_tokens("")

        # Reset the standard output
        sys.stdout = sys.__stdout__

        # Get the console output
        console_output = stdout.getvalue()

        self.assertIn("foo.txt", console_output)
        self.assertIn("bar.txt", console_output)
=======
    def test_cmd_git(self):
        # Initialize the Commands and InputOutput objects
        io = InputOutput(pretty=False, yes=True)

        with GitTemporaryDirectory() as tempdir:
            # Create a file in the temporary directory
            with open(f"{tempdir}/test.txt", "w") as f:
                f.write("test")

            coder = Coder.create(models.GPT35, None, io)
            commands = Commands(io, coder)

            # Run the cmd_git method with the arguments "commit -a -m msg"
            commands.cmd_git("commit -a -m msg")

            # Check if the file has been committed to the repository
            repo = git.Repo(tempdir)
            files_in_repo = [item.a_path for item in repo.index.diff(None)]
            self.assertIn("test.txt", files_in_repo)
>>>>>>> UPDATED
</source>
2023-07-17 15:58:28 -03:00
Paul Gauthier
6f7936e5f8 Merge branch 'main' into gitignore 2023-07-16 18:06:01 -03:00
Paul Gauthier
5dc7bc055e Whitelist more /cmds for no dirty commit 2023-07-16 13:42:36 -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
611e21e457 Merge branch 'main' into gitignore 2023-07-16 12:25:23 -03:00
Paul Gauthier
4447f61dc6 refac make_repo 2023-07-16 12:25:18 -03:00
Paul Gauthier
67554196f3 Merge branch 'main' into gitignore 2023-07-16 12:21:16 -03:00
Paul Gauthier
a9cca05680 Added GitTemporaryDirectory 2023-07-16 09:39:07 -03:00
Paul Gauthier
86cea01434 aider: Change the relative import to an absolute import for the IgnorantTemporaryDirectory class in test_repomap.py.
# Aider chat conversation:

USER: ImportError: Failed to import test module: test_repomap
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.17/x64/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/opt/hostedtoolcache/Python/3.8.17/x64/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/runner/work/aider/aider/tests/test_repomap.py", line 8, in <module>
    from .utils import IgnorantTemporaryDirectory
ImportError: attempted relative import with no known parent package

ASSISTANT: The error message indicates that Python is unable to find the `utils` module when it's being imported in `test_repomap.py`. This is because the import statement is using a relative import with the dot (`.`) prefix, which is not working in this context.

To fix this, we need to change the relative import to an absolute import. This means we need to specify the full path to the `utils` module from the project's root directory.

Here is the change you need to make:

```python
tests/test_repomap.py
<<<<<<< ORIGINAL
from .utils import IgnorantTemporaryDirectory
=======
from tests.utils import IgnorantTemporaryDirectory
>>>>>>> UPDATED
```

This change tells Python to import the `IgnorantTemporaryDirectory` class from the `utils` module located in the `tests` directory, which is an absolute path from the project's root directory.
2023-07-16 09:05:18 -03:00
Paul Gauthier
cdf35cd122 refac 2023-07-16 09:02:35 -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
e6996da018 fix tests for windows 2023-07-14 20:28:33 -07:00
Paul Gauthier
78d9d014f2 fix bug blocking addition of subdir filename mentions 2023-07-14 20:00:04 -07:00
Paul Gauthier
16a21d9844 added failing test for mention 2023-07-14 19:53:49 -07:00
Paul Gauthier
719810b1a1 removed import 2023-07-13 17:47:24 -07:00
Paul Gauthier
b52a62190a Ignore errors when cleaning up temp dirs (Win) 2023-07-13 17:18:09 -07:00
Paul Gauthier
8e2b92febb close the open cache files, so Windows won't error 2023-07-13 16:02:29 -07:00
Paul Gauthier
24015dab5f Try and make test run on windows 2023-07-13 15:55:33 -07:00
Paul Gauthier
f6be208d77 updated tests 2023-07-11 15:17:00 -07:00
Paul Gauthier
bcdf8968f1 updated tests 2023-07-11 15:15:56 -07:00
Paul Gauthier
2190435780 updated tests 2023-07-11 15:15:26 -07:00
paul-gauthier
7e17059b59
Merge pull request #81 from paul-gauthier/issue-73
Prioritize how to allocate code blocks to filenames in wholefile
2023-07-11 09:44:33 -07:00
Paul Gauthier
d22b1dfb32 small improvement to test 2023-07-11 09:40:54 -07:00
Paul Gauthier
1f83a89192 Merge branch 'main' into issue-73 2023-07-11 09:36:34 -07:00
Paul Gauthier
3e3694b38a aider: Set the git user name and email in the get_tracked_files test.
# Aider chat conversation:

USER: set the git user name and email in the `get_tracked_files` test

ASSISTANT: To set the git user name and email in the `get_tracked_files` test, we need to modify the `tests/test_coder.py` file. We will add two lines of code to set the git user name and email after initializing the git repository.

Here is the change:

```python
tests/test_coder.py
<<<<<<< ORIGINAL
        # Initialize a git repository in the temporary directory
        repo = git.Repo.init(tempdir)
=======
        # Initialize a git repository in the temporary directory and set user name and email
        repo = git.Repo.init(tempdir)
        repo.config_writer().set_value("user", "name", "Test User").release()
        repo.config_writer().set_value("user", "email", "testuser@example.com").release()
>>>>>>> UPDATED
```

This change sets the git user name to "Test User" and the email to "testuser@example.com".
2023-07-11 09:20:50 -07:00
Paul Gauthier
8d5f17898a test subdirs too 2023-07-11 09:17:55 -07:00
Paul Gauthier
cccd0d1ebe Make the tests run on windows; added test for files with quotes 2023-07-11 09:15:21 -07:00
Paul Gauthier
68c26a60ef get tracked files by traversing the tree 2023-07-11 09:08:37 -07:00
Paul Gauthier
59a54f5f83 fix Coder.create 2023-07-11 08:21:16 -07:00
Paul Gauthier
cb5d2450ec aider: Ensure parent directory exists before creating file in test_get_tracked_files.
# Aider chat conversation:

USER: I ran this command:

 pytest -k test_get_tracked_files -s

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 61 items / 60 deselected / 1 selected

tests/test_coder.py F

=================================== FAILURES ===================================
_______________________ TestCoder.test_get_tracked_files _______________________

self = <tests.test_coder.TestCoder testMethod=test_get_tracked_files>

    def test_get_tracked_files(self):
        # Create a temporary directory
        tempdir = Path(tempfile.mkdtemp())

        # Initialize a git repository in the temporary directory
        repo = git.Repo.init(tempdir)

        # Create three empty files and add them to the git repository
        filenames = ["README.md", "doc/fänny_dirname/README.md", "doc/systemüberblick.md"]
        for filename in filenames:
>           (tempdir / filename).touch()

tests/test_coder.py:374:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = PosixPath('/var/folders/49/kxrdwwbx0h9bchx99397477c0000gn/T/tmpoowegb9x/doc/fänny_dirname/README.md')
mode = 438, exist_ok = True

    def touch(self, mode=0o666, exist_ok=True):
        """
        Create this file with the given access mode, if it doesn't exist.
        """

        if exist_ok:
            # First try to bump modification time
            # Implementation note: GNU touch uses the UTIME_NOW option of
            # the utimensat() / futimens() functions.
            try:
                os.utime(self, None)
            except OSError:
                # Avoid exception chaining
                pass
            else:
                return
        flags = os.O_CREAT | os.O_WRONLY
        if not exist_ok:
            flags |= os.O_EXCL
>       fd = os.open(self, flags, mode)
E       FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/49/kxrdwwbx0h9bchx99397477c0000gn/T/tmpoowegb9x/doc/fänny_dirname/README.md'

/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py:1108: FileNotFoundError
=========================== short test summary info ============================
FAILED tests/test_coder.py::TestCoder::test_get_tracked_files - FileNotFoundE...
======================= 1 failed, 60 deselected in 1.53s =======================

ASSISTANT: The error message indicates that the directory `doc/fänny_dirname` does not exist when the test tries to create the file `README.md` inside it.

We need to ensure that the directory exists before creating the file. We can use the `mkdir` method of `Path` with the `parents=True` argument to create the directory and all its missing parents.

Here is the updated code:

```python
tests/test_coder.py
<<<<<<< ORIGINAL
    def test_get_tracked_files(self):
        # Create a temporary directory
        tempdir = Path(tempfile.mkdtemp())

        # Initialize a git repository in the temporary directory
        repo = git.Repo.init(tempdir)

        # Create three empty files and add them to the git repository
        filenames = ["README.md", "doc/fänny_dirname/README.md", "doc/systemüberblick.md"]
        for filename in filenames:
            (tempdir / filename).touch()
            repo.git.add(filename)

        # Create a Coder object on the temporary directory
        coder = Coder.create(
            models.GPT4, None, io=InputOutput(), openai_api_key="fake_key", root=tempdir
        )

        # Assert that coder.get_tracked_files() returns the three filenames
        self.assertEqual(set(coder.get_tracked_files()), set(filenames))
=======
    def test_get_tracked_files(self):
        # Create a temporary directory
        tempdir = Path(tempfile.mkdtemp())

        # Initialize a git repository in the temporary directory
        repo = git.Repo.init(tempdir)

        # Create three empty files and add them to the git repository
        filenames = ["README.md", "doc/fänny_dirname/README.md", "doc/systemüberblick.md"]
        for filename in filenames:
            file_path = tempdir / filename
            file_path.parent.mkdir(parents=True, exist_ok=True)
            file_path.touch()
            repo.git.add(str(file_path))

        # Create a Coder object on the temporary directory
        coder = Coder.create(
            models.GPT4, None, io=InputOutput(), openai_api_key="fake_key", root=tempdir
        )

        # Assert that coder.get_tracked_files() returns the three filenames
        self.assertEqual(set(coder.get_tracked_files()), set(filenames))
>>>>>>> UPDATED
```

This code creates the parent directory of each file before creating the file. It also converts the `Path` object to a string before passing it to `repo.git.add`, because the `git` library may not accept `Path` objects.
2023-07-11 08:20:22 -07:00
Paul Gauthier
db8fb97a38 added import 2023-07-11 08:19:24 -07:00