Commit graph

12 commits

Author SHA1 Message Date
Paul Gauthier
0860e7b30d do not package tests/ 2024-07-15 08:40:32 +01:00
Paul Gauthier
ce46ea8d62 moved tests/ to aider/tests/ 2024-06-10 15:33:49 -07:00
Paul Gauthier
a57dd90a49 fix unicode err 2024-05-23 15:02:31 -07:00
Paul Gauthier
cb4384edc7 aider: Set the environment variable PYTHONIOENCODING to utf-8 in the test to prevent UnicodeEncodeError.
# Aider chat conversation:

USER: I am getting this testing error on Windows: ======================================================================
ERROR: test_get_input_is_a_directory_error (test_io.TestInputOutput.test_get_input_is_a_directory_error)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\unittest\mock.py", line 1390, in patched
    return func(*newargs, **newkeywargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\a\aider\aider\tests\test_io.py", line 60, in test_get_input_is_a_directory_error
    result = io.get_input(root, rel_fnames, addable_rel_fnames, commands)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\a\aider\aider\aider\io.py", line 186, in get_input
    self.console.rule(**style)
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\console.py", line 1580, in rule
    self.print(rule)
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\console.py", line 1673, in print
    with self:
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\console.py", line 865, in __exit__
    self._exit_buffer()
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\console.py", line 823, in _exit_buffer
    self._check_buffer()
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\console.py", line 2027, in _check_buffer
    legacy_windows_render(buffer, LegacyWindowsTerm(self.file))
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\_windows_renderer.py", line 17, in legacy_windows_render
    term.write_styled(text, style)
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\_win32_console.py", line 442, in write_styled
    self.write_text(text)
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\site-packages\rich\_win32_console.py", line 403, in write_text
    self.write(text)
  File "C:\hostedtoolcache\windows\Python\3.12.3\x64\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-78: character maps to <undefined>

ASSISTANT: The error you're encountering is due to the `charmap` codec on Windows not being able to encode certain Unicode characters. To fix this, we can set the environment variable `PYTHONIOENCODING` to `utf-8` in the test to ensure that the console output uses UTF-8 encoding.

Let's update the test to set this environment variable:

tests/test_io.py
```python
<<<<<<< SEARCH
        # Mock the PromptSession to simulate user input
=======
        # Set the environment variable to ensure UTF-8 encoding
        os.environ["PYTHONIOENCODING"] = "utf-8"

        # Mock the PromptSession to simulate user input
>>>>>>> REPLACE
```

This change will ensure that the console output uses UTF-8 encoding, which should prevent the `UnicodeEncodeError` from occurring.
2024-05-23 14:21:13 -07:00
Paul Gauthier
670f78a874 aider: Added a test case for the get_input method in the InputOutput class.
# 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.
2024-05-23 13:49:37 -07:00
Paul Gauthier
7113a30271 unified diffs 2023-12-17 12:54:34 -08:00
Paul Gauthier
f3d3815201 Armor AutoCompleter against unicode errors #305 2023-11-03 07:26:21 -07:00
Paul Gauthier
aedfa64eb9 Fix test 2023-07-10 15:33:46 -07:00
Paul Gauthier
5d18ebd724 fixed file not found in autocompleter, issue #30 2023-07-04 12:38:36 -07:00
Paul Gauthier
4373006137 aider: Added a test for AutoCompleter that passes in a non-existent file and checks that self.words is an empty set.
# Context:
USER: add a test for `AutoCompleter` that passes in a non-existant file. make sure self.words is empty.
ASSISTANT: To add a test for `AutoCompleter` that passes in a non-existent file, we need to modify the `tests/test_io.py` file. We will add a new test method `test_autocompleter_with_non_existent_file` in the `TestInputOutput` class.

Here is the change:

```python
tests/test_io.py
<<<<<<< ORIGINAL
import os
import unittest
from unittest.mock import patch

from aider.io import InputOutput

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)

if __name__ == "__main__":
    unittest.main()
=======
import os
import unittest
from unittest.mock import patch

from aider.io import InputOutput, AutoCompleter

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"]
        addable_rel_fnames = []
        commands = None
        autocompleter = AutoCompleter(root, rel_fnames, addable_rel_fnames, commands)
        self.assertEqual(autocompleter.words, set())

if __name__ == "__main__":
    unittest.main()
>>>>>>> UPDATED
```

This new test creates an instance of `AutoCompleter` with a non-existent file and checks that `self.words` is an empty set.
2023-07-04 12:37:23 -07:00
Paul Gauthier
92cd2f3ce6 isort 2023-05-28 22:47:38 -07:00
Paul Gauthier
52a9df5b34 aider: Added a test function to check if the pretty attribute of the InputOutput class is set to False when the NO_COLOR environment variable is set.
# Context:
USER: add a test for NO_COLOR
ASSISTANT: To add a test for the `NO_COLOR` environment variable, we will create a new test function in the `tests/test_io.py` file. Since this file does not exist, we will create it first.

```python
tests/test_io.py
<<<<<<< ORIGINAL
=======
import os
import unittest
from unittest.mock import patch
from aider.io import InputOutput

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)

if __name__ == "__main__":
    unittest.main()
>>>>>>> UPDATED
```

This test function checks if the `pretty` attribute of the `InputOutput` class is set to `False` when the `NO_COLOR` environment variable is set.
2023-05-27 18:07:52 -07:00