diff --git a/aider/coder.py b/aider/coder.py index 85e94f60a..a49f1edba 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -352,8 +352,12 @@ class Coder: fname_to_rel_fnames[fname].append(rel_fname) for fname, rel_fnames in fname_to_rel_fnames.items(): - if len(rel_fnames) == 1 and (fname in words or rel_fnames[0] in words): + if len(rel_fnames) == 1 and fname in words: mentioned_rel_fnames.add(rel_fnames[0]) + else: + for rel_fname in rel_fnames: + if rel_fname in words: + mentioned_rel_fnames.add(rel_fname) if not mentioned_rel_fnames: return diff --git a/tests/test_coder.py b/tests/test_coder.py index 710eb4a97..efc180878 100644 --- a/tests/test_coder.py +++ b/tests/test_coder.py @@ -8,8 +8,6 @@ class TestCoder(unittest.TestCase): def test_check_for_file_mentions(self): # Mock the IO object mock_io = MagicMock() - mock_io.get_input.return_value = "Please check file1.txt and file2.py" - mock_io.confirm_ask.return_value = True # Initialize the Coder object with the mocked IO and mocked repo coder = Coder(io=mock_io, openai_api_key="fake_key") @@ -29,8 +27,6 @@ class TestCoder(unittest.TestCase): def test_check_for_filename_mentions_of_longer_paths(self): # Mock the IO object mock_io = MagicMock() - mock_io.get_input.return_value = "Please check file1.txt and file2.py" - mock_io.confirm_ask.return_value = True # Initialize the Coder object with the mocked IO and mocked repo coder = Coder(io=mock_io, openai_api_key="fake_key") @@ -47,6 +43,23 @@ class TestCoder(unittest.TestCase): expected_files = {os.path.abspath("file1.txt"), os.path.abspath("file2.py")} self.assertEqual(coder.abs_fnames, expected_files) + def test_check_for_ambiguous_filename_mentions_of_longer_paths(self): + # Mock the IO object + mock_io = MagicMock() + + # Initialize the Coder object with the mocked IO and mocked repo + coder = Coder(io=mock_io, openai_api_key="fake_key") + + # Mock the git repo + mock_repo = MagicMock() + mock_repo.git.ls_files.return_value = "./file1.txt\n./other/file1.txt" + coder.repo = mock_repo + + # Call the check_for_file_mentions method + coder.check_for_file_mentions("Please check file1.txt!") + + self.assertEqual(coder.abs_fnames, set()) + if __name__ == "__main__": unittest.main()