diff --git a/_pytest/config.py b/_pytest/config.py index 29b3c3bbd..da595fef4 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -83,6 +83,7 @@ class Parser: self._inidict[name] = (help, type, default) self._ininames.append(name) + class OptionGroup: def __init__(self, name, description="", parser=None): self.name = name @@ -346,6 +347,14 @@ class Config(object): args.append(py.std.os.getcwd()) 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): """ return configuration value from an ini file. If the specified name hasn't been registered through a prior ``parse.addini`` diff --git a/testing/test_config.py b/testing/test_config.py index b54bd83a7..31e4eaf78 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -208,6 +208,40 @@ class TestConfigAPI: l = config.getini("a2") 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 runfiletest(opts): reprec = testdir.inline_run(*opts)