2013-07-26 14:59:31 +08:00
|
|
|
from __future__ import with_statement
|
2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest import config as parseopt
|
2010-10-31 01:23:50 +08:00
|
|
|
from textwrap import dedent
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestParser:
|
2010-10-28 04:29:01 +08:00
|
|
|
def test_no_help_by_default(self, capsys):
|
2009-02-27 18:18:27 +08:00
|
|
|
parser = parseopt.Parser(usage="xyz")
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(SystemExit, 'parser.parse(["-h"])')
|
2009-07-31 20:21:02 +08:00
|
|
|
out, err = capsys.readouterr()
|
2013-07-25 21:33:43 +08:00
|
|
|
assert err.find("error: unrecognized arguments") != -1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2013-07-25 21:33:43 +08:00
|
|
|
def test_argument(self):
|
|
|
|
with pytest.raises(parseopt.ArgumentError):
|
|
|
|
# need a short or long option
|
|
|
|
argument = parseopt.Argument()
|
|
|
|
argument = parseopt.Argument('-t')
|
|
|
|
assert argument._short_opts == ['-t']
|
|
|
|
assert argument._long_opts == []
|
|
|
|
assert argument.dest == 't'
|
|
|
|
argument = parseopt.Argument('-t', '--test')
|
|
|
|
assert argument._short_opts == ['-t']
|
|
|
|
assert argument._long_opts == ['--test']
|
|
|
|
assert argument.dest == 'test'
|
|
|
|
argument = parseopt.Argument('-t', '--test', dest='abc')
|
|
|
|
assert argument.dest == 'abc'
|
|
|
|
|
|
|
|
def test_argument_type(self):
|
|
|
|
argument = parseopt.Argument('-t', dest='abc', type='int')
|
|
|
|
assert argument.type is int
|
|
|
|
argument = parseopt.Argument('-t', dest='abc', type='string')
|
|
|
|
assert argument.type is str
|
|
|
|
argument = parseopt.Argument('-t', dest='abc', type=float)
|
|
|
|
assert argument.type is float
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
argument = parseopt.Argument('-t', dest='abc', type='choice')
|
|
|
|
argument = parseopt.Argument('-t', dest='abc', type='choice',
|
|
|
|
choices=['red', 'blue'])
|
|
|
|
assert argument.type is str
|
|
|
|
|
|
|
|
def test_argument_processopt(self):
|
|
|
|
argument = parseopt.Argument('-t', type=int)
|
|
|
|
argument.default = 42
|
|
|
|
argument.dest = 'abc'
|
|
|
|
res = argument.attrs()
|
|
|
|
assert res['default'] == 42
|
|
|
|
assert res['dest'] == 'abc'
|
2013-07-26 14:59:31 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_group_add_and_get(self):
|
|
|
|
parser = parseopt.Parser()
|
2010-10-14 00:41:53 +08:00
|
|
|
group = parser.getgroup("hello", description="desc")
|
2009-02-27 18:18:27 +08:00
|
|
|
assert group.name == "hello"
|
|
|
|
assert group.description == "desc"
|
2009-10-17 23:43:33 +08:00
|
|
|
|
|
|
|
def test_getgroup_simple(self):
|
2009-04-13 18:33:01 +08:00
|
|
|
parser = parseopt.Parser()
|
|
|
|
group = parser.getgroup("hello", description="desc")
|
|
|
|
assert group.name == "hello"
|
|
|
|
assert group.description == "desc"
|
|
|
|
group2 = parser.getgroup("hello")
|
|
|
|
assert group2 is group
|
|
|
|
|
2009-10-17 23:43:33 +08:00
|
|
|
def test_group_ordering(self):
|
|
|
|
parser = parseopt.Parser()
|
|
|
|
group0 = parser.getgroup("1")
|
|
|
|
group1 = parser.getgroup("2")
|
|
|
|
group1 = parser.getgroup("3", after="1")
|
|
|
|
groups = parser._groups
|
|
|
|
groups_names = [x.name for x in groups]
|
|
|
|
assert groups_names == list("132")
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_group_addoption(self):
|
|
|
|
group = parseopt.OptionGroup("hello")
|
|
|
|
group.addoption("--option1", action="store_true")
|
|
|
|
assert len(group.options) == 1
|
2013-07-25 21:33:43 +08:00
|
|
|
assert isinstance(group.options[0], parseopt.Argument)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_group_shortopt_lowercase(self):
|
|
|
|
parser = parseopt.Parser()
|
2010-10-14 00:41:53 +08:00
|
|
|
group = parser.getgroup("hello")
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ValueError, """
|
2009-02-27 18:18:27 +08:00
|
|
|
group.addoption("-x", action="store_true")
|
|
|
|
""")
|
|
|
|
assert len(group.options) == 0
|
|
|
|
group._addoption("-x", action="store_true")
|
|
|
|
assert len(group.options) == 1
|
|
|
|
|
|
|
|
def test_parser_addoption(self):
|
|
|
|
parser = parseopt.Parser()
|
2009-03-26 20:21:05 +08:00
|
|
|
group = parser.getgroup("custom options")
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len(group.options) == 0
|
|
|
|
group.addoption("--option1", action="store_true")
|
|
|
|
assert len(group.options) == 1
|
|
|
|
|
|
|
|
def test_parse(self):
|
|
|
|
parser = parseopt.Parser()
|
|
|
|
parser.addoption("--hello", dest="hello", action="store")
|
2013-07-25 21:33:43 +08:00
|
|
|
args = parser.parse(['--hello', 'world'])
|
|
|
|
assert args.hello == "world"
|
|
|
|
assert not getattr(args, parseopt.Config._file_or_dir)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2013-07-25 21:33:43 +08:00
|
|
|
def test_parse2(self):
|
2009-02-27 18:18:27 +08:00
|
|
|
parser = parseopt.Parser()
|
2013-07-25 21:33:43 +08:00
|
|
|
args = parser.parse([py.path.local()])
|
|
|
|
assert getattr(args, parseopt.Config._file_or_dir)[0] == py.path.local()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_parse_will_set_default(self):
|
|
|
|
parser = parseopt.Parser()
|
|
|
|
parser.addoption("--hello", dest="hello", default="x", action="store")
|
2013-07-25 21:33:43 +08:00
|
|
|
option = parser.parse([])
|
2009-02-27 18:18:27 +08:00
|
|
|
assert option.hello == "x"
|
|
|
|
del option.hello
|
|
|
|
args = parser.parse_setoption([], option)
|
|
|
|
assert option.hello == "x"
|
|
|
|
|
|
|
|
def test_parse_setoption(self):
|
|
|
|
parser = parseopt.Parser()
|
|
|
|
parser.addoption("--hello", dest="hello", action="store")
|
|
|
|
parser.addoption("--world", dest="world", default=42)
|
|
|
|
class A: pass
|
|
|
|
option = A()
|
|
|
|
args = parser.parse_setoption(['--hello', 'world'], option)
|
|
|
|
assert option.hello == "world"
|
|
|
|
assert option.world == 42
|
|
|
|
assert not args
|
|
|
|
|
2013-07-25 21:33:43 +08:00
|
|
|
def test_parse_special_destination(self):
|
|
|
|
parser = parseopt.Parser()
|
|
|
|
x = parser.addoption("--ultimate-answer", type=int)
|
|
|
|
args = parser.parse(['--ultimate-answer', '42'])
|
|
|
|
assert args.ultimate_answer == 42
|
2013-07-26 14:59:31 +08:00
|
|
|
|
2013-07-30 17:26:15 +08:00
|
|
|
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
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_parse_defaultgetter(self):
|
|
|
|
def defaultget(option):
|
2013-07-25 21:33:43 +08:00
|
|
|
if not hasattr(option, 'type'):
|
|
|
|
return
|
|
|
|
if option.type is int:
|
2009-02-27 18:18:27 +08:00
|
|
|
option.default = 42
|
2013-07-25 21:33:43 +08:00
|
|
|
elif option.type is str:
|
2009-02-27 18:18:27 +08:00
|
|
|
option.default = "world"
|
|
|
|
parser = parseopt.Parser(processopt=defaultget)
|
|
|
|
parser.addoption("--this", dest="this", type="int", action="store")
|
|
|
|
parser.addoption("--hello", dest="hello", type="string", action="store")
|
|
|
|
parser.addoption("--no", dest="no", action="store_true")
|
2013-07-25 21:33:43 +08:00
|
|
|
option = parser.parse([])
|
2009-02-27 18:18:27 +08:00
|
|
|
assert option.hello == "world"
|
|
|
|
assert option.this == 42
|
2013-07-25 21:33:43 +08:00
|
|
|
assert option.no is False
|
2010-10-31 01:23:50 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,5)")
|
2009-10-15 22:18:57 +08:00
|
|
|
def test_addoption_parser_epilog(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_addoption(parser):
|
2010-01-03 05:48:53 +08:00
|
|
|
parser.hints.append("hello world")
|
2013-07-25 21:33:43 +08:00
|
|
|
parser.hints.append("from me too")
|
2009-10-15 22:18:57 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest('--help')
|
|
|
|
#assert result.ret != 0
|
2013-07-25 21:33:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["hint: hello world", "hint: from me too"])
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2013-07-30 17:26:15 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,5)")
|
|
|
|
def test_argcomplete(testdir):
|
2013-07-30 18:33:38 +08:00
|
|
|
if not py.path.local.sysfind('bash'):
|
2013-07-31 22:03:53 +08:00
|
|
|
pytest.skip("bash not available")
|
2013-07-30 17:26:15 +08:00
|
|
|
import os
|
2013-07-30 18:33:38 +08:00
|
|
|
script = os.path.join(os.getcwd(), 'test_argcomplete')
|
2013-07-30 17:26:15 +08:00
|
|
|
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')
|
2013-07-31 22:03:53 +08:00
|
|
|
# alternative would be exteneded Testdir.{run(),_run(),popen()} to be able
|
|
|
|
# to handle a keyword argument env that replaces os.environ in popen or
|
|
|
|
# extends the copy, advantage: could not forget to restore
|
|
|
|
orgenv = os.environ.copy()
|
2013-07-30 17:26:15 +08:00
|
|
|
os.environ['_ARGCOMPLETE'] = "1"
|
|
|
|
os.environ['_ARGCOMPLETE_IFS'] = "\x0b"
|
|
|
|
os.environ['COMP_WORDBREAKS'] = ' \\t\\n"\\\'><=;|&(:'
|
|
|
|
|
2013-07-30 18:33:38 +08:00
|
|
|
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)
|
2013-07-30 17:26:15 +08:00
|
|
|
print dir(result), result.ret
|
|
|
|
if result.ret == 255:
|
|
|
|
# argcomplete not found
|
2013-07-30 18:33:38 +08:00
|
|
|
pytest.skip("argcomplete not available")
|
2013-07-30 17:26:15 +08:00
|
|
|
else:
|
|
|
|
result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"])
|
2013-07-30 18:33:38 +08:00
|
|
|
|
|
|
|
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/"])
|
2013-07-31 22:03:53 +08:00
|
|
|
# restore environment
|
|
|
|
os.environ = orgenv.copy()
|