From 7cadfd9b4c3d87ccf602cb7bcbcb160d4484a6d4 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 6 Jul 2023 11:56:15 -0700 Subject: [PATCH] aider: Changed encoding to 'latin-1' and provided content that will cause an error when read with 'utf-8' encoding in the `test_run_with_file_utf_unicode_error` method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Context: USER: do the TODOs ASSISTANT: I see two TODOs in the provided code. 1. In the `test_run_with_file_utf_unicode_error` method, you need to specify an encoding other than 'utf-8'. 2. In the same method, you need to provide content that will cause an error when read with 'utf-8' encoding. Here are the proposed changes: ```python tests/test_coder.py <<<<<<< ORIGINAL encoding = 'pick something other than utf-8' # TODO ======= encoding = 'latin-1' >>>>>>> UPDATED ``` ```python tests/test_coder.py <<<<<<< ORIGINAL some_content_which_will_error_if_read_with_encoding_utf8 = '' # TODO ======= some_content_which_will_error_if_read_with_encoding_utf8 = 'ÅÍÎÏ'.encode('utf-16') >>>>>>> UPDATED ``` In the first change, I've chosen 'latin-1' as the encoding. It's a common encoding that's different from 'utf-8'. In the second change, I've provided a string of characters that are not valid 'utf-8' when encoded with 'utf-16'. This will cause an error when trying to read the file with 'utf-8' encoding. --- tests/test_coder.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_coder.py b/tests/test_coder.py index 5138d59d8..17ad349e8 100644 --- a/tests/test_coder.py +++ b/tests/test_coder.py @@ -264,5 +264,38 @@ class TestCoder(unittest.TestCase): self.assertNotEqual(coder.fence[0], "```") + def test_run_with_file_utf_unicode_error(self): + # Create a few temporary files + _, file1 = tempfile.mkstemp() + _, file2 = tempfile.mkstemp() + + files = [file1, file2] + + encoding = 'latin-1' + + # Initialize the Coder object with the mocked IO and mocked repo + coder = Coder.create( + models.GPT4, None, io=InputOutput(encoding=encoding), openai_api_key="fake_key", fnames=files + ) + + def mock_send(*args, **kwargs): + coder.partial_response_content = "ok" + coder.partial_response_function_call = dict() + + coder.send = MagicMock(side_effect=mock_send) + + # Call the run method with a message + coder.run(with_message="hi") + self.assertEqual(len(coder.abs_fnames), 2) + + some_content_which_will_error_if_read_with_encoding_utf8 = 'ÅÍÎÏ'.encode('utf-16') + with open(file1, "wb") as f: + f.write(some_content_which_will_error_if_read_with_encoding_utf8) + + coder.run(with_message="hi") + + # both files should still be here + self.assertEqual(len(coder.abs_fnames), 2) + if __name__ == "__main__": unittest.main()