Move FixtureRequest.fspath to legacypath plugin
This commit is contained in:
		
							parent
							
								
									a1a605a63e
								
							
						
					
					
						commit
						7c0011374c
					
				| 
						 | 
					@ -46,8 +46,6 @@ from _pytest.compat import getfuncargnames
 | 
				
			||||||
from _pytest.compat import getimfunc
 | 
					from _pytest.compat import getimfunc
 | 
				
			||||||
from _pytest.compat import getlocation
 | 
					from _pytest.compat import getlocation
 | 
				
			||||||
from _pytest.compat import is_generator
 | 
					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 NOTSET
 | 
				
			||||||
from _pytest.compat import safe_getattr
 | 
					from _pytest.compat import safe_getattr
 | 
				
			||||||
from _pytest.config import _PluggyPlugin
 | 
					from _pytest.config import _PluggyPlugin
 | 
				
			||||||
| 
						 | 
					@ -528,11 +526,6 @@ class FixtureRequest:
 | 
				
			||||||
            raise AttributeError(f"module not available in {self.scope}-scoped context")
 | 
					            raise AttributeError(f"module not available in {self.scope}-scoped context")
 | 
				
			||||||
        return self._pyfuncitem.getparent(_pytest.python.Module).obj
 | 
					        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
 | 
					    @property
 | 
				
			||||||
    def path(self) -> Path:
 | 
					    def path(self) -> Path:
 | 
				
			||||||
        if self.scope not in ("function", "class", "module", "package"):
 | 
					        if self.scope not in ("function", "class", "module", "package"):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -315,6 +315,11 @@ def Cache_makedir(self: pytest.Cache, name: str) -> LEGACY_PATH:
 | 
				
			||||||
    return legacy_path(self.mkdir(name))
 | 
					    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:
 | 
					def pytest_configure(config: pytest.Config) -> None:
 | 
				
			||||||
    mp = pytest.MonkeyPatch()
 | 
					    mp = pytest.MonkeyPatch()
 | 
				
			||||||
    config.add_cleanup(mp.undo)
 | 
					    config.add_cleanup(mp.undo)
 | 
				
			||||||
| 
						 | 
					@ -335,3 +340,8 @@ def pytest_configure(config: pytest.Config) -> None:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Add Cache.makedir().
 | 
					    # Add Cache.makedir().
 | 
				
			||||||
    mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)
 | 
					    mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Add FixtureRequest.fspath property.
 | 
				
			||||||
 | 
					    mp.setattr(
 | 
				
			||||||
 | 
					        pytest.FixtureRequest, "fspath", property(FixtureRequest_fspath), raising=False
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -967,7 +967,6 @@ class TestRequestBasic:
 | 
				
			||||||
        (item,) = pytester.genitems([modcol])
 | 
					        (item,) = pytester.genitems([modcol])
 | 
				
			||||||
        req = fixtures.FixtureRequest(item, _ispytest=True)
 | 
					        req = fixtures.FixtureRequest(item, _ispytest=True)
 | 
				
			||||||
        assert req.path == modcol.path
 | 
					        assert req.path == modcol.path
 | 
				
			||||||
        assert req.fspath == modcol.fspath
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_request_fixturenames(self, pytester: Pytester) -> None:
 | 
					    def test_request_fixturenames(self, pytester: Pytester) -> None:
 | 
				
			||||||
        pytester.makepyfile(
 | 
					        pytester.makepyfile(
 | 
				
			||||||
| 
						 | 
					@ -1098,12 +1097,11 @@ class TestRequestSessionScoped:
 | 
				
			||||||
    def session_request(self, request):
 | 
					    def session_request(self, request):
 | 
				
			||||||
        return 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):
 | 
					    def test_session_scoped_unavailable_attributes(self, session_request, name):
 | 
				
			||||||
        expected = "path" if name == "fspath" else name
 | 
					 | 
				
			||||||
        with pytest.raises(
 | 
					        with pytest.raises(
 | 
				
			||||||
            AttributeError,
 | 
					            AttributeError,
 | 
				
			||||||
            match=f"{expected} not available in session-scoped context",
 | 
					            match=f"{name} not available in session-scoped context",
 | 
				
			||||||
        ):
 | 
					        ):
 | 
				
			||||||
            getattr(session_request, name)
 | 
					            getattr(session_request, name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,3 +73,24 @@ def test_cache_makedir(cache: pytest.Cache) -> None:
 | 
				
			||||||
    dir = cache.makedir("foo")  # type: ignore[attr-defined]
 | 
					    dir = cache.makedir("foo")  # type: ignore[attr-defined]
 | 
				
			||||||
    assert dir.exists()
 | 
					    assert dir.exists()
 | 
				
			||||||
    dir.remove()
 | 
					    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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue