From bf6cd8294e92ddfc5c4533a18ac0fca20ef1b022 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 15 Nov 2023 13:34:29 -0800 Subject: [PATCH] handle submodules (or other dirs) which are git tracked #336 --- aider/coders/base_coder.py | 1 + aider/io.py | 3 ++ tests/test_coder.py | 71 ++++++++++++++------------------------ 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index d418c85df..8774a4863 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -728,6 +728,7 @@ class Coder: else: files = self.get_inchat_relative_files() + files = [fname for fname in files if Path(self.abs_root_path(fname)).is_file()] return sorted(set(files)) def get_all_abs_files(self): diff --git a/aider/io.py b/aider/io.py index 2da220812..1da68e83b 100644 --- a/aider/io.py +++ b/aider/io.py @@ -146,6 +146,9 @@ class InputOutput: except FileNotFoundError: self.tool_error(f"{filename}: file not found error") return + except IsADirectoryError: + self.tool_error(f"{filename}: is a directory") + return except UnicodeError as e: self.tool_error(f"{filename}: {e}") self.tool_error("Use --encoding to set the unicode encoding.") diff --git a/tests/test_coder.py b/tests/test_coder.py index 6d333be07..3a0995605 100644 --- a/tests/test_coder.py +++ b/tests/test_coder.py @@ -122,33 +122,6 @@ class TestCoder(unittest.TestCase): fname.unlink() self.assertEqual(coder.get_last_modified(), 0) - def test_check_for_file_mentions(self): - # Mock the IO object - mock_io = MagicMock() - - # Initialize the Coder object with the mocked IO and mocked repo - coder = Coder.create(models.GPT4, None, mock_io) - - # Mock the git repo - mock = MagicMock() - mock.return_value = set(["file1.txt", "file2.py"]) - coder.repo.get_tracked_files = mock - - # Call the check_for_file_mentions method - coder.check_for_file_mentions("Please check file1.txt and file2.py") - - # Check if coder.abs_fnames contains both files - expected_files = set( - map( - str, - [ - Path(coder.root) / "file1.txt", - Path(coder.root) / "file2.py", - ], - ) - ) - self.assertEqual(coder.abs_fnames, expected_files) - def test_get_files_content(self): tempdir = Path(tempfile.mkdtemp()) @@ -167,31 +140,37 @@ class TestCoder(unittest.TestCase): self.assertIn("file1.txt", content) self.assertIn("file2.txt", content) - def test_check_for_filename_mentions_of_longer_paths(self): - # Mock the IO object - mock_io = MagicMock() + def test_check_for_filename_mentions(self): + with GitTemporaryDirectory(): + repo = git.Repo() - # Initialize the Coder object with the mocked IO and mocked repo - coder = Coder.create(models.GPT4, None, mock_io) + mock_io = MagicMock() - mock = MagicMock() - mock.return_value = set(["file1.txt", "file2.py"]) - coder.repo.get_tracked_files = mock + fname1 = Path("file1.txt") + fname2 = Path("file2.py") - # Call the check_for_file_mentions method - coder.check_for_file_mentions("Please check file1.txt and file2.py") + fname1.write_text("one\n") + fname2.write_text("two\n") - # Check if coder.abs_fnames contains both files - expected_files = set( - map( - str, + repo.git.add(str(fname1)) + repo.git.add(str(fname2)) + repo.git.commit("-m", "new") + + # Initialize the Coder object with the mocked IO and mocked repo + coder = Coder.create(models.GPT4, None, mock_io) + + # Call the check_for_file_mentions method + coder.check_for_file_mentions("Please check file1.txt and file2.py") + + # Check if coder.abs_fnames contains both files + expected_files = set( [ - Path(coder.root) / "file1.txt", - Path(coder.root) / "file2.py", - ], + str(Path(coder.root) / fname1), + str(Path(coder.root) / fname2), + ] ) - ) - self.assertEqual(coder.abs_fnames, expected_files) + + self.assertEqual(coder.abs_fnames, expected_files) def test_check_for_ambiguous_filename_mentions_of_longer_paths(self): with GitTemporaryDirectory():