- restore compatibility to old getvalueorskip behaviour
- introduce a better NOTSET representation to improve docs
This commit is contained in:
parent
b61ed2cf7e
commit
d6281b4206
|
@ -560,7 +560,11 @@ class CmdOptions(object):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<CmdOptions %r>" %(self.__dict__,)
|
return "<CmdOptions %r>" %(self.__dict__,)
|
||||||
|
|
||||||
notset = object()
|
class Notset:
|
||||||
|
def __repr__(self):
|
||||||
|
return "<NOTSET>"
|
||||||
|
|
||||||
|
notset = Notset()
|
||||||
FILE_OR_DIR = 'file_or_dir'
|
FILE_OR_DIR = 'file_or_dir'
|
||||||
class Config(object):
|
class Config(object):
|
||||||
""" access to configuration values, pluginmanager and plugin hooks. """
|
""" access to configuration values, pluginmanager and plugin hooks. """
|
||||||
|
@ -798,11 +802,15 @@ class Config(object):
|
||||||
:arg name: name of the option. You may also specify
|
:arg name: name of the option. You may also specify
|
||||||
the literal ``--OPT`` option instead of the "dest" option name.
|
the literal ``--OPT`` option instead of the "dest" option name.
|
||||||
:arg default: default value if no option of that name exists.
|
:arg default: default value if no option of that name exists.
|
||||||
:arg skip: if True raise pytest.skip if not option exists.
|
:arg skip: if True raise pytest.skip if option does not exists
|
||||||
|
or has a None value.
|
||||||
"""
|
"""
|
||||||
name = self._opt2dest.get(name, name)
|
name = self._opt2dest.get(name, name)
|
||||||
try:
|
try:
|
||||||
return getattr(self.option, name)
|
val = getattr(self.option, name)
|
||||||
|
if val is None and skip:
|
||||||
|
raise AttributeError(name)
|
||||||
|
return val
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
if default is not notset:
|
if default is not notset:
|
||||||
return default
|
return default
|
||||||
|
|
|
@ -117,6 +117,15 @@ class TestConfigAPI:
|
||||||
verbose = config.getvalueorskip("verbose")
|
verbose = config.getvalueorskip("verbose")
|
||||||
assert verbose == config.option.verbose
|
assert verbose == config.option.verbose
|
||||||
|
|
||||||
|
def test_config_getvalueorskip_None(self, testdir):
|
||||||
|
testdir.makeconftest("""
|
||||||
|
def pytest_addoption(parser):
|
||||||
|
parser.addoption("--hello")
|
||||||
|
""")
|
||||||
|
config = testdir.parseconfig()
|
||||||
|
pytest.raises(pytest.skip.Exception,
|
||||||
|
"config.getvalueorskip('hello')")
|
||||||
|
|
||||||
def test_getoption(self, testdir):
|
def test_getoption(self, testdir):
|
||||||
config = testdir.parseconfig()
|
config = testdir.parseconfig()
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
|
|
Loading…
Reference in New Issue