From 670f78a874b5734e0a2f153ff74e677010b4037a Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 23 May 2024 13:49:37 -0700 Subject: [PATCH] aider: Added a test case for the `get_input` method in the `InputOutput` class. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Aider chat conversation: USER: add a test case for this ASSISTANT: To add a test case for the `get_input` method in the `InputOutput` class, we need to create a new test file. Let's create a new file named `test_io.py` in the `tests` directory and add the necessary test case. Here is the *SEARCH/REPLACE* block to create the new test file: tests/test_io.py ```python <<<<<<< SEARCH ======= import unittest from unittest.mock import patch, MagicMock from aider.io import InputOutput class TestInputOutput(unittest.TestCase): @patch('aider.io.PromptSession') def test_get_input_is_a_directory_error(self, MockPromptSession): # Mock the PromptSession to simulate user input mock_session = MockPromptSession.return_value mock_session.prompt.return_value = "test input" io = InputOutput() root = "/" rel_fnames = ["existing_file.txt"] addable_rel_fnames = ["new_file.txt"] commands = MagicMock() # Simulate IsADirectoryError with patch('aider.io.open', side_effect=IsADirectoryError): result = io.get_input(root, rel_fnames, addable_rel_fnames, commands) self.assertEqual(result, "test input") if __name__ == '__main__': unittest.main() >>>>>>> REPLACE ``` This test case uses the `unittest` framework and mocks the `PromptSession` to simulate user input. It also patches the `open` function to raise an `IsADirectoryError` and verifies that the `get_input` method handles it correctly. USER: I added these files to the chat: tests/test_io.py. If you need to propose edits to other existing files not already added to the chat, you *MUST* tell the me their full path names and ask me to *add the files to the chat*. End your reply and wait for my approval. You can keep asking if you then decide you need to edit more files. ASSISTANT: Let's add the new test case to the existing `tests/test_io.py` file to ensure that the `get_input` method in the `InputOutput` class handles the `IsADirectoryError` exception appropriately. Here is the *SEARCH/REPLACE* block to add the new test case: tests/test_io.py ```python <<<<<<< SEARCH import os import unittest from pathlib import Path from unittest.mock import patch from aider.io import AutoCompleter, InputOutput from aider.utils import ChdirTemporaryDirectory class TestInputOutput(unittest.TestCase): def test_no_color_environment_variable(self): with patch.dict(os.environ, {"NO_COLOR": "1"}): io = InputOutput() self.assertFalse(io.pretty) def test_autocompleter_with_non_existent_file(self): root = "" rel_fnames = ["non_existent_file.txt"] addable_rel_fnames = [] commands = None autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) def test_autocompleter_with_unicode_file(self): with ChdirTemporaryDirectory(): root = "" fname = "file.py" rel_fnames = [fname] addable_rel_fnames = [] commands = None autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) Path(fname).write_text("def hello(): pass\n") autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames + ["hello"])) encoding = "utf-16" some_content_which_will_error_if_read_with_encoding_utf8 = "ÅÍÎÏ".encode(encoding) with open(fname, "wb") as f: f.write(some_content_which_will_error_if_read_with_encoding_utf8) autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) ======= import os import unittest from pathlib import Path from unittest.mock import patch, MagicMock from aider.io import AutoCompleter, InputOutput from aider.utils import ChdirTemporaryDirectory class TestInputOutput(unittest.TestCase): def test_no_color_environment_variable(self): with patch.dict(os.environ, {"NO_COLOR": "1"}): io = InputOutput() self.assertFalse(io.pretty) def test_autocompleter_with_non_existent_file(self): root = "" rel_fnames = ["non_existent_file.txt"] addable_rel_fnames = [] commands = None autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) def test_autocompleter_with_unicode_file(self): with ChdirTemporaryDirectory(): root = "" fname = "file.py" rel_fnames = [fname] addable_rel_fnames = [] commands = None autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) Path(fname).write_text("def hello(): pass\n") autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames + ["hello"])) encoding = "utf-16" some_content_which_will_error_if_read_with_encoding_utf8 = "ÅÍÎÏ".encode(encoding) with open(fname, "wb") as f: f.write(some_content_which_will_error_if_read_with_encoding_utf8) autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) @patch('aider.io.PromptSession') def test_get_input_is_a_directory_error(self, MockPromptSession): # Mock the PromptSession to simulate user input mock_session = MockPromptSession.return_value mock_session.prompt.return_value = "test input" io = InputOutput() root = "/" rel_fnames = ["existing_file.txt"] addable_rel_fnames = ["new_file.txt"] commands = MagicMock() # Simulate IsADirectoryError with patch('aider.io.open', side_effect=IsADirectoryError): result = io.get_input(root, rel_fnames, addable_rel_fnames, commands) self.assertEqual(result, "test input") >>>>>>> REPLACE ``` This will add the new test case to the existing `tests/test_io.py` file. --- tests/test_io.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_io.py b/tests/test_io.py index 91c79308a..249961641 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,7 +1,7 @@ import os import unittest from pathlib import Path -from unittest.mock import patch +from unittest.mock import patch, MagicMock from aider.io import AutoCompleter, InputOutput from aider.utils import ChdirTemporaryDirectory @@ -43,6 +43,23 @@ class TestInputOutput(unittest.TestCase): autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands, "utf-8") self.assertEqual(autocompleter.words, set(rel_fnames)) + @patch('aider.io.PromptSession') + def test_get_input_is_a_directory_error(self, MockPromptSession): + # Mock the PromptSession to simulate user input + mock_session = MockPromptSession.return_value + mock_session.prompt.return_value = "test input" + + io = InputOutput() + root = "/" + rel_fnames = ["existing_file.txt"] + addable_rel_fnames = ["new_file.txt"] + commands = MagicMock() + + # Simulate IsADirectoryError + with patch('aider.io.open', side_effect=IsADirectoryError): + result = io.get_input(root, rel_fnames, addable_rel_fnames, commands) + self.assertEqual(result, "test input") + if __name__ == "__main__": unittest.main()