tests pass on windows

This commit is contained in:
Paul Gauthier 2023-07-05 14:09:31 -07:00
parent 76eb9e020f
commit 9aaee49c51

View file

@ -1,4 +1,5 @@
import os import os
import shutil
import tempfile import tempfile
import unittest import unittest
from pathlib import Path from pathlib import Path
@ -11,179 +12,163 @@ from aider.io import InputOutput
class TestWholeFileCoder(unittest.TestCase): class TestWholeFileCoder(unittest.TestCase):
def setUp(self): def setUp(self):
self.original_cwd = os.getcwd() self.original_cwd = os.getcwd()
self.tempdir = tempfile.mkdtemp()
os.chdir(self.tempdir)
def tearDown(self): def tearDown(self):
os.chdir(self.original_cwd) os.chdir(self.original_cwd)
shutil.rmtree(self.tempdir, ignore_errors=True)
def test_update_files(self): def test_update_files(self):
with tempfile.TemporaryDirectory() as temp_dir: # Create a sample file in the temporary directory
os.chdir(temp_dir) sample_file = "sample.txt"
with open(sample_file, "w") as f:
f.write("Original content\n")
# Create a sample file in the temporary directory # Initialize WholeFileCoder with the temporary directory
sample_file = "sample.txt" io = InputOutput(yes=True)
with open(sample_file, "w") as f: coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file])
f.write("Original content\n")
# Initialize WholeFileCoder with the temporary directory # Set the partial response content with the updated content
io = InputOutput(yes=True) coder.partial_response_content = f"{sample_file}\n```\nUpdated content\n```"
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file])
# Set the partial response content with the updated content # Call update_files method
coder.partial_response_content = f"{sample_file}\n```\nUpdated content\n```" edited_files = coder.update_files()
# Call update_files method # Check if the sample file was updated
edited_files = coder.update_files() self.assertIn("sample.txt", edited_files)
# Check if the sample file was updated # Check if the content of the sample file was updated
self.assertIn("sample.txt", edited_files) with open(sample_file, "r") as f:
updated_content = f.read()
# Check if the content of the sample file was updated self.assertEqual(updated_content, "Updated content\n")
with open(sample_file, "r") as f:
updated_content = f.read()
self.assertEqual(updated_content, "Updated content\n")
def test_update_files_with_existing_fence(self): def test_update_files_with_existing_fence(self):
with tempfile.TemporaryDirectory() as temp_dir: # Create a sample file in the temporary directory
os.chdir(temp_dir) sample_file = "sample.txt"
original_content = """
# Create a sample file in the temporary directory
sample_file = "sample.txt"
original_content = """
Here is some quoted text: Here is some quoted text:
``` ```
Quote! Quote!
``` ```
""" """
with open(sample_file, "w") as f: with open(sample_file, "w") as f:
f.write(original_content) f.write(original_content)
# Initialize WholeFileCoder with the temporary directory # Initialize WholeFileCoder with the temporary directory
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file]) coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file])
coder.choose_fence() coder.choose_fence()
self.assertNotEqual(coder.fence[0], "```") self.assertNotEqual(coder.fence[0], "```")
# Set the partial response content with the updated content # Set the partial response content with the updated content
coder.partial_response_content = ( coder.partial_response_content = (
f"{sample_file}\n{coder.fence[0]}\nUpdated content\n{coder.fence[1]}" f"{sample_file}\n{coder.fence[0]}\nUpdated content\n{coder.fence[1]}"
) )
# Call update_files method # Call update_files method
edited_files = coder.update_files() edited_files = coder.update_files()
# Check if the sample file was updated # Check if the sample file was updated
self.assertIn("sample.txt", edited_files) self.assertIn("sample.txt", edited_files)
# Check if the content of the sample file was updated # Check if the content of the sample file was updated
with open(sample_file, "r") as f: with open(sample_file, "r") as f:
updated_content = f.read() updated_content = f.read()
self.assertEqual(updated_content, "Updated content\n") self.assertEqual(updated_content, "Updated content\n")
def test_update_files_bogus_path_prefix(self): def test_update_files_bogus_path_prefix(self):
with tempfile.TemporaryDirectory() as temp_dir: # Create a sample file in the temporary directory
os.chdir(temp_dir) sample_file = "sample.txt"
with open(sample_file, "w") as f:
f.write("Original content\n")
# Create a sample file in the temporary directory # Initialize WholeFileCoder with the temporary directory
sample_file = "sample.txt" io = InputOutput(yes=True)
with open(sample_file, "w") as f: coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file])
f.write("Original content\n")
# Initialize WholeFileCoder with the temporary directory # Set the partial response content with the updated content
io = InputOutput(yes=True) # With path/to/ prepended onto the filename
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file]) coder.partial_response_content = f"path/to/{sample_file}\n```\nUpdated content\n```"
# Set the partial response content with the updated content # Call update_files method
# With path/to/ prepended onto the filename edited_files = coder.update_files()
coder.partial_response_content = f"path/to/{sample_file}\n```\nUpdated content\n```"
# Call update_files method # Check if the sample file was updated
edited_files = coder.update_files() self.assertIn("sample.txt", edited_files)
# Check if the sample file was updated # Check if the content of the sample file was updated
self.assertIn("sample.txt", edited_files) with open(sample_file, "r") as f:
updated_content = f.read()
# Check if the content of the sample file was updated self.assertEqual(updated_content, "Updated content\n")
with open(sample_file, "r") as f:
updated_content = f.read()
self.assertEqual(updated_content, "Updated content\n")
def test_update_files_not_in_chat(self): def test_update_files_not_in_chat(self):
with tempfile.TemporaryDirectory() as temp_dir: # Create a sample file in the temporary directory
os.chdir(temp_dir) sample_file = "sample.txt"
with open(sample_file, "w") as f:
f.write("Original content\n")
# Create a sample file in the temporary directory # Initialize WholeFileCoder with the temporary directory
sample_file = "sample.txt" io = InputOutput(yes=True)
with open(sample_file, "w") as f: coder = WholeFileCoder(main_model=models.GPT35, io=io)
f.write("Original content\n")
# Initialize WholeFileCoder with the temporary directory # Set the partial response content with the updated content
io = InputOutput(yes=True) coder.partial_response_content = f"{sample_file}\n```\nUpdated content\n```"
coder = WholeFileCoder(main_model=models.GPT35, io=io)
# Set the partial response content with the updated content # Call update_files method
coder.partial_response_content = f"{sample_file}\n```\nUpdated content\n```" edited_files = coder.update_files()
# Call update_files method # Check if the sample file was updated
edited_files = coder.update_files() self.assertIn("sample.txt", edited_files)
# Check if the sample file was updated # Check if the content of the sample file was updated
self.assertIn("sample.txt", edited_files) with open(sample_file, "r") as f:
updated_content = f.read()
# Check if the content of the sample file was updated self.assertEqual(updated_content, "Updated content\n")
with open(sample_file, "r") as f:
updated_content = f.read()
self.assertEqual(updated_content, "Updated content\n")
def test_update_files_no_filename_single_file_in_chat(self): def test_update_files_no_filename_single_file_in_chat(self):
with tempfile.TemporaryDirectory() as temp_dir: sample_file = "accumulate.py"
os.chdir(temp_dir) content = (
"def accumulate(collection, operation):\n return [operation(x) for x in"
" collection]\n"
)
sample_file = "accumulate.py" with open(sample_file, "w") as f:
content = ( f.write("Original content\n")
"def accumulate(collection, operation):\n return [operation(x) for x in"
" collection]\n"
)
with open(sample_file, "w") as f: # Initialize WholeFileCoder with the temporary directory
f.write("Original content\n") io = InputOutput(yes=True)
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file])
# Initialize WholeFileCoder with the temporary directory # Set the partial response content with the updated content
io = InputOutput(yes=True) coder.partial_response_content = (
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file]) f"Here's the modified `{sample_file}` file that implements the `accumulate`"
f" function as per the given instructions:\n\n```\n{content}```\n\nThis"
" implementation uses a list comprehension to apply the `operation` function to"
" each element of the `collection` and returns the resulting list."
)
# Set the partial response content with the updated content # Call update_files method
coder.partial_response_content = ( edited_files = coder.update_files()
f"Here's the modified `{sample_file}` file that implements the `accumulate`"
f" function as per the given instructions:\n\n```\n{content}```\n\nThis"
" implementation uses a list comprehension to apply the `operation` function to"
" each element of the `collection` and returns the resulting list."
)
# Call update_files method # Check if the sample file was updated
edited_files = coder.update_files() self.assertIn(sample_file, edited_files)
# Check if the sample file was updated # Check if the content of the sample file was updated
self.assertIn(sample_file, edited_files) with open(sample_file, "r") as f:
updated_content = f.read()
# Check if the content of the sample file was updated self.assertEqual(updated_content, content)
with open(sample_file, "r") as f:
updated_content = f.read()
self.assertEqual(updated_content, content)
def test_update_files_earlier_filename(self): def test_update_files_earlier_filename(self):
with tempfile.TemporaryDirectory() as temp_dir: fname_a = Path("a.txt")
os.chdir(temp_dir) fname_b = Path("b.txt")
print(temp_dir)
fname_a = Path("a.txt") fname_a.write_text("before a\n")
fname_b = Path("b.txt") fname_b.write_text("before b\n")
fname_a.write_text("before a\n") response = """
fname_b.write_text("before b\n")
response = """
Here is a new version of `a.txt` for you to consider: Here is a new version of `a.txt` for you to consider:
``` ```
@ -196,22 +181,22 @@ And here is `b.txt`:
after b after b
``` ```
""" """
# Initialize WholeFileCoder with the temporary directory # Initialize WholeFileCoder with the temporary directory
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[fname_a, fname_b]) coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[fname_a, fname_b])
# Set the partial response content with the updated content # Set the partial response content with the updated content
coder.partial_response_content = response coder.partial_response_content = response
# Call update_files method # Call update_files method
edited_files = coder.update_files() edited_files = coder.update_files()
# Check if the sample file was updated # Check if the sample file was updated
self.assertIn(str(fname_a), edited_files) self.assertIn(str(fname_a), edited_files)
self.assertIn(str(fname_b), edited_files) self.assertIn(str(fname_b), edited_files)
self.assertEqual(fname_a.read_text(), "after a\n") self.assertEqual(fname_a.read_text(), "after a\n")
self.assertEqual(fname_b.read_text(), "after b\n") self.assertEqual(fname_b.read_text(), "after b\n")
if __name__ == "__main__": if __name__ == "__main__":