Skip to content

Commit 7d4cbc4

Browse files
committed
Make sure pexpect uses same version of Python to spawn persistent history example
1 parent 8209a5a commit 7d4cbc4

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

examples/persistent_history.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010

1111
class Cmd2PersistentHistory(cmd2.Cmd):
1212
"""Basic example of how to enable persistent readline history within your cmd2 app."""
13-
def __init__(self):
14-
""""""
15-
cmd2.Cmd.__init__(self, persistent_history_file='~/.persistent_history.cmd2', persistent_history_length=500)
13+
def __init__(self, hist_file):
14+
"""Configure the app to load persistent readline history from a file.
15+
16+
:param hist_file: file to load readline history from at start and write it to at end
17+
"""
18+
cmd2.Cmd.__init__(self, persistent_history_file=hist_file, persistent_history_length=500)
19+
self.allow_cli_args = False
1620
self.prompt = 'ph> '
1721

1822
# ... your class code here ...
1923

2024

2125
if __name__ == '__main__':
22-
app = Cmd2PersistentHistory()
26+
import sys
27+
28+
history_file = '~/.persistent_history.cmd2'
29+
if len(sys.argv) > 1:
30+
history_file = sys.argv[1]
31+
32+
app = Cmd2PersistentHistory(hist_file=history_file)
2333
app.cmdloop()

tests/test_cmd2.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,14 @@ def test_persistent_history(request):
15321532
test_dir = os.path.dirname(request.module.__file__)
15331533
persistent_app = os.path.join(test_dir, '..', 'examples', 'persistent_history.py')
15341534

1535+
python = 'python3'
1536+
if six.PY2:
1537+
python = 'python2'
1538+
1539+
command = '{} {}'.format(python, persistent_app)
1540+
15351541
# Start an instance of the persistent history example and send it a few commands
1536-
child = pexpect.spawn(persistent_app)
1542+
child = pexpect.spawn(command)
15371543
prompt = 'ph> '
15381544
child.expect(prompt)
15391545
child.sendline('help')
@@ -1545,10 +1551,9 @@ def test_persistent_history(request):
15451551

15461552
# Start a 2nd instance of the persistent history example and send it an up arrow to verify persistent history
15471553
up_arrow = '\x1b[A'
1548-
child2 = pexpect.spawn(persistent_app)
1554+
child2 = pexpect.spawn(command)
15491555
child2.expect(prompt)
15501556
child2.send(up_arrow)
15511557
child2.expect('quit')
15521558
assert child2.after == b'quit'
1553-
1554-
1559+
child2.close()

0 commit comments

Comments
 (0)