[8.1.x] Revert legacy path removals (#12093)

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
github-actions[bot]
2024-03-08 23:24:31 +00:00
committed by GitHub
parent 86c3aab005
commit 15fbe57c44
15 changed files with 329 additions and 119 deletions

View File

@@ -1,5 +1,10 @@
# mypy: allow-untyped-defs
from pathlib import Path
import re
import sys
from _pytest import deprecated
from _pytest.compat import legacy_path
from _pytest.pytester import Pytester
import pytest
from pytest import PytestDeprecationWarning
@@ -85,6 +90,56 @@ def test_private_is_deprecated() -> None:
PrivateInit(10, _ispytest=True)
@pytest.mark.parametrize("hooktype", ["hook", "ihook"])
def test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request):
path = legacy_path(tmp_path)
PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(collection_path: pathlib\.Path.*"
if hooktype == "ihook":
hooks = request.node.ihook
else:
hooks = request.config.hook
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
l1 = sys._getframe().f_lineno
hooks.pytest_ignore_collect(
config=request.config, path=path, collection_path=tmp_path
)
l2 = sys._getframe().f_lineno
(record,) = r
assert record.filename == __file__
assert l1 < record.lineno < l2
hooks.pytest_ignore_collect(config=request.config, collection_path=tmp_path)
# Passing entirely *different* paths is an outright error.
with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"):
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
hooks.pytest_ignore_collect(
config=request.config, path=path, collection_path=Path("/bla/bla")
)
def test_node_ctor_fspath_argument_is_deprecated(pytester: Pytester) -> None:
mod = pytester.getmodulecol("")
class MyFile(pytest.File):
def collect(self):
raise NotImplementedError()
with pytest.warns(
pytest.PytestDeprecationWarning,
match=re.escape(
"The (fspath: py.path.local) argument to MyFile is deprecated."
),
):
MyFile.from_parent(
parent=mod.parent,
fspath=legacy_path("bla"),
)
def test_fixture_disallow_on_marked_functions():
"""Test that applying @pytest.fixture to a marked function warns (#3364)."""
with pytest.warns(

View File

@@ -1,8 +1,8 @@
# mypy: allow-untyped-defs
from pathlib import Path
from _pytest.compat import LEGACY_PATH
from _pytest.fixtures import TopRequest
from _pytest.legacypath import LEGACY_PATH
from _pytest.legacypath import TempdirFactory
from _pytest.legacypath import Testdir
import pytest
@@ -16,7 +16,7 @@ def test_item_fspath(pytester: pytest.Pytester) -> None:
items2, hookrec = pytester.inline_genitems(item.nodeid)
(item2,) = items2
assert item2.name == item.name
assert item2.fspath == item.fspath # type: ignore[attr-defined]
assert item2.fspath == item.fspath
assert item2.path == item.path

View File

@@ -6,6 +6,7 @@ from typing import Type
import warnings
from _pytest import nodes
from _pytest.compat import legacy_path
from _pytest.outcomes import OutcomeException
from _pytest.pytester import Pytester
from _pytest.warning_types import PytestWarning
@@ -44,9 +45,9 @@ def test_subclassing_both_item_and_collector_deprecated(
warnings.simplefilter("error")
class SoWrong(nodes.Item, nodes.File):
def __init__(self, path, parent):
def __init__(self, fspath, parent):
"""Legacy ctor with legacy call # don't wana see"""
super().__init__(parent, path)
super().__init__(fspath, parent)
def collect(self):
raise NotImplementedError()
@@ -55,7 +56,9 @@ def test_subclassing_both_item_and_collector_deprecated(
raise NotImplementedError()
with pytest.warns(PytestWarning) as rec:
SoWrong.from_parent(request.session, path=tmp_path / "broken.txt", wrong=10)
SoWrong.from_parent(
request.session, fspath=legacy_path(tmp_path / "broken.txt")
)
messages = [str(x.message) for x in rec]
assert any(
re.search(".*SoWrong.* not using a cooperative constructor.*", x)