Skip to content

Commit 16e2737

Browse files
implement tmp_path_factory and deprecate pytest.ensuretemp as intended
1 parent 36c2a10 commit 16e2737

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/_pytest/deprecated.py

+5
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@
109109
PYTEST_NAMESPACE = RemovedInPytest4Warning(
110110
"pytest_namespace is deprecated and will be removed soon"
111111
)
112+
113+
PYTEST_ENSURETEMP = RemovedInPytest4Warning(
114+
"pytest/tmpdir_factory.ensuretemp is deprecated, \n"
115+
"please use the tmp_path fixture or tmp_path_factory.mktemp"
116+
)

src/_pytest/tmpdir.py

+26-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from _pytest.monkeypatch import MonkeyPatch
99
import attr
1010
import tempfile
11+
import warnings
12+
1113
from .pathlib import (
1214
Path,
1315
make_numbered_dir,
@@ -88,6 +90,9 @@ def ensuretemp(self, string, dir=1):
8890
and is guaranteed to be empty.
8991
"""
9092
# py.log._apiwarn(">1.1", "use tmpdir function argument")
93+
from .deprecated import PYTEST_ENSURETEMP
94+
95+
warnings.warn(PYTEST_ENSURETEMP, stacklevel=2)
9196
return self.getbasetemp().ensure(string, dir=dir)
9297

9398
def mktemp(self, basename, numbered=True):
@@ -101,9 +106,6 @@ def getbasetemp(self):
101106
"""backward compat wrapper for ``_tmppath_factory.getbasetemp``"""
102107
return py.path.local(self._tmppath_factory.getbasetemp().resolve())
103108

104-
def finish(self):
105-
self._tmppath_factory.trace("finish")
106-
107109

108110
def get_user():
109111
"""Return the current user name, or None if getuser() does not work
@@ -127,18 +129,34 @@ def pytest_configure(config):
127129
mp = MonkeyPatch()
128130
tmppath_handler = TempPathFactory.from_config(config)
129131
t = TempdirFactory(tmppath_handler)
130-
config._cleanup.extend([mp.undo, t.finish])
132+
config._cleanup.append(mp.undo)
133+
mp.setattr(config, "_tmp_path_factory", tmppath_handler, raising=False)
131134
mp.setattr(config, "_tmpdirhandler", t, raising=False)
132135
mp.setattr(pytest, "ensuretemp", t.ensuretemp, raising=False)
133136

134137

135138
@pytest.fixture(scope="session")
136139
def tmpdir_factory(request):
137-
"""Return a TempdirFactory instance for the test session.
140+
"""Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
138141
"""
139142
return request.config._tmpdirhandler
140143

141144

145+
@pytest.fixture(scope="session")
146+
def tmp_path_factory(request):
147+
"""Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
148+
"""
149+
return request.config._tmp_path_factory
150+
151+
152+
def _mk_tmp(request, factory):
153+
name = request.node.name
154+
name = re.sub(r"[\W]", "_", name)
155+
MAXVAL = 30
156+
name = name[:MAXVAL]
157+
return factory.mktemp(name, numbered=True)
158+
159+
142160
@pytest.fixture
143161
def tmpdir(request, tmpdir_factory):
144162
"""Return a temporary directory path object
@@ -149,17 +167,11 @@ def tmpdir(request, tmpdir_factory):
149167
150168
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
151169
"""
152-
name = request.node.name
153-
name = re.sub(r"[\W]", "_", name)
154-
MAXVAL = 30
155-
if len(name) > MAXVAL:
156-
name = name[:MAXVAL]
157-
x = tmpdir_factory.mktemp(name, numbered=True)
158-
return x
170+
return _mk_tmp(request, tmpdir_factory)
159171

160172

161173
@pytest.fixture
162-
def tmp_path(tmpdir):
174+
def tmp_path(request, tmp_path_factory):
163175
"""Return a temporary directory path object
164176
which is unique to each test function invocation,
165177
created as a sub directory of the base temporary
@@ -171,4 +183,4 @@ def tmp_path(tmpdir):
171183
in python < 3.6 this is a pathlib2.Path
172184
"""
173185

174-
return Path(tmpdir)
186+
return _mk_tmp(request, tmp_path_factory)

0 commit comments

Comments
 (0)