Move FixtureRequest.fspath to legacypath plugin

This commit is contained in:
Ran Benita 2021-10-16 10:45:05 +03:00
parent a1a605a63e
commit 7c0011374c
4 changed files with 33 additions and 11 deletions

View File

@ -46,8 +46,6 @@ from _pytest.compat import getfuncargnames
from _pytest.compat import getimfunc
from _pytest.compat import getlocation
from _pytest.compat import is_generator
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.compat import NOTSET
from _pytest.compat import safe_getattr
from _pytest.config import _PluggyPlugin
@ -528,11 +526,6 @@ class FixtureRequest:
raise AttributeError(f"module not available in {self.scope}-scoped context")
return self._pyfuncitem.getparent(_pytest.python.Module).obj
@property
def fspath(self) -> LEGACY_PATH:
"""(deprecated) The file system path of the test module which collected this test."""
return legacy_path(self.path)
@property
def path(self) -> Path:
if self.scope not in ("function", "class", "module", "package"):

View File

@ -315,6 +315,11 @@ def Cache_makedir(self: pytest.Cache, name: str) -> LEGACY_PATH:
return legacy_path(self.mkdir(name))
def FixtureRequest_fspath(self: pytest.FixtureRequest) -> LEGACY_PATH:
"""(deprecated) The file system path of the test module which collected this test."""
return legacy_path(self.path)
def pytest_configure(config: pytest.Config) -> None:
mp = pytest.MonkeyPatch()
config.add_cleanup(mp.undo)
@ -335,3 +340,8 @@ def pytest_configure(config: pytest.Config) -> None:
# Add Cache.makedir().
mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)
# Add FixtureRequest.fspath property.
mp.setattr(
pytest.FixtureRequest, "fspath", property(FixtureRequest_fspath), raising=False
)

View File

@ -967,7 +967,6 @@ class TestRequestBasic:
(item,) = pytester.genitems([modcol])
req = fixtures.FixtureRequest(item, _ispytest=True)
assert req.path == modcol.path
assert req.fspath == modcol.fspath
def test_request_fixturenames(self, pytester: Pytester) -> None:
pytester.makepyfile(
@ -1098,12 +1097,11 @@ class TestRequestSessionScoped:
def session_request(self, request):
return request
@pytest.mark.parametrize("name", ["path", "fspath", "module"])
@pytest.mark.parametrize("name", ["path", "module"])
def test_session_scoped_unavailable_attributes(self, session_request, name):
expected = "path" if name == "fspath" else name
with pytest.raises(
AttributeError,
match=f"{expected} not available in session-scoped context",
match=f"{name} not available in session-scoped context",
):
getattr(session_request, name)

View File

@ -73,3 +73,24 @@ def test_cache_makedir(cache: pytest.Cache) -> None:
dir = cache.makedir("foo") # type: ignore[attr-defined]
assert dir.exists()
dir.remove()
def test_fixturerequest_getmodulepath(pytester: pytest.Pytester) -> None:
modcol = pytester.getmodulecol("def test_somefunc(): pass")
(item,) = pytester.genitems([modcol])
req = pytest.FixtureRequest(item, _ispytest=True)
assert req.path == modcol.path
assert req.fspath == modcol.fspath # type: ignore[attr-defined]
class TestFixtureRequestSessionScoped:
@pytest.fixture(scope="session")
def session_request(self, request):
return request
def test_session_scoped_unavailable_attributes(self, session_request):
with pytest.raises(
AttributeError,
match="path not available in session-scoped context",
):
session_request.fspath