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>
This commit is contained in:
Paul Gauthier 2023-07-17 15:58:28 -03:00
parent c05efad274
commit 8dcff175bc

View file

@ -6,6 +6,7 @@ import tempfile
from io import StringIO
from pathlib import Path
from unittest import TestCase
from tests.utils import GitTemporaryDirectory
from aider import models
from aider.coders import Coder
@ -155,26 +156,22 @@ class TestCommands(TestCase):
self.assertEqual(coder.abs_fnames, set())
def test_cmd_tokens(self):
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)
commands.cmd_add("foo.txt bar.txt")
# Run the cmd_git method with the arguments "commit -a -m msg"
commands.cmd_git("commit -a -m msg")
# 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)
# 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)