Skip to content

Commit 9eb1d73

Browse files
committed
--override-ini now correctly overrides some fundamental options like "python_files"
#2238
1 parent 3d9c5cf commit 9eb1d73

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
* Ignore exceptions raised from descriptors (e.g. properties) during Python test collection (`#2234`_).
88
Thanks to `@bluetech`_.
99

10-
*
10+
* ``--override-ini`` now correctly overrides some fundamental options like ``python_files`` (`#2238`_).
11+
Thanks `@sirex`_ for the report and `@nicoddemus`_ for the PR.
1112

1213
* Replace ``raise StopIteration`` usages in the code by simple ``returns`` to finish generators, in accordance to `PEP-479`_ (`#2160`_).
1314
Thanks `@tgoodlet`_ for the report and `@nicoddemus`_ for the PR.
@@ -28,12 +29,14 @@
2829

2930
.. _@bluetech: https://github.com/bluetech
3031
.. _@gst: https://github.com/gst
32+
.. _@sirex: https://github.com/sirex
3133
.. _@vidartf: https://github.com/vidartf
3234

3335
.. _#2137: https://github.com/pytest-dev/pytest/issues/2137
3436
.. _#2160: https://github.com/pytest-dev/pytest/issues/2160
3537
.. _#2231: https://github.com/pytest-dev/pytest/issues/2231
3638
.. _#2234: https://github.com/pytest-dev/pytest/issues/2234
39+
.. _#2238: https://github.com/pytest-dev/pytest/issues/2238
3740

3841
.. _PEP-479: https://www.python.org/dev/peps/pep-0479/
3942

_pytest/config.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ def __init__(self, pluginmanager):
877877
self.trace = self.pluginmanager.trace.root.get("config")
878878
self.hook = self.pluginmanager.hook
879879
self._inicache = {}
880+
self._override_ini = ()
880881
self._opt2dest = {}
881882
self._cleanup = []
882883
self._warn = self.pluginmanager._warn
@@ -977,6 +978,7 @@ def _initini(self, args):
977978
self.invocation_dir = py.path.local()
978979
self._parser.addini('addopts', 'extra command line options', 'args')
979980
self._parser.addini('minversion', 'minimally required pytest version')
981+
self._override_ini = ns.override_ini or ()
980982

981983
def _consider_importhook(self, args, entrypoint_name):
982984
"""Install the PEP 302 import hook if using assertion re-writing.
@@ -1159,15 +1161,14 @@ def _get_override_ini_value(self, name):
11591161
# and -o foo1=bar1 -o foo2=bar2 options
11601162
# always use the last item if multiple value set for same ini-name,
11611163
# e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2
1162-
if self.getoption("override_ini", None):
1163-
for ini_config_list in self.option.override_ini:
1164-
for ini_config in ini_config_list:
1165-
try:
1166-
(key, user_ini_value) = ini_config.split("=", 1)
1167-
except ValueError:
1168-
raise UsageError("-o/--override-ini expects option=value style.")
1169-
if key == name:
1170-
value = user_ini_value
1164+
for ini_config_list in self._override_ini:
1165+
for ini_config in ini_config_list:
1166+
try:
1167+
(key, user_ini_value) = ini_config.split("=", 1)
1168+
except ValueError:
1169+
raise UsageError("-o/--override-ini expects option=value style.")
1170+
if key == name:
1171+
value = user_ini_value
11711172
return value
11721173

11731174
def getoption(self, name, default=notset, skip=False):

testing/test_config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,21 @@ def test_override_ini_usage_error_bad_style(self, testdir):
778778
result = testdir.runpytest("--override-ini", 'xdist_strict True', "-s")
779779
result.stderr.fnmatch_lines(["*ERROR* *expects option=value*"])
780780

781+
@pytest.mark.parametrize('with_ini', [True, False])
782+
def test_override_ini_handled_asap(self, testdir, with_ini):
783+
"""-o should be handled as soon as possible and always override what's in ini files (#2238)"""
784+
if with_ini:
785+
testdir.makeini("""
786+
[pytest]
787+
python_files=test_*.py
788+
""")
789+
testdir.makepyfile(unittest_ini_handle="""
790+
def test():
791+
pass
792+
""")
793+
result = testdir.runpytest("--override-ini", 'python_files=unittest_*.py')
794+
result.stdout.fnmatch_lines(["*1 passed in*"])
795+
781796
def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch):
782797
monkeypatch.chdir(str(tmpdir))
783798
a = tmpdir.mkdir("a")

0 commit comments

Comments
 (0)