legacypath: only add `testdir` and `tmpdir` fixtures if corresponding plugins are registered

This preserves the existing behavior and gives a proper error message
in case e.g. `testdir` is requested without the `pytester` plugin being
loaded.
This commit is contained in:
Ran Benita 2021-10-29 13:11:12 +03:00
parent c3dff755af
commit e6eac28f0e
2 changed files with 51 additions and 41 deletions

View File

@ -638,7 +638,7 @@ tmpdir
:ref:`tmpdir and tmpdir_factory` :ref:`tmpdir and tmpdir_factory`
.. autofunction:: _pytest.legacypath.tmpdir() .. autofunction:: _pytest.legacypath.LegacyTmpdirPlugin.tmpdir()
:no-auto-options: :no-auto-options:

View File

@ -248,15 +248,17 @@ class Testdir:
pytest.Testdir = Testdir # type: ignore[attr-defined] pytest.Testdir = Testdir # type: ignore[attr-defined]
@pytest.fixture class LegacyTestdirPlugin:
def testdir(pytester: pytest.Pytester) -> Testdir: @staticmethod
""" @pytest.fixture
Identical to :fixture:`pytester`, and provides an instance whose methods return def testdir(pytester: pytest.Pytester) -> Testdir:
legacy ``LEGACY_PATH`` objects instead when applicable. """
Identical to :fixture:`pytester`, and provides an instance whose methods return
legacy ``LEGACY_PATH`` objects instead when applicable.
New code should avoid using :fixture:`testdir` in favor of :fixture:`pytester`. New code should avoid using :fixture:`testdir` in favor of :fixture:`pytester`.
""" """
return Testdir(pytester, _ispytest=True) return Testdir(pytester, _ispytest=True)
@final @final
@ -285,29 +287,31 @@ class TempdirFactory:
pytest.TempdirFactory = TempdirFactory # type: ignore[attr-defined] pytest.TempdirFactory = TempdirFactory # type: ignore[attr-defined]
@pytest.fixture(scope="session") class LegacyTmpdirPlugin:
def tmpdir_factory(request: pytest.FixtureRequest) -> TempdirFactory: @staticmethod
"""Return a :class:`pytest.TempdirFactory` instance for the test session.""" @pytest.fixture(scope="session")
# Set dynamically by pytest_configure(). def tmpdir_factory(request: pytest.FixtureRequest) -> TempdirFactory:
return request.config._tmpdirhandler # type: ignore """Return a :class:`pytest.TempdirFactory` instance for the test session."""
# Set dynamically by pytest_configure().
return request.config._tmpdirhandler # type: ignore
@staticmethod
@pytest.fixture
def tmpdir(tmp_path: Path) -> LEGACY_PATH:
"""Return a temporary directory path object which is unique to each test
function invocation, created as a sub directory of the base temporary
directory.
@pytest.fixture By default, a new base temporary directory is created each test session,
def tmpdir(tmp_path: Path) -> LEGACY_PATH: and old bases are removed after 3 sessions, to aid in debugging. If
"""Return a temporary directory path object which is unique to each test ``--basetemp`` is used then it is cleared each session. See :ref:`base
function invocation, created as a sub directory of the base temporary temporary directory`.
directory.
By default, a new base temporary directory is created each test session, The returned object is a `legacy_path`_ object.
and old bases are removed after 3 sessions, to aid in debugging. If
``--basetemp`` is used then it is cleared each session. See :ref:`base
temporary directory`.
The returned object is a `legacy_path`_ object. .. _legacy_path: https://py.readthedocs.io/en/latest/path.html
"""
.. _legacy_path: https://py.readthedocs.io/en/latest/path.html return legacy_path(tmp_path)
"""
return legacy_path(tmp_path)
def Cache_makedir(self: pytest.Cache, name: str) -> LEGACY_PATH: def Cache_makedir(self: pytest.Cache, name: str) -> LEGACY_PATH:
@ -400,19 +404,25 @@ def pytest_configure(config: pytest.Config) -> None:
mp = pytest.MonkeyPatch() mp = pytest.MonkeyPatch()
config.add_cleanup(mp.undo) config.add_cleanup(mp.undo)
# Create TmpdirFactory and attach it to the config object. if config.pluginmanager.has_plugin("pytester"):
# config.pluginmanager.register(LegacyTestdirPlugin, "legacypath-pytester")
# This is to comply with existing plugins which expect the handler to be
# available at pytest_configure time, but ideally should be moved entirely if config.pluginmanager.has_plugin("tmpdir"):
# to the tmpdir_factory session fixture. # Create TmpdirFactory and attach it to the config object.
try: #
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined] # This is to comply with existing plugins which expect the handler to be
except AttributeError: # available at pytest_configure time, but ideally should be moved entirely
# tmpdir plugin is blocked. # to the tmpdir_factory session fixture.
pass try:
else: tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True) except AttributeError:
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False) # tmpdir plugin is blocked.
pass
else:
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")
# Add Cache.makedir(). # Add Cache.makedir().
mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False) mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)