Skip to content

Commit 3d05273

Browse files
committed
Fix one history test
TODO: - prompt-toolkit history isn't properly initialized with history from a persistent history file, as shown by the remaining failing history test
1 parent ef36ba2 commit 3d05273

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

cmd2/cmd2.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,15 @@ def __init__(
413413
# Commands to exclude from the help menu and tab completion
414414
self.hidden_commands = ['eof', '_relative_run_script']
415415

416-
# Initialize history
416+
# Initialize prompt-toolkit PromptSession
417+
self.history_adapter = Cmd2History(self)
418+
self.completer = Cmd2Completer(self)
419+
self.session: PromptSession[str] = PromptSession(
420+
history=self.history_adapter,
421+
completer=self.completer,
422+
)
423+
424+
# Initialize history from a persistent history file (if present)
417425
self.persistent_history_file = ''
418426
self._persistent_history_length = persistent_history_length
419427
self._initialize_history(persistent_history_file)
@@ -462,14 +470,6 @@ def __init__(
462470
# The multiline command currently being typed which is used to tab complete multiline commands.
463471
self._multiline_in_progress = ''
464472

465-
# Initialize PromptSession
466-
self.history_adapter = Cmd2History(self)
467-
self.completer = Cmd2Completer(self)
468-
self.session: PromptSession[str] = PromptSession(
469-
history=self.history_adapter,
470-
completer=self.completer,
471-
)
472-
473473
# Characters used to draw a horizontal rule. Should not be blank.
474474
self.ruler = "─"
475475

tests/test_history.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,25 @@ def verify_hi_last_result(app: cmd2.Cmd, expected_length: int) -> None:
2828

2929

3030
#
31-
# readline tests
31+
# prompt-toolkit tests
3232
#
33-
def test_readline_remove_history_item() -> None:
34-
from cmd2.rl_utils import (
35-
readline,
36-
)
33+
def test_pt_add_history_item() -> None:
34+
from prompt_toolkit import PromptSession
35+
from prompt_toolkit.history import InMemoryHistory
36+
37+
# Create a history object and add some initial items
38+
history = InMemoryHistory()
39+
history.append_string('command one')
40+
history.append_string('command two')
41+
assert 'command one' in history.get_strings()
42+
assert len(history.get_strings()) == 2
43+
44+
# Start a session and use this history
45+
session = PromptSession(history=history)
3746

38-
readline.clear_history()
39-
assert readline.get_current_history_length() == 0
40-
readline.add_history('this is a test')
41-
assert readline.get_current_history_length() == 1
42-
readline.remove_history_item(0)
43-
assert readline.get_current_history_length() == 0
47+
session.history.get_strings().append('new command')
48+
assert 'new command' not in session.history.get_strings()
49+
assert len(history.get_strings()) == 2
4450

4551

4652
#
@@ -949,7 +955,7 @@ def test_history_file_bad_json(mocker, capsys) -> None:
949955
assert 'Error processing persistent history data' in err
950956

951957

952-
def test_history_populates_readline(hist_file) -> None:
958+
def test_history_populates_pt(hist_file) -> None:
953959
# - create a cmd2 with persistent history
954960
app = cmd2.Cmd(persistent_history_file=hist_file)
955961
run_cmd(app, 'help')
@@ -967,17 +973,14 @@ def test_history_populates_readline(hist_file) -> None:
967973
assert app.history.get(3).statement.raw == 'shortcuts'
968974
assert app.history.get(4).statement.raw == 'alias'
969975

970-
# readline only adds a single entry for multiple sequential identical commands
976+
# prompt-toolkit only adds a single entry for multiple sequential identical commands
971977
# so we check to make sure that cmd2 populated the readline history
972978
# using the same rules
973-
from cmd2.rl_utils import (
974-
readline,
975-
)
976-
977-
assert readline.get_current_history_length() == 3
978-
assert readline.get_history_item(1) == 'help'
979-
assert readline.get_history_item(2) == 'shortcuts'
980-
assert readline.get_history_item(3) == 'alias'
979+
pt_history = app.session.history.get_strings()
980+
assert len(pt_history) == 3
981+
assert pt_history.get(1) == 'help'
982+
assert pt_history.get(2) == 'shortcuts'
983+
assert pt_history.get(3) == 'alias'
981984

982985

983986
#

0 commit comments

Comments
 (0)