Skip to content

Commit 9aed656

Browse files
Merge pull request #7066 from RonnyPfannschmidt/backport-6992-restore-tmpdir-indirection
backport #6992 to 5.4.x - restores tmpdir indirection
2 parents a600e7a + 40f02d7 commit 9aed656

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

changelog/6992.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert "tmpdir: clean up indirection via config for factories" #6767 as it breaks pytest-xdist.

src/_pytest/tmpdir.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .pathlib import make_numbered_dir_with_cleanup
1515
from .pathlib import Path
1616
from _pytest.fixtures import FixtureRequest
17+
from _pytest.monkeypatch import MonkeyPatch
1718

1819

1920
@attr.s
@@ -134,18 +135,35 @@ def get_user() -> Optional[str]:
134135
return None
135136

136137

138+
def pytest_configure(config) -> None:
139+
"""Create a TempdirFactory and attach it to the config object.
140+
141+
This is to comply with existing plugins which expect the handler to be
142+
available at pytest_configure time, but ideally should be moved entirely
143+
to the tmpdir_factory session fixture.
144+
"""
145+
mp = MonkeyPatch()
146+
tmppath_handler = TempPathFactory.from_config(config)
147+
t = TempdirFactory(tmppath_handler)
148+
config._cleanup.append(mp.undo)
149+
mp.setattr(config, "_tmp_path_factory", tmppath_handler, raising=False)
150+
mp.setattr(config, "_tmpdirhandler", t, raising=False)
151+
152+
137153
@pytest.fixture(scope="session")
138-
def tmpdir_factory(tmp_path_factory) -> TempdirFactory:
154+
def tmpdir_factory(request: FixtureRequest) -> TempdirFactory:
139155
"""Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
140156
"""
141-
return TempdirFactory(tmp_path_factory)
157+
# Set dynamically by pytest_configure() above.
158+
return request.config._tmpdirhandler # type: ignore
142159

143160

144161
@pytest.fixture(scope="session")
145162
def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
146163
"""Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
147164
"""
148-
return TempPathFactory.from_config(request.config)
165+
# Set dynamically by pytest_configure() above.
166+
return request.config._tmp_path_factory # type: ignore
149167

150168

151169
def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:

0 commit comments

Comments
 (0)