| import codecs |
| import os |
| import shutil |
| import sys |
| import tempfile |
| from io import StringIO |
| from pathlib import Path |
| from unittest import TestCase |
|
|
| import git |
|
|
| from aider import models |
| from aider.coders import Coder |
| from aider.commands import Commands |
| from aider.dump import dump |
| from aider.io import InputOutput |
| from tests.utils import GitTemporaryDirectory |
|
|
|
|
| class TestCommands(TestCase): |
| def setUp(self): |
| 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, ignore_errors=True) |
|
|
| def test_cmd_add(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
| from aider.coders import Coder |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| |
| commands.cmd_add("foo.txt bar.txt") |
|
|
| |
| self.assertTrue(os.path.exists("foo.txt")) |
| self.assertTrue(os.path.exists("bar.txt")) |
|
|
| def test_cmd_add_with_glob_patterns(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
| from aider.coders import Coder |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| |
| with open("test1.py", "w") as f: |
| f.write("print('test1')") |
| with open("test2.py", "w") as f: |
| f.write("print('test2')") |
| with open("test.txt", "w") as f: |
| f.write("test") |
|
|
| |
| commands.cmd_add("*.py") |
|
|
| |
| self.assertIn(str(Path("test1.py").resolve()), coder.abs_fnames) |
| self.assertIn(str(Path("test2.py").resolve()), coder.abs_fnames) |
|
|
| |
| self.assertNotIn(str(Path("test.txt").resolve()), coder.abs_fnames) |
|
|
| def test_cmd_add_no_match(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
| from aider.coders import Coder |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| |
| commands.cmd_add("*.nonexistent") |
|
|
| |
| self.assertEqual(len(coder.abs_fnames), 0) |
|
|
| def test_cmd_add_drop_directory(self): |
| |
| io = InputOutput(pretty=False, yes=False) |
| from aider.coders import Coder |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| |
| Path("test_dir").mkdir() |
| Path("test_dir/another_dir").mkdir() |
| Path("test_dir/test_file1.txt").write_text("Test file 1") |
| Path("test_dir/test_file2.txt").write_text("Test file 2") |
| Path("test_dir/another_dir/test_file.txt").write_text("Test file 3") |
|
|
| |
| commands.cmd_add("test_dir test_dir/test_file2.txt") |
|
|
| |
| self.assertIn(str(Path("test_dir/test_file1.txt").resolve()), coder.abs_fnames) |
| self.assertIn(str(Path("test_dir/test_file2.txt").resolve()), coder.abs_fnames) |
| self.assertIn(str(Path("test_dir/another_dir/test_file.txt").resolve()), coder.abs_fnames) |
|
|
| commands.cmd_drop("test_dir/another_dir") |
| self.assertIn(str(Path("test_dir/test_file1.txt").resolve()), coder.abs_fnames) |
| self.assertIn(str(Path("test_dir/test_file2.txt").resolve()), coder.abs_fnames) |
| self.assertNotIn( |
| str(Path("test_dir/another_dir/test_file.txt").resolve()), coder.abs_fnames |
| ) |
|
|
| |
|
|
| |
| abs_fname = str(Path("test_dir/another_dir/test_file.txt").resolve()) |
|
|
| |
| Path("side_dir").mkdir() |
| os.chdir("side_dir") |
|
|
| |
| commands.cmd_add("test_dir/another_dir/test_file.txt") |
|
|
| |
| self.assertIn(abs_fname, coder.abs_fnames) |
|
|
| |
| commands.cmd_drop("test_dir/another_dir/test_file.txt") |
|
|
| |
| self.assertNotIn(abs_fname, coder.abs_fnames) |
|
|
| def test_cmd_drop_with_glob_patterns(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
| from aider.coders import Coder |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| subdir = Path("subdir") |
| subdir.mkdir() |
| (subdir / "subtest1.py").touch() |
| (subdir / "subtest2.py").touch() |
|
|
| Path("test1.py").touch() |
| Path("test2.py").touch() |
|
|
| |
| commands.cmd_add("*.py") |
|
|
| self.assertEqual(len(coder.abs_fnames), 2) |
|
|
| |
| commands.cmd_drop("*2.py") |
|
|
| self.assertIn(str(Path("test1.py").resolve()), coder.abs_fnames) |
| self.assertNotIn(str(Path("test2.py").resolve()), coder.abs_fnames) |
|
|
| def test_cmd_add_bad_encoding(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
| from aider.coders import Coder |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| |
| with codecs.open("foo.bad", "w", encoding="iso-8859-15") as f: |
| f.write("ÆØÅ") |
|
|
| commands.cmd_add("foo.bad") |
|
|
| self.assertEqual(coder.abs_fnames, set()) |
|
|
| def test_cmd_git(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
|
|
| with GitTemporaryDirectory() as tempdir: |
| |
| 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_git("add test.txt") |
| commands.cmd_git("commit -a -m msg") |
|
|
| |
| repo = git.Repo(tempdir) |
| files_in_repo = repo.git.ls_files() |
| self.assertIn("test.txt", files_in_repo) |
|
|
| def test_cmd_tokens(self): |
| |
| io = InputOutput(pretty=False, yes=True) |
|
|
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| commands.cmd_add("foo.txt bar.txt") |
|
|
| |
| stdout = StringIO() |
| sys.stdout = stdout |
|
|
| commands.cmd_tokens("") |
|
|
| |
| sys.stdout = sys.__stdout__ |
|
|
| |
| console_output = stdout.getvalue() |
|
|
| self.assertIn("foo.txt", console_output) |
| self.assertIn("bar.txt", console_output) |
|
|
| def test_cmd_add_from_subdir(self): |
| repo = git.Repo.init() |
| repo.config_writer().set_value("user", "name", "Test User").release() |
| repo.config_writer().set_value("user", "email", "testuser@example.com").release() |
|
|
| |
| filenames = ["one.py", Path("subdir") / "two.py", Path("anotherdir") / "three.py"] |
| for filename in filenames: |
| file_path = Path(filename) |
| file_path.parent.mkdir(parents=True, exist_ok=True) |
| file_path.touch() |
| repo.git.add(str(file_path)) |
| repo.git.commit("-m", "added") |
|
|
| filenames = [str(Path(fn).resolve()) for fn in filenames] |
|
|
| |
|
|
| os.chdir("subdir") |
|
|
| io = InputOutput(pretty=False, yes=True) |
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| |
| commands.cmd_add(str(Path("anotherdir") / "three.py")) |
|
|
| |
| commands.cmd_add("*.py") |
|
|
| self.assertIn(filenames[0], coder.abs_fnames) |
| self.assertNotIn(filenames[1], coder.abs_fnames) |
| self.assertIn(filenames[2], coder.abs_fnames) |
|
|
| def test_cmd_commit(self): |
| with GitTemporaryDirectory(): |
| fname = "test.txt" |
| with open(fname, "w") as f: |
| f.write("test") |
| repo = git.Repo() |
| repo.git.add(fname) |
| repo.git.commit("-m", "initial") |
|
|
| io = InputOutput(pretty=False, yes=True) |
| coder = Coder.create(models.GPT35, None, io) |
| commands = Commands(io, coder) |
|
|
| self.assertFalse(repo.is_dirty()) |
| with open(fname, "w") as f: |
| f.write("new") |
| self.assertTrue(repo.is_dirty()) |
|
|
| commit_message = "Test commit message" |
| commands.cmd_commit(commit_message) |
| self.assertFalse(repo.is_dirty()) |
|
|