mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-28 08:14:59 +00:00
test: update input mocking in TestInputOutput
This commit is contained in:
parent
1d312e372d
commit
d5d087123a
1 changed files with 38 additions and 40 deletions
|
@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
|
|||
|
||||
from aider.dump import dump # noqa: F401
|
||||
from aider.io import AutoCompleter, ConfirmGroup, InputOutput
|
||||
import builtins
|
||||
from aider.utils import ChdirTemporaryDirectory
|
||||
|
||||
|
||||
|
@ -46,12 +47,8 @@ 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"
|
||||
|
||||
@patch("builtins.input", return_value="test input")
|
||||
def test_get_input_is_a_directory_error(self, mock_input):
|
||||
io = InputOutput(pretty=False) # Windows tests throw UnicodeDecodeError
|
||||
root = "/"
|
||||
rel_fnames = ["existing_file.txt"]
|
||||
|
@ -62,105 +59,106 @@ class TestInputOutput(unittest.TestCase):
|
|||
with patch("aider.io.open", side_effect=IsADirectoryError):
|
||||
result = io.get_input(root, rel_fnames, addable_rel_fnames, commands)
|
||||
self.assertEqual(result, "test input")
|
||||
mock_input.assert_called_once()
|
||||
|
||||
@patch("aider.io.prompt")
|
||||
def test_confirm_ask_explicit_yes_required(self, mock_prompt):
|
||||
@patch("builtins.input")
|
||||
def test_confirm_ask_explicit_yes_required(self, mock_input):
|
||||
io = InputOutput(pretty=False)
|
||||
|
||||
# Test case 1: explicit_yes_required=True, self.yes=True
|
||||
io.yes = True
|
||||
result = io.confirm_ask("Are you sure?", explicit_yes_required=True)
|
||||
self.assertFalse(result)
|
||||
mock_prompt.assert_not_called()
|
||||
mock_input.assert_not_called()
|
||||
|
||||
# Test case 2: explicit_yes_required=True, self.yes=False
|
||||
io.yes = False
|
||||
result = io.confirm_ask("Are you sure?", explicit_yes_required=True)
|
||||
self.assertFalse(result)
|
||||
mock_prompt.assert_not_called()
|
||||
mock_input.assert_not_called()
|
||||
|
||||
# Test case 3: explicit_yes_required=True, user input required
|
||||
io.yes = None
|
||||
mock_prompt.return_value = "y"
|
||||
mock_input.return_value = "y"
|
||||
result = io.confirm_ask("Are you sure?", explicit_yes_required=True)
|
||||
self.assertTrue(result)
|
||||
mock_prompt.assert_called_once()
|
||||
mock_input.assert_called_once()
|
||||
|
||||
# Reset mock_prompt
|
||||
mock_prompt.reset_mock()
|
||||
# Reset mock_input
|
||||
mock_input.reset_mock()
|
||||
|
||||
# Test case 4: explicit_yes_required=False, self.yes=True
|
||||
io.yes = True
|
||||
result = io.confirm_ask("Are you sure?", explicit_yes_required=False)
|
||||
self.assertTrue(result)
|
||||
mock_prompt.assert_not_called()
|
||||
mock_input.assert_not_called()
|
||||
|
||||
@patch("aider.io.prompt")
|
||||
def test_confirm_ask_with_group(self, mock_prompt):
|
||||
@patch("builtins.input")
|
||||
def test_confirm_ask_with_group(self, mock_input):
|
||||
io = InputOutput(pretty=False)
|
||||
group = ConfirmGroup()
|
||||
|
||||
# Test case 1: No group preference, user selects 'All'
|
||||
mock_prompt.return_value = "a"
|
||||
mock_input.return_value = "a"
|
||||
result = io.confirm_ask("Are you sure?", group=group)
|
||||
self.assertTrue(result)
|
||||
self.assertEqual(group.preference, "all")
|
||||
mock_prompt.assert_called_once()
|
||||
mock_prompt.reset_mock()
|
||||
mock_input.assert_called_once()
|
||||
mock_input.reset_mock()
|
||||
|
||||
# Test case 2: Group preference is 'All', should not prompt
|
||||
result = io.confirm_ask("Are you sure?", group=group)
|
||||
self.assertTrue(result)
|
||||
mock_prompt.assert_not_called()
|
||||
mock_input.assert_not_called()
|
||||
|
||||
# Test case 3: No group preference, user selects 'Skip all'
|
||||
group.preference = None
|
||||
mock_prompt.return_value = "s"
|
||||
mock_input.return_value = "s"
|
||||
result = io.confirm_ask("Are you sure?", group=group)
|
||||
self.assertFalse(result)
|
||||
self.assertEqual(group.preference, "skip")
|
||||
mock_prompt.assert_called_once()
|
||||
mock_prompt.reset_mock()
|
||||
mock_input.assert_called_once()
|
||||
mock_input.reset_mock()
|
||||
|
||||
# Test case 4: Group preference is 'Skip all', should not prompt
|
||||
result = io.confirm_ask("Are you sure?", group=group)
|
||||
self.assertFalse(result)
|
||||
mock_prompt.assert_not_called()
|
||||
mock_input.assert_not_called()
|
||||
|
||||
# Test case 5: explicit_yes_required=True, should not offer 'All' option
|
||||
group.preference = None
|
||||
mock_prompt.return_value = "y"
|
||||
mock_input.return_value = "y"
|
||||
result = io.confirm_ask("Are you sure?", group=group, explicit_yes_required=True)
|
||||
self.assertTrue(result)
|
||||
self.assertIsNone(group.preference)
|
||||
mock_prompt.assert_called_once()
|
||||
self.assertNotIn("(A)ll", mock_prompt.call_args[0][0])
|
||||
mock_prompt.reset_mock()
|
||||
mock_input.assert_called_once()
|
||||
self.assertNotIn("(A)ll", mock_input.call_args[0][0])
|
||||
mock_input.reset_mock()
|
||||
|
||||
@patch("aider.io.prompt")
|
||||
def test_confirm_ask_yes_no(self, mock_prompt):
|
||||
@patch("builtins.input")
|
||||
def test_confirm_ask_yes_no(self, mock_input):
|
||||
io = InputOutput(pretty=False)
|
||||
|
||||
# Test case 1: User selects 'Yes'
|
||||
mock_prompt.return_value = "y"
|
||||
mock_input.return_value = "y"
|
||||
result = io.confirm_ask("Are you sure?")
|
||||
self.assertTrue(result)
|
||||
mock_prompt.assert_called_once()
|
||||
mock_prompt.reset_mock()
|
||||
mock_input.assert_called_once()
|
||||
mock_input.reset_mock()
|
||||
|
||||
# Test case 2: User selects 'No'
|
||||
mock_prompt.return_value = "n"
|
||||
mock_input.return_value = "n"
|
||||
result = io.confirm_ask("Are you sure?")
|
||||
self.assertFalse(result)
|
||||
mock_prompt.assert_called_once()
|
||||
mock_prompt.reset_mock()
|
||||
mock_input.assert_called_once()
|
||||
mock_input.reset_mock()
|
||||
|
||||
# Test case 3: Empty input (default to Yes)
|
||||
mock_prompt.return_value = ""
|
||||
mock_input.return_value = ""
|
||||
result = io.confirm_ask("Are you sure?")
|
||||
self.assertTrue(result)
|
||||
mock_prompt.assert_called_once()
|
||||
mock_prompt.reset_mock()
|
||||
mock_input.assert_called_once()
|
||||
mock_input.reset_mock()
|
||||
|
||||
def test_get_command_completions(self):
|
||||
root = ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue