summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-05 11:26:07 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-05 11:26:07 -0400
commitf1e4fc3a90bad1c0d245af4c30fe4ec41a474ec9 (patch)
tree8b6c25a8ab4418fa526b4b1ca69e8ea42ccf226f
parent3e0c9ba1ad4a415e0aba81daf2e7f9f57281b1bc (diff)
downloadcmd2-git-f1e4fc3a90bad1c0d245af4c30fe4ec41a474ec9.tar.gz
Updated unit tests for read_input()
-rw-r--r--cmd2/cmd2.py7
-rwxr-xr-xtests/test_cmd2.py36
-rwxr-xr-xtests/test_history.py1
3 files changed, 42 insertions, 2 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 088bb500..19f1e6e9 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2607,6 +2607,9 @@ class Cmd(cmd.Cmd):
nonlocal saved_history
nonlocal parser
+ if readline_configured: # pragma: no cover
+ return
+
# Configure tab completion
if self._completion_supported():
saved_completer = readline.get_completer()
@@ -2614,7 +2617,7 @@ class Cmd(cmd.Cmd):
# Disable completion
if completion_mode == CompletionMode.NONE:
# noinspection PyUnusedLocal
- def complete_none(text: str, state: int) -> Optional[str]:
+ def complete_none(text: str, state: int): # pragma: no cover
return None
complete_func = complete_none
@@ -2651,7 +2654,7 @@ class Cmd(cmd.Cmd):
def restore_readline():
"""Restore readline tab completion and history"""
nonlocal readline_configured
- if not readline_configured:
+ if not readline_configured: # pragma: no cover
return
if self._completion_supported():
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index be6f52d1..f5c8dbc6 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1437,6 +1437,42 @@ def test_read_input_rawinput_true(capsys, monkeypatch):
line = app.read_input(prompt_str)
assert line == input_str
+ # Run custom history code
+ import readline
+ readline.add_history('old_history')
+ custom_history = ['cmd1', 'cmd2']
+ line = app.read_input(prompt_str, history=custom_history, completion_mode=cmd2.CompletionMode.NONE)
+ assert line == input_str
+ readline.clear_history()
+
+ # Run all completion modes
+ line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.NONE)
+ assert line == input_str
+
+ line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.COMMANDS)
+ assert line == input_str
+
+ # custom choices
+ custom_choices = ['choice1', 'choice2']
+ line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM,
+ choices=custom_choices)
+ assert line == input_str
+
+ # custom choices_provider
+ line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM,
+ choices_provider=cmd2.Cmd.get_all_commands)
+ assert line == input_str
+
+ # custom completer
+ line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM,
+ completer=cmd2.Cmd.path_complete)
+ assert line == input_str
+
+ # custom parser
+ line = app.read_input(prompt_str, completion_mode=cmd2.CompletionMode.CUSTOM,
+ parser=cmd2.Cmd2ArgumentParser())
+ assert line == input_str
+
# isatty is False
with mock.patch('sys.stdin.isatty', mock.MagicMock(name='isatty', return_value=False)):
# echo True
diff --git a/tests/test_history.py b/tests/test_history.py
index 6fa16ad8..86c52592 100755
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -27,6 +27,7 @@ except ImportError:
#
def test_readline_remove_history_item(base_app):
from cmd2.rl_utils import readline
+ readline.clear_history()
assert readline.get_current_history_length() == 0
readline.add_history('this is a test')
assert readline.get_current_history_length() == 1