Skip to content

Instantly share code, notes, and snippets.

@meeuw
Last active October 13, 2018 14:30
Show Gist options
  • Save meeuw/584c5be28b203351a015cec4a9517fd4 to your computer and use it in GitHub Desktop.
Save meeuw/584c5be28b203351a015cec4a9517fd4 to your computer and use it in GitHub Desktop.
reproduce prompt toolkit assert using pexpect and click
#!/usr/bin/env python
import sys
if 'prompt' in sys.argv:
from prompt_toolkit.shortcuts import PromptSession, CompleteStyle
import click
prompt_app = PromptSession(message='>')
query = ''
while 1:
try:
text = prompt_app.prompt(default=query)
except:
query = ''
if 'edit' in text:
query = click.edit()
elif 'quit' in text:
print("bye bye")
break
else:
import pexpect
import os
import time
os.environ['EDITOR'] = 'ex'
cli = pexpect.spawnu('./pttest.py prompt')
cli.logfile = open('/tmp/pttest.log', 'w')
cli.expect_exact('>', timeout=2)
cli.sendline('edit')
cli.expect_exact('Entering Ex mode. Type "visual" to go to Normal mode.', timeout=2)
cli.sendline('i')
cli.sendline('select * from abc')
cli.sendline('.')
cli.expect_exact('\r\n:', timeout=2)
cli.sendline('x')
cli.expect_exact("written", timeout=2)
for match in 'select * from abc'.split(' '):
cli.expect_exact(match, timeout=2)
cli.sendcontrol('c')
cli.expect_exact('>', timeout=2)
cli.sendline('quit')
cli.expect_exact('bye bye', timeout=2)
cli.wait()
cli.logfile.close()
with open('/tmp/pttest.log') as f:
print(f.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment