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

This reverts commit e6eac28f0e.

This approach doesn't work when pytester is loaded thru a conftest
`pytest_plugins`.

Fixes #9280.
This commit is contained in:
Ran Benita 2021-11-08 20:37:27 +02:00
parent f87df9c52e
commit 16227e058b
2 changed files with 41 additions and 51 deletions

View File

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

View File

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