Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interactive test using pexpect #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions fancycompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ def has_leopard_libedit(config):
return config.readline.__doc__ and 'libedit' in config.readline.__doc__


def setup():
def setup(**kwargs):
"""
Install fancycompleter as the default completer for readline.
"""
completer = Completer()
completer = Completer(**kwargs)
readline = completer.config.readline
if has_leopard_libedit(completer.config):
readline.parse_and_bind("bind ^I rl_complete")
Expand Down Expand Up @@ -390,7 +390,7 @@ def save_history():
atexit.register(save_history)


def interact(persist_history=None):
def interact(persist_history=None, **kwargs):
"""
Main entry point for fancycompleter: run an interactive Python session
after installing fancycompleter.
Expand All @@ -411,7 +411,7 @@ def interact(persist_history=None):
By default, pyrepl is preferred and automatically used if found.
"""
import sys
completer = setup()
completer = setup(**kwargs)
if persist_history:
setup_history(completer, persist_history)
if completer.config.using_pyrepl and '__pypy__' not in sys.builtin_module_names:
Expand Down
24 changes: 24 additions & 0 deletions testing/fcomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import cmd
import sys

from fancycompleter import interact, DefaultConfig


class ConfigForTest(DefaultConfig):
use_colors = False


class CompleterCmd(cmd.Cmd):
prompt = ''


if __name__ == '__main__':
globals().update({
name: name
for name in sys.argv[1:]
})

interact(Config=ConfigForTest)

repl = CompleterCmd(completekey=None)
repl.cmdloop(intro='')
42 changes: 42 additions & 0 deletions testing/test_interactive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os.path
import time

import pexpect


TEST_DIR = os.path.dirname(__file__)
FCOMPLETE_PATH = os.path.join(TEST_DIR, 'fcomplete.py')


def test_global_matches():
prefix = 'complete_'
names = ['a', 'b', 'c']
full_names = [prefix + name for name in names]

args = [FCOMPLETE_PATH]
args.extend(full_names)

fcomplete = pexpect.spawn('python', args)

try:
fcomplete.send(prefix)
output = fcomplete.read_nonblocking(1000).decode('utf-8')
assert output == prefix

fcomplete.send('\t')
fcomplete.read_nonblocking(1000).decode('utf-8')

fcomplete.send('\t')
time.sleep(0.1)
output = fcomplete.read_nonblocking(1000).decode('utf-8')
lines = output.split('\r\n')

assert len(lines) == 3
assert lines[2].endswith(prefix)

assert all(full_name in lines[1] for full_name in full_names)
completed_names = lines[1].strip().split(' ')
assert set(completed_names) == set(full_names)

finally:
fcomplete.terminate(force=True)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ envlist = py{27,34,35,36,37,38,py,py3}, checkqa
[testenv]
deps =
pytest
pexpect
coverage: pytest-cov
commands = pytest {posargs}
setenv =
Expand Down