From 2c7f94fdb94d166593384ec3455a52654a735e8b Mon Sep 17 00:00:00 2001 From: Andrew Toolan Date: Sat, 20 Jan 2018 18:23:08 +0000 Subject: [PATCH] Added basic fix and test --- _pytest/config.py | 5 ++++- testing/test_config.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/_pytest/config.py b/_pytest/config.py index 22bf6c60c..544c260ee 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -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 diff --git a/testing/test_config.py b/testing/test_config.py index 44b8c317a..f958b5890 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -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()