8
8
from _pytest .monkeypatch import MonkeyPatch
9
9
import attr
10
10
import tempfile
11
+ import warnings
12
+
11
13
from .pathlib import (
12
14
Path ,
13
15
make_numbered_dir ,
@@ -88,6 +90,9 @@ def ensuretemp(self, string, dir=1):
88
90
and is guaranteed to be empty.
89
91
"""
90
92
# py.log._apiwarn(">1.1", "use tmpdir function argument")
93
+ from .deprecated import PYTEST_ENSURETEMP
94
+
95
+ warnings .warn (PYTEST_ENSURETEMP , stacklevel = 2 )
91
96
return self .getbasetemp ().ensure (string , dir = dir )
92
97
93
98
def mktemp (self , basename , numbered = True ):
@@ -101,9 +106,6 @@ def getbasetemp(self):
101
106
"""backward compat wrapper for ``_tmppath_factory.getbasetemp``"""
102
107
return py .path .local (self ._tmppath_factory .getbasetemp ().resolve ())
103
108
104
- def finish (self ):
105
- self ._tmppath_factory .trace ("finish" )
106
-
107
109
108
110
def get_user ():
109
111
"""Return the current user name, or None if getuser() does not work
@@ -127,18 +129,34 @@ def pytest_configure(config):
127
129
mp = MonkeyPatch ()
128
130
tmppath_handler = TempPathFactory .from_config (config )
129
131
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 )
131
134
mp .setattr (config , "_tmpdirhandler" , t , raising = False )
132
135
mp .setattr (pytest , "ensuretemp" , t .ensuretemp , raising = False )
133
136
134
137
135
138
@pytest .fixture (scope = "session" )
136
139
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.
138
141
"""
139
142
return request .config ._tmpdirhandler
140
143
141
144
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
+
142
160
@pytest .fixture
143
161
def tmpdir (request , tmpdir_factory ):
144
162
"""Return a temporary directory path object
@@ -149,17 +167,11 @@ def tmpdir(request, tmpdir_factory):
149
167
150
168
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
151
169
"""
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 )
159
171
160
172
161
173
@pytest .fixture
162
- def tmp_path (tmpdir ):
174
+ def tmp_path (request , tmp_path_factory ):
163
175
"""Return a temporary directory path object
164
176
which is unique to each test function invocation,
165
177
created as a sub directory of the base temporary
@@ -171,4 +183,4 @@ def tmp_path(tmpdir):
171
183
in python < 3.6 this is a pathlib2.Path
172
184
"""
173
185
174
- return Path ( tmpdir )
186
+ return _mk_tmp ( request , tmp_path_factory )
0 commit comments