Skip to content

Commit

Permalink
fix issue pytest-dev#3523 - deprecate using setup.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Jul 12, 2022
1 parent a2bad99 commit dfaef3a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import warnings
from pathlib import Path
from typing import Dict
from typing import Iterable
Expand All @@ -11,6 +12,7 @@
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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
" (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,
Expand Down
27 changes: 23 additions & 4 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@
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,
Expand Down Expand Up @@ -62,6 +71,7 @@ def test_getcfg_and_config(
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(
Expand Down Expand Up @@ -113,7 +123,7 @@ def test_tox_ini_wrong_version(self, pytester: Pytester) -> None:
@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"),
Expand Down Expand Up @@ -1355,7 +1365,12 @@ def test_simple_noini(self, tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
"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:
Expand Down Expand Up @@ -1488,6 +1503,7 @@ def test_with_existing_file_in_subdir(
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:
Expand All @@ -1505,7 +1521,10 @@ def test_with_config_also_in_parent_directory(


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(
Expand Down
1 change: 1 addition & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def test_markers():
rec.assertoutcome(passed=1)


@pytest.mark.filterwarnings("ignore:.*setup.cfg.*:pytest.PytestDeprecationWarning")
def test_marker_without_description(pytester: Pytester) -> None:
pytester.makefile(
".cfg",
Expand Down

0 comments on commit dfaef3a

Please sign in to comment.