Skip to content

Commit 16227e0

Browse files
committed
Revert "legacypath: only add testdir and tmpdir fixtures if corresponding plugins are registered"
This reverts commit e6eac28. This approach doesn't work when pytester is loaded thru a conftest `pytest_plugins`. Fixes pytest-dev#9280.
1 parent f87df9c commit 16227e0

File tree

2 files changed

+41
-51
lines changed

2 files changed

+41
-51
lines changed

doc/en/reference/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ tmpdir
638638

639639
:ref:`tmpdir and tmpdir_factory`
640640

641-
.. autofunction:: _pytest.legacypath.LegacyTmpdirPlugin.tmpdir()
641+
.. autofunction:: _pytest.legacypath.tmpdir()
642642
:no-auto-options:
643643

644644

src/_pytest/legacypath.py

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,15 @@ def __str__(self) -> str:
248248
pytest.Testdir = Testdir # type: ignore[attr-defined]
249249

250250

251-
class LegacyTestdirPlugin:
252-
@staticmethod
253-
@pytest.fixture
254-
def testdir(pytester: pytest.Pytester) -> Testdir:
255-
"""
256-
Identical to :fixture:`pytester`, and provides an instance whose methods return
257-
legacy ``LEGACY_PATH`` objects instead when applicable.
251+
@pytest.fixture
252+
def testdir(pytester: pytest.Pytester) -> Testdir:
253+
"""
254+
Identical to :fixture:`pytester`, and provides an instance whose methods return
255+
legacy ``LEGACY_PATH`` objects instead when applicable.
258256
259-
New code should avoid using :fixture:`testdir` in favor of :fixture:`pytester`.
260-
"""
261-
return Testdir(pytester, _ispytest=True)
257+
New code should avoid using :fixture:`testdir` in favor of :fixture:`pytester`.
258+
"""
259+
return Testdir(pytester, _ispytest=True)
262260

263261

264262
@final
@@ -287,31 +285,29 @@ def getbasetemp(self) -> LEGACY_PATH:
287285
pytest.TempdirFactory = TempdirFactory # type: ignore[attr-defined]
288286

289287

290-
class LegacyTmpdirPlugin:
291-
@staticmethod
292-
@pytest.fixture(scope="session")
293-
def tmpdir_factory(request: pytest.FixtureRequest) -> TempdirFactory:
294-
"""Return a :class:`pytest.TempdirFactory` instance for the test session."""
295-
# Set dynamically by pytest_configure().
296-
return request.config._tmpdirhandler # type: ignore
288+
@pytest.fixture(scope="session")
289+
def tmpdir_factory(request: pytest.FixtureRequest) -> TempdirFactory:
290+
"""Return a :class:`pytest.TempdirFactory` instance for the test session."""
291+
# Set dynamically by pytest_configure().
292+
return request.config._tmpdirhandler # type: ignore
297293

298-
@staticmethod
299-
@pytest.fixture
300-
def tmpdir(tmp_path: Path) -> LEGACY_PATH:
301-
"""Return a temporary directory path object which is unique to each test
302-
function invocation, created as a sub directory of the base temporary
303-
directory.
304294

305-
By default, a new base temporary directory is created each test session,
306-
and old bases are removed after 3 sessions, to aid in debugging. If
307-
``--basetemp`` is used then it is cleared each session. See :ref:`base
308-
temporary directory`.
295+
@pytest.fixture
296+
def tmpdir(tmp_path: Path) -> LEGACY_PATH:
297+
"""Return a temporary directory path object which is unique to each test
298+
function invocation, created as a sub directory of the base temporary
299+
directory.
309300
310-
The returned object is a `legacy_path`_ object.
301+
By default, a new base temporary directory is created each test session,
302+
and old bases are removed after 3 sessions, to aid in debugging. If
303+
``--basetemp`` is used then it is cleared each session. See :ref:`base
304+
temporary directory`.
311305
312-
.. _legacy_path: https://py.readthedocs.io/en/latest/path.html
313-
"""
314-
return legacy_path(tmp_path)
306+
The returned object is a `legacy_path`_ object.
307+
308+
.. _legacy_path: https://py.readthedocs.io/en/latest/path.html
309+
"""
310+
return legacy_path(tmp_path)
315311

316312

317313
def Cache_makedir(self: pytest.Cache, name: str) -> LEGACY_PATH:
@@ -404,25 +400,19 @@ def pytest_configure(config: pytest.Config) -> None:
404400
mp = pytest.MonkeyPatch()
405401
config.add_cleanup(mp.undo)
406402

407-
if config.pluginmanager.has_plugin("pytester"):
408-
config.pluginmanager.register(LegacyTestdirPlugin, "legacypath-pytester")
409-
410-
if config.pluginmanager.has_plugin("tmpdir"):
411-
# Create TmpdirFactory and attach it to the config object.
412-
#
413-
# This is to comply with existing plugins which expect the handler to be
414-
# available at pytest_configure time, but ideally should be moved entirely
415-
# to the tmpdir_factory session fixture.
416-
try:
417-
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
418-
except AttributeError:
419-
# tmpdir plugin is blocked.
420-
pass
421-
else:
422-
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
423-
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
424-
425-
config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")
403+
# Create TmpdirFactory and attach it to the config object.
404+
#
405+
# This is to comply with existing plugins which expect the handler to be
406+
# available at pytest_configure time, but ideally should be moved entirely
407+
# to the tmpdir_factory session fixture.
408+
try:
409+
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
410+
except AttributeError:
411+
# tmpdir plugin is blocked.
412+
pass
413+
else:
414+
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
415+
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
426416

427417
# Add Cache.makedir().
428418
mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)

0 commit comments

Comments
 (0)