Added tests to exercise all combinations of path formats for get_file_mentions and get_addable_relative_files, and updated the get_file_mentions method to handle both Unix and Windows path formats.

This commit is contained in:
Paul Gauthier (aider) 2024-07-18 15:19:32 +01:00
parent c076c134ac
commit 07550d6f5f
2 changed files with 34 additions and 2 deletions

View file

@ -1062,13 +1062,14 @@ class Coder:
mentioned_rel_fnames = set()
fname_to_rel_fnames = {}
for rel_fname in addable_rel_fnames:
if rel_fname in words:
normalized_rel_fname = rel_fname.replace('\\', '/')
if normalized_rel_fname in words or rel_fname in words:
mentioned_rel_fnames.add(str(rel_fname))
fname = os.path.basename(rel_fname)
# Don't add basenames that could be plain words like "run" or "make"
if "/" in fname or "." in fname or "_" in fname or "-" in fname:
if "/" in fname or "\\" in fname or "." in fname or "_" in fname or "-" in fname:
if fname not in fname_to_rel_fnames:
fname_to_rel_fnames[fname] = []
fname_to_rel_fnames[fname].append(rel_fname)

View file

@ -205,6 +205,37 @@ class TestCoder(unittest.TestCase):
self.assertEqual(coder.abs_fnames, set([str(fname.resolve())]))
def test_get_file_mentions_path_formats(self):
with GitTemporaryDirectory():
io = InputOutput(pretty=False, yes=True)
coder = Coder.create(self.GPT35, None, io)
# Test cases with different path formats
test_cases = [
# Unix paths in content, Unix paths in get_addable_relative_files
("Check file1.txt and dir/file2.txt", ["file1.txt", "dir/file2.txt"]),
# Windows paths in content, Windows paths in get_addable_relative_files
("Check file1.txt and dir\\file2.txt", ["file1.txt", "dir\\file2.txt"]),
# Unix paths in content, Windows paths in get_addable_relative_files
("Check file1.txt and dir/file2.txt", ["file1.txt", "dir\\file2.txt"]),
# Windows paths in content, Unix paths in get_addable_relative_files
("Check file1.txt and dir\\file2.txt", ["file1.txt", "dir/file2.txt"]),
# Mixed paths in content, Unix paths in get_addable_relative_files
("Check file1.txt, dir/file2.txt, and other\\file3.txt",
["file1.txt", "dir/file2.txt", "other/file3.txt"]),
# Mixed paths in content, Windows paths in get_addable_relative_files
("Check file1.txt, dir/file2.txt, and other\\file3.txt",
["file1.txt", "dir\\file2.txt", "other\\file3.txt"]),
]
for content, addable_files in test_cases:
with self.subTest(content=content, addable_files=addable_files):
coder.get_addable_relative_files = MagicMock(return_value=set(addable_files))
mentioned_files = coder.get_file_mentions(content)
expected_files = set(addable_files)
self.assertEqual(mentioned_files, expected_files,
f"Failed for content: {content}, addable_files: {addable_files}")
def test_run_with_file_deletion(self):
# Create a few temporary files