fix: Handle filenames starting with fences or triple backticks correctly

This commit is contained in:
Paul Gauthier 2025-04-20 16:36:36 -07:00 committed by Paul Gauthier (aider)
parent c6ce871700
commit 5e210c700d
2 changed files with 16 additions and 37 deletions

View file

@ -414,11 +414,15 @@ def strip_filename(filename, fence):
start_fence = fence[0] start_fence = fence[0]
if filename.startswith(start_fence): if filename.startswith(start_fence):
candidate = filename[len(start_fence) :] candidate = filename[len(start_fence) :]
if candidate and "." in candidate: if candidate and ("." in candidate or "/" in candidate):
return candidate return candidate
return
if filename.startswith(triple_backticks): if filename.startswith(triple_backticks):
filename = filename[len(triple_backticks) :] candidate = filename[len(triple_backticks) :]
if candidate and ("." in candidate or "/" in candidate):
return candidate
return
filename = filename.rstrip(":") filename = filename.rstrip(":")
filename = filename.lstrip("#") filename = filename.lstrip("#")

View file

@ -108,29 +108,6 @@ Hope you like it!
edits = list(eb.find_original_update_blocks(edit)) edits = list(eb.find_original_update_blocks(edit))
self.assertEqual(edits, [("foo.txt", "Two\n", "Tooooo\n")]) self.assertEqual(edits, [("foo.txt", "Two\n", "Tooooo\n")])
def test_find_original_update_blocks_mangled_filename_w_source_tag(self):
source = "source"
edit = """
Here's the change:
<%s>foo.txt
<<<<<<< SEARCH
One
=======
Two
>>>>>>> REPLACE
</%s>
Hope you like it!
""" % (source, source)
fence = ("<%s>" % source, "</%s>" % source)
with self.assertRaises(ValueError) as cm:
_edits = list(eb.find_original_update_blocks(edit, fence))
self.assertIn("missing filename", str(cm.exception))
def test_find_original_update_blocks_quote_below_filename(self): def test_find_original_update_blocks_quote_below_filename(self):
edit = """ edit = """
Here's the change: Here's the change:
@ -181,10 +158,11 @@ Tooooo
oops! oops!
>>>>>>> REPLACE
""" """
with self.assertRaises(ValueError) as cm: with self.assertRaises(ValueError) as cm:
list(eb.find_original_update_blocks(edit)) _blocks = list(eb.find_original_update_blocks(edit))
self.assertIn("filename", str(cm.exception)) self.assertIn("filename", str(cm.exception))
def test_find_original_update_blocks_no_final_newline(self): def test_find_original_update_blocks_no_final_newline(self):
@ -575,7 +553,7 @@ Hope you like it!
edits = list(eb.find_original_update_blocks(edit, fence=quad_backticks)) edits = list(eb.find_original_update_blocks(edit, fence=quad_backticks))
self.assertEqual(edits, [("foo.txt", "", "Tooooo\n")]) self.assertEqual(edits, [("foo.txt", "", "Tooooo\n")])
#Test for shell script blocks with sh language identifier (issue #3785) # Test for shell script blocks with sh language identifier (issue #3785)
def test_find_original_update_blocks_with_sh_language_identifier(self): def test_find_original_update_blocks_with_sh_language_identifier(self):
# https://github.com/Aider-AI/aider/issues/3785 # https://github.com/Aider-AI/aider/issues/3785
edit = """ edit = """
@ -609,13 +587,13 @@ exit 0
# Check that the content contains the expected shell script elements # Check that the content contains the expected shell script elements
result_content = edits[0][2] result_content = edits[0][2]
self.assertIn("#!/bin/bash", result_content) self.assertIn("#!/bin/bash", result_content)
self.assertIn("if [ \"$#\" -ne 1 ];", result_content) self.assertIn('if [ "$#" -ne 1 ];', result_content)
self.assertIn("echo \"Usage: $0 <argument>\"", result_content) self.assertIn('echo "Usage: $0 <argument>"', result_content)
self.assertIn("exit 1", result_content) self.assertIn("exit 1", result_content)
self.assertIn("echo \"$1\"", result_content) self.assertIn('echo "$1"', result_content)
self.assertIn("exit 0", result_content) self.assertIn("exit 0", result_content)
#Test for C# code blocks with csharp language identifier # Test for C# code blocks with csharp language identifier
def test_find_original_update_blocks_with_csharp_language_identifier(self): def test_find_original_update_blocks_with_csharp_language_identifier(self):
edit = """ edit = """
Here's a C# code change: Here's a C# code change:
@ -631,12 +609,9 @@ Console.WriteLine("Hello, C# World!");
""" """
edits = list(eb.find_original_update_blocks(edit)) edits = list(eb.find_original_update_blocks(edit))
search_text = "Console.WriteLine(\"Hello World!\");\n" search_text = 'Console.WriteLine("Hello World!");\n'
replace_text = "Console.WriteLine(\"Hello, C# World!\");\n" replace_text = 'Console.WriteLine("Hello, C# World!");\n'
self.assertEqual( self.assertEqual(edits, [("Program.cs", search_text, replace_text)])
edits,
[("Program.cs", search_text, replace_text)]
)
if __name__ == "__main__": if __name__ == "__main__":