Merged in anthon_van_der_neut/pytest/argcomplete (pull request #50)

Fixes for argcomplete
This commit is contained in:
holger krekel 2013-07-31 07:51:07 +02:00
commit 8a0a880294
5 changed files with 161 additions and 9 deletions

67
_pytest/_argcomplete.py Normal file
View File

@ -0,0 +1,67 @@
"""allow bash-completion for argparse with argcomplete if installed
needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail
to find the magic string, so _ARGCOMPLETE env. var is never set, and
this does not need special code.
argcomplete does not support python 2.5 (although the changes for that
are minor).
Function try_argcomplete(parser) should be called directly before
the call to ArgumentParser.parse_args().
The filescompleter is what you normally would use on the positional
arguments specification, in order to get "dirname/" after "dirn<TAB>"
instead of the default "dirname ":
optparser.add_argument(Config._file_or_dir, nargs='*'
).completer=filescompleter
Other, application specific, completers should go in the file
doing the add_argument calls as they need to be specified as .completer
attributes as well. (If argcomplete is not installed, the function the
attribute points to will not be used).
---
To include this support in another application that has setup.py generated
scripts:
- add the line:
# PYTHON_ARGCOMPLETE_OK
near the top of the main python entry point
- include in the file calling parse_args():
from _argcomplete import try_argcomplete, filescompleter
, call try_argcomplete just before parse_args(), and optionally add
filescompleter to the positional arguments' add_argument()
If things do not work right away:
- switch on argcomplete debugging with (also helpful when doing custom
completers):
export _ARC_DEBUG=1
- run:
python-argcomplete-check-easy-install-script $(which appname)
echo $?
will echo 0 if the magic line has been found, 1 if not
- sometimes it helps to find early on errors using:
_ARGCOMPLETE=1 _ARC_DEBUG=1 appname
which should throw a KeyError: 'COMPLINE' (which is properly set by the
global argcomplete script).
"""
import sys
import os
if os.environ.get('_ARGCOMPLETE'):
# argcomplete 0.5.6 is not compatible with python 2.5.6: print/with/format
if sys.version_info[:2] < (2, 6):
sys.exit(1)
try:
import argcomplete.completers
except ImportError:
sys.exit(-1)
filescompleter = argcomplete.completers.FilesCompleter()
def try_argcomplete(parser):
argcomplete.autocomplete(parser)
else:
def try_argcomplete(parser): pass
filescompleter = None

View File

@ -4,6 +4,7 @@ import py
import sys, os
from _pytest.core import PluginManager
import pytest
from _argcomplete import try_argcomplete, filescompleter
# enable after some grace period for plugin writers
TYPE_WARN = False
@ -91,7 +92,9 @@ class Parser:
n = option.names()
a = option.attrs()
arggroup.add_argument(*n, **a)
optparser.add_argument(Config._file_or_dir, nargs='*')
# bash like autocompletion for dirs (appending '/')
optparser.add_argument(Config._file_or_dir, nargs='*'
).completer=filescompleter
try_argcomplete(self.optparser)
return self.optparser.parse_args([str(x) for x in args])
@ -115,13 +118,6 @@ class Parser:
self._inidict[name] = (help, type, default)
self._ininames.append(name)
def try_argcomplete(parser):
try:
import argcomplete
except ImportError:
pass
else:
argcomplete.autocomplete(parser)
class ArgumentError(Exception):
"""
@ -304,6 +300,7 @@ class MyOptionParser(py.std.argparse.ArgumentParser):
self._parser = parser
py.std.argparse.ArgumentParser.__init__(self, usage=parser._usage,
add_help=False)
def format_epilog(self, formatter):
hints = self._parser.hints
if hints:
@ -312,6 +309,18 @@ class MyOptionParser(py.std.argparse.ArgumentParser):
return s
return ""
def parse_args(self, args=None, namespace=None):
"""allow splitting of positional arguments"""
args, argv = self.parse_known_args(args, namespace)
if argv:
for arg in argv:
if arg and arg[0] == '-':
msg = py.std.argparse._('unrecognized arguments: %s')
self.error(msg % ' '.join(argv))
getattr(args, Config._file_or_dir).extend(argv)
return args
class Conftest(object):
""" the single place for accessing values and interacting
towards conftest modules from py.test objects.

View File

@ -0,0 +1,28 @@
.. _bash_completion:
Setting up bash completion
==========================
When using bash as your shell, ``py.test`` can use argcomplete
(https://argcomplete.readthedocs.org/) for auto-completion.
For this ``argcomplete`` needs to be installed **and** enabled.
Install argcomplete using::
sudo pip install 'argcomplete>=0.5.7'
For global activation of all argcomplete enabled python applications run::
sudo activate-global-python-argcomplete
For permanent (but not global) ``py.test`` activation, use::
register-python-argcomplete py.test >> ~/.bashrc
For one-time activation of argcomplete for ``py.test`` only, use::
eval "$(register-python-argcomplete py.test)"

View File

@ -130,6 +130,21 @@ class TestParser:
args = parser.parse(['--ultimate-answer', '42'])
assert args.ultimate_answer == 42
def test_parse_split_positional_arguments(self):
parser = parseopt.Parser()
parser.addoption("-R", action='store_true')
parser.addoption("-S", action='store_false')
args = parser.parse(['-R', '4', '2', '-S'])
assert getattr(args, parseopt.Config._file_or_dir) == ['4', '2']
args = parser.parse(['-R', '-S', '4', '2', '-R'])
assert getattr(args, parseopt.Config._file_or_dir) == ['4', '2']
assert args.R == True
assert args.S == False
args = parser.parse(['-R', '4', '-S', '2'])
assert getattr(args, parseopt.Config._file_or_dir) == ['4', '2']
assert args.R == True
assert args.S == False
def test_parse_defaultgetter(self):
def defaultget(option):
if not hasattr(option, 'type'):
@ -158,3 +173,36 @@ def test_addoption_parser_epilog(testdir):
#assert result.ret != 0
result.stdout.fnmatch_lines(["hint: hello world", "hint: from me too"])
@pytest.mark.skipif("sys.version_info < (2,5)")
def test_argcomplete(testdir):
if not py.path.local.sysfind('bash'):
pytest.skip("bash not available")
import os
script = os.path.join(os.getcwd(), 'test_argcomplete')
with open(str(script), 'w') as fp:
# redirect output from argcomplete to stdin and stderr is not trivial
# http://stackoverflow.com/q/12589419/1307905
# so we use bash
fp.write('COMP_WORDBREAKS="$COMP_WORDBREAKS" $(which py.test) '
'8>&1 9>&2')
os.environ['_ARGCOMPLETE'] = "1"
os.environ['_ARGCOMPLETE_IFS'] = "\x0b"
os.environ['COMP_WORDBREAKS'] = ' \\t\\n"\\\'><=;|&(:'
arg = '--fu'
os.environ['COMP_LINE'] = "py.test " + arg
os.environ['COMP_POINT'] = str(len(os.environ['COMP_LINE']))
result = testdir.run('bash', str(script), arg)
print dir(result), result.ret
if result.ret == 255:
# argcomplete not found
pytest.skip("argcomplete not available")
else:
result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"])
os.mkdir('test_argcomplete.d')
arg = 'test_argc'
os.environ['COMP_LINE'] = "py.test " + arg
os.environ['COMP_POINT'] = str(len(os.environ['COMP_LINE']))
result = testdir.run('bash', str(script), arg)
result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"])

View File

@ -43,7 +43,7 @@ changedir=.
deps=twisted
pexpect
commands=
py.test -rsxf testing/test_unittest.py \
py.test -rsxf \
--junitxml={envlogdir}/junit-{envname}.xml {posargs:testing/test_unittest.py}
[testenv:doctest]
changedir=.