Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix and test improvements #2164

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions aider/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.shortcuts import CompleteStyle, PromptSession
from prompt_toolkit.styles import Style
from pygments.lexers import MarkdownLexer, guess_lexer_for_filename
from pygments.lexers import guess_lexer_for_filename
from pygments.lexers.markup import MarkdownLexer
from pygments.token import Token
from rich.console import Console
from rich.markdown import Markdown
Expand Down Expand Up @@ -295,15 +296,15 @@ def read_image(self, filename):
with open(str(filename), "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string.decode("utf-8")
except OSError as err:
self.tool_error(f"{filename}: unable to read: {err}")
return
except FileNotFoundError:
self.tool_error(f"{filename}: file not found error")
return
except IsADirectoryError:
self.tool_error(f"{filename}: is a directory")
return
except OSError as err:
self.tool_error(f"{filename}: unable to read: {err}")
return
except Exception as e:
self.tool_error(f"{filename}: {e}")
return
Expand All @@ -315,9 +316,6 @@ def read_text(self, filename):
try:
with open(str(filename), "r", encoding=self.encoding) as f:
return f.read()
except OSError as err:
self.tool_error(f"{filename}: unable to read: {err}")
return
except FileNotFoundError:
self.tool_error(f"{filename}: file not found error")
return
Expand All @@ -328,6 +326,9 @@ def read_text(self, filename):
self.tool_error(f"{filename}: {e}")
self.tool_error("Use --encoding to set the unicode encoding.")
return
except OSError as err:
self.tool_error(f"{filename}: unable to read: {err}")
return

def write_text(self, filename, content):
if self.dry_run:
Expand Down Expand Up @@ -436,8 +437,8 @@ def add_to_input_history(self, inp):
return
FileHistory(self.input_history_file).append_string(inp)
# Also add to the in-memory history if it exists
if hasattr(self, "session") and hasattr(self.session, "history"):
self.session.history.append_string(inp)
if hasattr(self, "prompt_session") and hasattr(self.prompt_session, "history"):
self.prompt_session.history.append_string(inp)

def get_input_history(self):
if not self.input_history_file:
Expand Down
29 changes: 29 additions & 0 deletions tests/basic/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from prompt_toolkit.completion import CompleteEvent
from prompt_toolkit.document import Document
from prompt_toolkit.history import InMemoryHistory

from aider.dump import dump # noqa: F401
from aider.io import AutoCompleter, ConfirmGroup, InputOutput
Expand Down Expand Up @@ -258,6 +259,34 @@ def test_confirm_ask_allow_never(self, mock_input):
self.assertEqual(mock_input.call_count, 2)
self.assertNotIn(("Do you want to proceed?", None), io.never_prompts)

@patch('aider.io.FileHistory')
def test_add_to_input_history(self, mock_file_history):
# Setup
io = InputOutput(input_history_file='test_history.txt', fancy_input=True)
io.prompt_session = MagicMock()
io.prompt_session.history = InMemoryHistory()

# Test adding to history
test_input = "test input"
io.add_to_input_history(test_input)

# Assert that FileHistory.append_string was called
mock_file_history.return_value.append_string.assert_called_once_with(test_input)

# Assert that prompt_session.history.append_string was called
self.assertIn(test_input, io.prompt_session.history.get_strings())

def test_add_to_input_history_no_prompt_session(self):
# Setup
io = InputOutput(input_history_file='test_history.txt', fancy_input=False)

# Test adding to history
test_input = "test input"
io.add_to_input_history(test_input)

# Assert that the method doesn't raise an exception
# when prompt_session is not initialized


if __name__ == "__main__":
unittest.main()