add a method to the config object to dynamically add a value to an (line-type) ini-value
This commit is contained in:
parent
67fbd24ebf
commit
979dfd20f2
|
@ -83,6 +83,7 @@ class Parser:
|
||||||
self._inidict[name] = (help, type, default)
|
self._inidict[name] = (help, type, default)
|
||||||
self._ininames.append(name)
|
self._ininames.append(name)
|
||||||
|
|
||||||
|
|
||||||
class OptionGroup:
|
class OptionGroup:
|
||||||
def __init__(self, name, description="", parser=None):
|
def __init__(self, name, description="", parser=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -346,6 +347,14 @@ class Config(object):
|
||||||
args.append(py.std.os.getcwd())
|
args.append(py.std.os.getcwd())
|
||||||
self.args = args
|
self.args = args
|
||||||
|
|
||||||
|
def addinivalue_line(self, name, line):
|
||||||
|
""" add a line to an ini-file option. The option must have been
|
||||||
|
declared but might not yet be set in which case the line becomes the
|
||||||
|
the first line in its value. """
|
||||||
|
x = self.getini(name)
|
||||||
|
assert isinstance(x, list)
|
||||||
|
x.append(line) # modifies the cached list inline
|
||||||
|
|
||||||
def getini(self, name):
|
def getini(self, name):
|
||||||
""" return configuration value from an ini file. If the
|
""" return configuration value from an ini file. If the
|
||||||
specified name hasn't been registered through a prior ``parse.addini``
|
specified name hasn't been registered through a prior ``parse.addini``
|
||||||
|
|
|
@ -208,6 +208,40 @@ class TestConfigAPI:
|
||||||
l = config.getini("a2")
|
l = config.getini("a2")
|
||||||
assert l == []
|
assert l == []
|
||||||
|
|
||||||
|
def test_addinivalue_line_existing(self, testdir):
|
||||||
|
testdir.makeconftest("""
|
||||||
|
def pytest_addoption(parser):
|
||||||
|
parser.addini("xy", "", type="linelist")
|
||||||
|
""")
|
||||||
|
p = testdir.makeini("""
|
||||||
|
[pytest]
|
||||||
|
xy= 123
|
||||||
|
""")
|
||||||
|
config = testdir.parseconfig()
|
||||||
|
l = config.getini("xy")
|
||||||
|
assert len(l) == 1
|
||||||
|
assert l == ["123"]
|
||||||
|
config.addinivalue_line("xy", "456")
|
||||||
|
l = config.getini("xy")
|
||||||
|
assert len(l) == 2
|
||||||
|
assert l == ["123", "456"]
|
||||||
|
|
||||||
|
def test_addinivalue_line_new(self, testdir):
|
||||||
|
testdir.makeconftest("""
|
||||||
|
def pytest_addoption(parser):
|
||||||
|
parser.addini("xy", "", type="linelist")
|
||||||
|
""")
|
||||||
|
config = testdir.parseconfig()
|
||||||
|
assert not config.getini("xy")
|
||||||
|
config.addinivalue_line("xy", "456")
|
||||||
|
l = config.getini("xy")
|
||||||
|
assert len(l) == 1
|
||||||
|
assert l == ["456"]
|
||||||
|
config.addinivalue_line("xy", "123")
|
||||||
|
l = config.getini("xy")
|
||||||
|
assert len(l) == 2
|
||||||
|
assert l == ["456", "123"]
|
||||||
|
|
||||||
def test_options_on_small_file_do_not_blow_up(testdir):
|
def test_options_on_small_file_do_not_blow_up(testdir):
|
||||||
def runfiletest(opts):
|
def runfiletest(opts):
|
||||||
reprec = testdir.inline_run(*opts)
|
reprec = testdir.inline_run(*opts)
|
||||||
|
|
Loading…
Reference in New Issue