fix: handle empty original content when creating new files

This commit is contained in:
Paul Gauthier 2024-11-05 09:14:01 -08:00 committed by Paul Gauthier (aider)
parent 85f23b3408
commit c71a92ac84
2 changed files with 44 additions and 1 deletions

View file

@ -10,6 +10,7 @@ from aider.coders import editblock_coder as eb
from aider.dump import dump # noqa: F401
from aider.io import InputOutput
from aider.models import Model
from aider.utils import GitTemporaryDirectory
class TestUtils(unittest.TestCase):
@ -341,6 +342,45 @@ These changes replace the `subprocess.run` patches with `subprocess.check_output
result = eb.replace_most_similar_chunk(whole, part, replace)
self.assertEqual(result, expected_output)
def test_create_new_file_with_other_file_in_chat(self):
# https://github.com/Aider-AI/aider/issues/2258
with GitTemporaryDirectory():
# Create a few temporary files
file1 = "file.txt"
with open(file1, "w", encoding="utf-8") as f:
f.write("one\ntwo\nthree\n")
files = [file1]
# Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(self.GPT35, "diff", io=InputOutput(yes=True), fnames=files)
def mock_send(*args, **kwargs):
coder.partial_response_content = f"""
Do this:
newfile.txt
<<<<<<< SEARCH
=======
creating a new file
>>>>>>> REPLACE
"""
coder.partial_response_function_call = dict()
return []
coder.send = mock_send
# Call the run method with a message
coder.run(with_message="hi")
content = Path(file1).read_text(encoding="utf-8")
self.assertEqual(content, "one\ntwo\nthree\n")
content = Path("newfile.txt").read_text(encoding="utf-8")
self.assertEqual(content, "creating a new file\n")
def test_full_edit(self):
# Create a few temporary files
_, file1 = tempfile.mkstemp()