From a965386b9edba40e06181eca7f695b5e643e7eca Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 14 Feb 2016 19:27:48 -0200 Subject: [PATCH] Add bool type to addini --- _pytest/config.py | 25 +++++++++++++++++++++++-- testing/test_config.py | 15 +++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 761b0e52e..70e19b1d1 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -502,13 +502,14 @@ class Parser: """ register an ini-file option. :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. The value of ini-variables can be retrieved via a call to :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._ininames.append(name) @@ -1011,6 +1012,8 @@ class Config(object): return shlex.split(value) elif type == "linelist": return [t for t in map(lambda x: x.strip(), value.split("\n")) if t] + elif type == "bool": + return bool(_strtobool(value.strip())) else: assert type is None return value @@ -1164,3 +1167,21 @@ def create_terminal_writer(config, *args, **kwargs): if config.option.color == 'no': tw.hasmarkup = False 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,)) diff --git a/testing/test_config.py b/testing/test_config.py index 5a984b35d..92c9bdb8b 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -231,6 +231,21 @@ class TestConfigAPI: l = config.getini("a2") 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): testdir.makeconftest(""" def pytest_addoption(parser):