Add bool type to addini
This commit is contained in:
parent
6f0d90cd5a
commit
a965386b9e
|
@ -502,13 +502,14 @@ class Parser:
|
||||||
""" register an ini-file option.
|
""" register an ini-file option.
|
||||||
|
|
||||||
:name: name of the ini-variable
|
:name: name of the ini-variable
|
||||||
:type: type of the variable, can be ``pathlist``, ``args`` or ``linelist``.
|
:type: type of the variable, can be ``pathlist``, ``args``, ``linelist``
|
||||||
|
or ``bool``.
|
||||||
:default: default value if no ini-file option exists but is queried.
|
:default: default value if no ini-file option exists but is queried.
|
||||||
|
|
||||||
The value of ini-variables can be retrieved via a call to
|
The value of ini-variables can be retrieved via a call to
|
||||||
:py:func:`config.getini(name) <_pytest.config.Config.getini>`.
|
:py:func:`config.getini(name) <_pytest.config.Config.getini>`.
|
||||||
"""
|
"""
|
||||||
assert type in (None, "pathlist", "args", "linelist")
|
assert type in (None, "pathlist", "args", "linelist", "bool")
|
||||||
self._inidict[name] = (help, type, default)
|
self._inidict[name] = (help, type, default)
|
||||||
self._ininames.append(name)
|
self._ininames.append(name)
|
||||||
|
|
||||||
|
@ -1011,6 +1012,8 @@ class Config(object):
|
||||||
return shlex.split(value)
|
return shlex.split(value)
|
||||||
elif type == "linelist":
|
elif type == "linelist":
|
||||||
return [t for t in map(lambda x: x.strip(), value.split("\n")) if t]
|
return [t for t in map(lambda x: x.strip(), value.split("\n")) if t]
|
||||||
|
elif type == "bool":
|
||||||
|
return bool(_strtobool(value.strip()))
|
||||||
else:
|
else:
|
||||||
assert type is None
|
assert type is None
|
||||||
return value
|
return value
|
||||||
|
@ -1164,3 +1167,21 @@ def create_terminal_writer(config, *args, **kwargs):
|
||||||
if config.option.color == 'no':
|
if config.option.color == 'no':
|
||||||
tw.hasmarkup = False
|
tw.hasmarkup = False
|
||||||
return tw
|
return tw
|
||||||
|
|
||||||
|
|
||||||
|
def _strtobool(val):
|
||||||
|
"""Convert a string representation of truth to true (1) or false (0).
|
||||||
|
|
||||||
|
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
|
||||||
|
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
|
||||||
|
'val' is anything else.
|
||||||
|
|
||||||
|
.. note:: copied from distutils.util
|
||||||
|
"""
|
||||||
|
val = val.lower()
|
||||||
|
if val in ('y', 'yes', 't', 'true', 'on', '1'):
|
||||||
|
return 1
|
||||||
|
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
raise ValueError("invalid truth value %r" % (val,))
|
||||||
|
|
|
@ -231,6 +231,21 @@ class TestConfigAPI:
|
||||||
l = config.getini("a2")
|
l = config.getini("a2")
|
||||||
assert l == []
|
assert l == []
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('str_val, bool_val',
|
||||||
|
[('True', True), ('no', False), ('no-ini', True)])
|
||||||
|
def test_addini_bool(self, testdir, str_val, bool_val):
|
||||||
|
testdir.makeconftest("""
|
||||||
|
def pytest_addoption(parser):
|
||||||
|
parser.addini("strip", "", type="bool", default=True)
|
||||||
|
""")
|
||||||
|
if str_val != 'no-ini':
|
||||||
|
testdir.makeini("""
|
||||||
|
[pytest]
|
||||||
|
strip=%s
|
||||||
|
""" % str_val)
|
||||||
|
config = testdir.parseconfig()
|
||||||
|
assert config.getini("strip") is bool_val
|
||||||
|
|
||||||
def test_addinivalue_line_existing(self, testdir):
|
def test_addinivalue_line_existing(self, testdir):
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
|
|
Loading…
Reference in New Issue