From 7c0011374c99a3738324db2c783d4e5d369cff23 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 16 Oct 2021 10:45:05 +0300 Subject: [PATCH] Move FixtureRequest.fspath to legacypath plugin --- src/_pytest/fixtures.py | 7 ------- src/_pytest/legacypath.py | 10 ++++++++++ testing/python/fixtures.py | 6 ++---- testing/test_legacypath.py | 21 +++++++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index bcb53552a..fddff931c 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -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"): diff --git a/src/_pytest/legacypath.py b/src/_pytest/legacypath.py index d53a0d315..08d668f46 100644 --- a/src/_pytest/legacypath.py +++ b/src/_pytest/legacypath.py @@ -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 + ) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index ea66f50f0..d8708b9ed 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -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) diff --git a/testing/test_legacypath.py b/testing/test_legacypath.py index 04cd61f0e..ed0435c1c 100644 --- a/testing/test_legacypath.py +++ b/testing/test_legacypath.py @@ -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