From bfda2a0050943f0ead1fec3b18a52d32c07f34fa Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 28 Mar 2019 00:27:23 +0100 Subject: [PATCH] setup.cfg: use existing [tool:pytest] (ignoring [pytest]) --- changelog/5008.feature.rst | 3 +++ src/_pytest/config/findpaths.py | 12 ++++++------ testing/test_config.py | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 changelog/5008.feature.rst diff --git a/changelog/5008.feature.rst b/changelog/5008.feature.rst new file mode 100644 index 000000000..17d2770fe --- /dev/null +++ b/changelog/5008.feature.rst @@ -0,0 +1,3 @@ +If a ``setup.cfg`` file contains ``[tool:pytest]`` and also the no longer supported ``[pytest]`` section, pytest will use ``[tool:pytest]`` ignoring ``[pytest]``. Previously it would unconditionally error out. + +This makes it simpler for plugins to support old pytest versions. diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index a0f16134d..fa2024470 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -33,7 +33,12 @@ def getcfg(args, config=None): p = base.join(inibasename) if exists(p): iniconfig = py.iniconfig.IniConfig(p) - if "pytest" in iniconfig.sections: + if ( + inibasename == "setup.cfg" + and "tool:pytest" in iniconfig.sections + ): + return base, p, iniconfig["tool:pytest"] + elif "pytest" in iniconfig.sections: if inibasename == "setup.cfg" and config is not None: fail( @@ -41,11 +46,6 @@ def getcfg(args, config=None): pytrace=False, ) return base, p, iniconfig["pytest"] - if ( - inibasename == "setup.cfg" - and "tool:pytest" in iniconfig.sections - ): - return base, p, iniconfig["tool:pytest"] elif inibasename == "pytest.ini": # allowed to be empty return base, p, {} diff --git a/testing/test_config.py b/testing/test_config.py index 07654e5ad..28cc3ab91 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -46,6 +46,22 @@ class TestParseIni(object): """correctly handle zero length arguments (a la pytest '')""" getcfg([""]) + def test_setupcfg_uses_toolpytest_with_pytest(self, testdir): + p1 = testdir.makepyfile("def test(): pass") + testdir.makefile( + ".cfg", + setup=""" + [tool:pytest] + testpaths=%s + [pytest] + testpaths=ignored + """ + % p1.basename, + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*, inifile: setup.cfg, *", "* 1 passed in *"]) + assert result.ret == 0 + def test_append_parse_args(self, testdir, tmpdir, monkeypatch): monkeypatch.setenv("PYTEST_ADDOPTS", '--color no -rs --tb="short"') tmpdir.join("pytest.ini").write(