mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-22 13:25:00 +00:00

# 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.
198 lines
7.6 KiB
Python
198 lines
7.6 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest import TestCase
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import git
|
|
from prompt_toolkit.input import DummyInput
|
|
from prompt_toolkit.output import DummyOutput
|
|
|
|
from aider.dump import dump # noqa: F401
|
|
from aider.io import InputOutput
|
|
from aider.main import check_gitignore, main, setup_git
|
|
from tests.utils import GitTemporaryDirectory, make_repo
|
|
|
|
|
|
class TestMain(TestCase):
|
|
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.coders.base_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, ignore_errors=True)
|
|
self.patcher.stop()
|
|
|
|
def test_main_with_empty_dir_no_files_on_command(self):
|
|
main(["--no-git"], input=DummyInput(), output=DummyOutput())
|
|
|
|
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"))
|
|
|
|
@patch("aider.repo.GitRepo.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"))
|
|
|
|
@patch("aider.repo.GitRepo.get_commit_message", return_value="mock commit message")
|
|
def test_main_with_empty_git_dir_new_files(self, _):
|
|
make_repo()
|
|
main(["--yes", "foo.txt", "bar.txt"], input=DummyInput(), output=DummyOutput())
|
|
self.assertTrue(os.path.exists("foo.txt"))
|
|
self.assertTrue(os.path.exists("bar.txt"))
|
|
|
|
def test_main_with_dname_and_fname(self):
|
|
subdir = Path("subdir")
|
|
subdir.mkdir()
|
|
make_repo(str(subdir))
|
|
res = main(["subdir", "foo.txt"], input=DummyInput(), output=DummyOutput())
|
|
self.assertNotEqual(res, None)
|
|
|
|
@patch("aider.repo.GitRepo.get_commit_message", return_value="mock commit message")
|
|
def test_main_with_subdir_repo_fnames(self, _):
|
|
subdir = Path("subdir")
|
|
subdir.mkdir()
|
|
make_repo(str(subdir))
|
|
main(
|
|
["--yes", str(subdir / "foo.txt"), str(subdir / "bar.txt")],
|
|
input=DummyInput(),
|
|
output=DummyOutput(),
|
|
)
|
|
self.assertTrue((subdir / "foo.txt").exists())
|
|
self.assertTrue((subdir / "bar.txt").exists())
|
|
|
|
def test_main_with_git_config_yml(self):
|
|
make_repo()
|
|
|
|
Path(".aider.conf.yml").write_text("auto-commits: false\n")
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main(["--yes"], input=DummyInput(), output=DummyOutput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["auto_commits"] is False
|
|
|
|
Path(".aider.conf.yml").write_text("auto-commits: true\n")
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main([], input=DummyInput(), output=DummyOutput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["auto_commits"] is True
|
|
|
|
def test_main_with_empty_git_dir_new_subdir_file(self):
|
|
make_repo()
|
|
subdir = Path("subdir")
|
|
subdir.mkdir()
|
|
fname = subdir / "foo.txt"
|
|
fname.touch()
|
|
subprocess.run(["git", "add", str(subdir)])
|
|
subprocess.run(["git", "commit", "-m", "added"])
|
|
|
|
# This will throw a git error on windows if get_tracked_files doesn't
|
|
# properly convert git/posix/paths to git\posix\paths.
|
|
# Because aider will try and `git add` a file that's already in the repo.
|
|
main(["--yes", str(fname)], input=DummyInput(), output=DummyOutput())
|
|
|
|
def test_setup_git(self):
|
|
io = InputOutput(pretty=False, yes=True)
|
|
git_root = setup_git(None, io)
|
|
git_root = Path(git_root).resolve()
|
|
self.assertEqual(git_root, Path(self.tempdir).resolve())
|
|
|
|
self.assertTrue(git.Repo(self.tempdir))
|
|
|
|
gitignore = Path.cwd() / ".gitignore"
|
|
self.assertTrue(gitignore.exists())
|
|
self.assertEqual(".aider*", gitignore.read_text().splitlines()[0])
|
|
|
|
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']
|
|
|
|
def test_main_git_ignore(self):
|
|
cwd = Path().cwd()
|
|
self.assertFalse((cwd / ".git").exists())
|
|
self.assertFalse((cwd / ".gitignore").exists())
|
|
|
|
with patch("aider.main.Coder.create"):
|
|
main(["--yes"], input=DummyInput())
|
|
|
|
self.assertTrue((cwd / ".git").exists())
|
|
self.assertTrue((cwd / ".gitignore").exists())
|
|
|
|
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
|
|
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main(["--auto-commits"], input=DummyInput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["auto_commits"] is True
|
|
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main([], input=DummyInput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["dirty_commits"] is True
|
|
assert kwargs["auto_commits"] is True
|
|
assert kwargs["pretty"] is True
|
|
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main(["--no-pretty"], input=DummyInput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["pretty"] is False
|
|
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main(["--pretty"], input=DummyInput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["pretty"] is True
|
|
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main(["--no-dirty-commits"], input=DummyInput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["dirty_commits"] is False
|
|
|
|
with patch("aider.main.Coder.create") as MockCoder:
|
|
main(["--dirty-commits"], input=DummyInput())
|
|
_, kwargs = MockCoder.call_args
|
|
assert kwargs["dirty_commits"] is True
|
|
|
|
def test_encodings_arg(self):
|
|
fname = "foo.py"
|
|
|
|
with GitTemporaryDirectory():
|
|
with patch("aider.main.Coder.create") as MockCoder: # noqa: F841
|
|
with patch("aider.main.InputOutput") as MockSend:
|
|
|
|
def side_effect(*args, **kwargs):
|
|
self.assertEqual(kwargs["encoding"], "iso-8859-15")
|
|
return MagicMock()
|
|
|
|
MockSend.side_effect = side_effect
|
|
|
|
main(["--yes", fname, "--encoding", "iso-8859-15"])
|