Added basic fix and test

This commit is contained in:
Andrew Toolan 2018-01-20 18:23:08 +00:00 committed by Bruno Oliveira
parent 3b3d237f07
commit 2c7f94fdb9
2 changed files with 41 additions and 1 deletions

View File

@ -1192,12 +1192,15 @@ class Config(object):
# and -o foo1=bar1 -o foo2=bar2 options
# always use the last item if multiple value set for same ini-name,
# e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2
first_override_set = False
for ini_config_list in self._override_ini:
for ini_config in ini_config_list:
try:
(key, user_ini_value) = ini_config.split("=", 1)
first_override_set = True
except ValueError:
raise UsageError("-o/--override-ini expects option=value style.")
if not first_override_set:
raise UsageError("-o/--override-ini expects option=value style.")
if key == name:
value = user_ini_value
return value

View File

@ -860,3 +860,40 @@ class TestOverrideIniArgs(object):
config = get_config()
config._preparse([], addopts=True)
assert config._override_ini == [['cache_dir=%s' % cache_dir]]
def test_all_the_things(self, testdir):
testdir.makeconftest("""
def pytest_addoption(parser):
addini = parser.addini
addini("custom_option_1", "", default="o1")
addini("custom_option_2", "", default="o2")""")
testdir.makepyfile("""
def test_multiple_options(pytestconfig):
prefix = "custom_option"
for x in range(1, 3):
ini_value=pytestconfig.getini("%s_%d" % (prefix, x))
print('\\nini%d:%s' % (x, ini_value))""")
result = testdir.runpytest(
"--override-ini", 'custom_option_1=fulldir=/tmp/user1',
'custom_option_2=url=/tmp/user2?a=b&d=e',
"test_all_the_things.py")
assert "ERROR: -o/--override-ini expects option=value style." not in result.stderr.str()
def test_throw_exception_if_not_value_pair(self, testdir):
testdir.makeconftest("""
def pytest_addoption(parser):
addini = parser.addini
addini("custom_option_1", "", default="o1")
addini("custom_option_2", "", default="o2")""")
testdir.makepyfile("""
def test_multiple_options(pytestconfig):
prefix = "custom_option"
for x in range(1, 3):
ini_value=pytestconfig.getini("%s_%d" % (prefix, x))
print('\\nini%d:%s' % (x, ini_value))""")
result = testdir.runpytest(
"--override-ini", 'custom_option_1',
"test_all_the_things.py")
assert "ERROR: -o/--override-ini expects option=value style." in result.stderr.str()