|
14 | 14 | from .pathlib import make_numbered_dir_with_cleanup
|
15 | 15 | from .pathlib import Path
|
16 | 16 | from _pytest.fixtures import FixtureRequest
|
| 17 | +from _pytest.monkeypatch import MonkeyPatch |
17 | 18 |
|
18 | 19 |
|
19 | 20 | @attr.s
|
@@ -134,18 +135,35 @@ def get_user() -> Optional[str]:
|
134 | 135 | return None
|
135 | 136 |
|
136 | 137 |
|
| 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 | + |
137 | 153 | @pytest.fixture(scope="session")
|
138 |
| -def tmpdir_factory(tmp_path_factory) -> TempdirFactory: |
| 154 | +def tmpdir_factory(request: FixtureRequest) -> TempdirFactory: |
139 | 155 | """Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
|
140 | 156 | """
|
141 |
| - return TempdirFactory(tmp_path_factory) |
| 157 | + # Set dynamically by pytest_configure() above. |
| 158 | + return request.config._tmpdirhandler # type: ignore |
142 | 159 |
|
143 | 160 |
|
144 | 161 | @pytest.fixture(scope="session")
|
145 | 162 | def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
|
146 | 163 | """Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
|
147 | 164 | """
|
148 |
| - return TempPathFactory.from_config(request.config) |
| 165 | + # Set dynamically by pytest_configure() above. |
| 166 | + return request.config._tmp_path_factory # type: ignore |
149 | 167 |
|
150 | 168 |
|
151 | 169 | def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
|
|
0 commit comments