From 17514c15dbb3176a835511275f7f33f171cf848d Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 20 Feb 2021 22:15:10 +0100 Subject: [PATCH] fix issue #3523 - deprecate using setup.cfg --- src/_pytest/config/findpaths.py | 7 +++++++ src/_pytest/deprecated.py | 6 ++++++ testing/test_config.py | 27 +++++++++++++++++++++++---- testing/test_mark.py | 1 + 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index f8eae04c1..7c6a6c420 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -1,5 +1,6 @@ import os import sys +import warnings from pathlib import Path from typing import Dict from typing import Iterable @@ -11,6 +12,7 @@ from typing import TYPE_CHECKING from typing import Union from .exceptions import UsageError +from _pytest.deprecated import SETUP_CFG_CONFIG from _pytest.outcomes import fail from _pytest.pathlib import absolutepath from _pytest.pathlib import commonpath @@ -70,6 +72,11 @@ def _parse_cfg_file(path: Path) -> PARSE_RESULT: iniconfig = _parse_ini_config(path) if "tool:pytest" in iniconfig.sections: + + if path.name == "setup.cfg": + warnings.warn_explicit( + SETUP_CFG_CONFIG, None, os.fspath(path), 0, module="pytest" + ) return dict(iniconfig["tool:pytest"].items()) elif "pytest" in iniconfig.sections: # If a setup.cfg contains a "[pytest]" section, we raise a failure to indicate users that diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index f2d79760a..4283d577f 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -66,6 +66,12 @@ ARGUMENT_TYPE_STR = UnformattedWarning( " (options: {names})", ) +SETUP_CFG_CONFIG = PytestDeprecationWarning( + "configuring pytest in setup.cfg has been deprecated \n" + "as pytest and setuptools do not share he same config parser\n" + "please consider pytest.ini/tox.ini or pyproject.toml" +) + HOOK_LEGACY_PATH_ARG = UnformattedWarning( PytestRemovedIn8Warning, diff --git a/testing/test_config.py b/testing/test_config.py index c2e3fe5bb..deac73d65 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -31,9 +31,18 @@ from _pytest.pathlib import absolutepath from _pytest.pytester import Pytester +setup_cfg_nowarn = pytest.mark.filterwarnings( + "ignore:.*setup.cfg.*:pytest.PytestDeprecationWarning" +) + + class TestParseIni: @pytest.mark.parametrize( - "section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")] + "section, filename", + [ + ("pytest", "pytest.ini"), + pytest.param("tool:pytest", "setup.cfg", marks=setup_cfg_nowarn), + ], ) def test_getcfg_and_config( self, @@ -62,6 +71,7 @@ class TestParseIni: config = pytester.parseconfigure(str(sub)) assert config.inicfg["name"] == "value" + @setup_cfg_nowarn def test_setupcfg_uses_toolpytest_with_pytest(self, pytester: Pytester) -> None: p1 = pytester.makepyfile("def test(): pass") pytester.makefile( @@ -113,7 +123,7 @@ class TestParseIni: @pytest.mark.parametrize( "section, name", [ - ("tool:pytest", "setup.cfg"), + pytest.param("tool:pytest", "setup.cfg", marks=setup_cfg_nowarn), ("pytest", "tox.ini"), ("pytest", "pytest.ini"), ("pytest", ".pytest.ini"), @@ -1355,7 +1365,12 @@ class TestRootdir: "pyproject.toml", "[tool.pytest.ini_options]\nx=10", id="pyproject.toml" ), pytest.param("tox.ini", "[pytest]\nx=10", id="tox.ini"), - pytest.param("setup.cfg", "[tool:pytest]\nx=10", id="setup.cfg"), + pytest.param( + "setup.cfg", + "[tool:pytest]\nx=10", + id="setup.cfg", + marks=setup_cfg_nowarn, + ), ], ) def test_with_ini(self, tmp_path: Path, name: str, contents: str) -> None: @@ -1488,6 +1503,7 @@ class TestRootdir: assert rootpath == tmp_path assert inipath is None + @setup_cfg_nowarn def test_with_config_also_in_parent_directory( self, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: @@ -1505,7 +1521,10 @@ class TestRootdir: class TestOverrideIniArgs: - @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) + @pytest.mark.parametrize( + "name", + [pytest.param("setup.cfg", marks=setup_cfg_nowarn), "tox.ini", "pytest.ini"], + ) def test_override_ini_names(self, pytester: Pytester, name: str) -> None: section = "[pytest]" if name != "setup.cfg" else "[tool:pytest]" pytester.path.joinpath(name).write_text( diff --git a/testing/test_mark.py b/testing/test_mark.py index 65f2581bd..3b79d357e 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -122,6 +122,7 @@ def test_ini_markers_whitespace(pytester: Pytester) -> None: rec.assertoutcome(passed=1) +@pytest.mark.filterwarnings("ignore:.*setup.cfg.*:pytest.PytestDeprecationWarning") def test_marker_without_description(pytester: Pytester) -> None: pytester.makefile( ".cfg",