summaryrefslogtreecommitdiff
path: root/tests/test_history.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2019-05-24 23:03:22 -0600
committerkotfu <kotfu@kotfu.net>2019-05-24 23:03:22 -0600
commitc13ff0f48b1e4652b6145a5b69ea2f439629afdf (patch)
treecc7709211f9c6900e99f76efb1cc8f56e4a1be06 /tests/test_history.py
parent69308f455193d5b5f89a6edf7c7d3d052bdcd5ce (diff)
downloadcmd2-git-c13ff0f48b1e4652b6145a5b69ea2f439629afdf.tar.gz
Populate readline history from unpickled history
Diffstat (limited to 'tests/test_history.py')
-rw-r--r--tests/test_history.py68
1 files changed, 28 insertions, 40 deletions
diff --git a/tests/test_history.py b/tests/test_history.py
index 4553575a..2fdd772f 100644
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -458,46 +458,6 @@ def hist_file():
except FileNotFoundError:
pass
-def test_existing_history_file(hist_file, capsys):
-
- # Create the history file before making cmd2 app
- with open(hist_file, 'w'):
- pass
-
- # Create a new cmd2 app
- cmd2.Cmd(persistent_history_file=hist_file)
- _, err = capsys.readouterr()
-
- # Make sure there were no errors
- assert err == ''
-
- # Unregister the call to write_history_file that cmd2 did
- ## TODO atexit.unregister(readline.write_history_file)
-
- # Remove created history file
- #os.remove(hist_file)
-
-def test_new_history_file(hist_file, capsys):
-
- # Remove any existing history file
- try:
- os.remove(hist_file)
- except OSError:
- pass
-
- # Create a new cmd2 app
- cmd2.Cmd(persistent_history_file=hist_file)
- _, err = capsys.readouterr()
-
- # Make sure there were no errors
- assert err == ''
-
- # Unregister the call to write_history_file that cmd2 did
- ### TODO atexit.unregister(readline.write_history_file)
-
- # Remove created history file
- #os.remove(hist_file)
-
def test_bad_history_file_path(capsys, request):
# Use a directory path as the history file
test_dir = os.path.dirname(request.module.__file__)
@@ -531,3 +491,31 @@ def test_history_file_conversion_no_truncate_on_init(hist_file, capsys):
assert histlist[0]== 'help\n'
assert histlist[1] == 'alias\n'
assert histlist[2] == 'alias create s shortcuts\n'
+
+def test_history_populates_readline(hist_file):
+ # - create a cmd2 with persistent history
+ app = cmd2.Cmd(persistent_history_file=hist_file)
+ run_cmd(app, 'help')
+ run_cmd(app, 'shortcuts')
+ run_cmd(app, 'shortcuts')
+ run_cmd(app, 'alias')
+
+ # call the private method which is registered to write history at exit
+ app._persist_history_on_exit()
+ # - create a new cmd2 with persistent history
+ app = cmd2.Cmd(persistent_history_file=hist_file)
+
+ assert len(app.history) == 4
+ assert app.history.get(1).statement.raw == 'help'
+ assert app.history.get(2).statement.raw == 'shortcuts'
+ assert app.history.get(3).statement.raw == 'shortcuts'
+ assert app.history.get(4).statement.raw == 'alias'
+
+ # readline only adds a single entry for multiple sequential identical commands
+ # so we check to make sure that cmd2 populated the readline history
+ # using the same rules
+ from cmd2.rl_utils import readline
+ assert readline.get_current_history_length() == 3
+ assert readline.get_history_item(1) == 'help'
+ assert readline.get_history_item(2) == 'shortcuts'
+ assert readline.get_history_item(3) == 'alias'