21
21
from .reports import CollectReport
22
22
from _pytest import nodes
23
23
from _pytest ._io import TerminalWriter
24
+ from _pytest .compat import assert_never
24
25
from _pytest .config import Config
25
26
from _pytest .config import ExitCode
26
27
from _pytest .config import hookimpl
@@ -123,6 +124,10 @@ def warn(self, fmt: str, *, _ispytest: bool = False, **args: object) -> None:
123
124
stacklevel = 3 ,
124
125
)
125
126
127
+ def _mkdir (self , path : Path ) -> None :
128
+ self ._ensure_cache_dir_and_supporting_files ()
129
+ path .mkdir (exist_ok = True , parents = True )
130
+
126
131
def mkdir (self , name : str ) -> Path :
127
132
"""Return a directory path object with the given name.
128
133
@@ -141,7 +146,7 @@ def mkdir(self, name: str) -> Path:
141
146
if len (path .parts ) > 1 :
142
147
raise ValueError ("name is not allowed to contain path separators" )
143
148
res = self ._cachedir .joinpath (self ._CACHE_PREFIX_DIRS , path )
144
- res . mkdir ( exist_ok = True , parents = True )
149
+ self . _mkdir ( res )
145
150
return res
146
151
147
152
def _getvaluepath (self , key : str ) -> Path :
@@ -178,20 +183,13 @@ def set(self, key: str, value: object) -> None:
178
183
"""
179
184
path = self ._getvaluepath (key )
180
185
try :
181
- if path .parent .is_dir ():
182
- cache_dir_exists_already = True
183
- else :
184
- cache_dir_exists_already = self ._cachedir .exists ()
185
- path .parent .mkdir (exist_ok = True , parents = True )
186
+ self ._mkdir (path .parent )
186
187
except OSError as exc :
187
188
self .warn (
188
189
f"could not create cache path { path } : { exc } " ,
189
190
_ispytest = True ,
190
191
)
191
192
return
192
- if not cache_dir_exists_already :
193
- self ._ensure_supporting_files ()
194
- data = json .dumps (value , ensure_ascii = False , indent = 2 )
195
193
try :
196
194
f = path .open ("w" , encoding = "UTF-8" )
197
195
except OSError as exc :
@@ -201,19 +199,27 @@ def set(self, key: str, value: object) -> None:
201
199
)
202
200
else :
203
201
with f :
204
- f .write (data )
205
-
206
- def _ensure_supporting_files (self ) -> None :
207
- """Create supporting files in the cache dir that are not really part of the cache."""
208
- readme_path = self ._cachedir / "README.md"
209
- readme_path .write_text (README_CONTENT , encoding = "UTF-8" )
202
+ json .dump (value , f , ensure_ascii = False , indent = 2 )
210
203
211
- gitignore_path = self . _cachedir . joinpath ( ".gitignore" )
212
- msg = "# Created by pytest automatically. \n * \n "
213
- gitignore_path . write_text ( msg , encoding = "UTF-8" )
204
+ def _ensure_cache_dir_and_supporting_files ( self ) -> None :
205
+ """Create the cache dir and its supporting files."" "
206
+ self . _cachedir . mkdir ( exist_ok = True , parents = True )
214
207
215
- cachedir_tag_path = self ._cachedir .joinpath ("CACHEDIR.TAG" )
216
- cachedir_tag_path .write_bytes (CACHEDIR_TAG_CONTENT )
208
+ files : Iterable [tuple [str , str | bytes ]] = (
209
+ ("README.md" , README_CONTENT ),
210
+ (".gitignore" , "# Created by pytest automatically.\n *\n " ),
211
+ ("CACHEDIR.TAG" , CACHEDIR_TAG_CONTENT ),
212
+ )
213
+ for file , content in files :
214
+ path = self ._cachedir .joinpath (file )
215
+ if path .exists ():
216
+ continue
217
+ if isinstance (content , str ):
218
+ path .write_text (content , encoding = "UTF-8" )
219
+ elif isinstance (content , bytes ):
220
+ path .write_bytes (content )
221
+ else :
222
+ assert_never (content )
217
223
218
224
219
225
class LFPluginCollWrapper :
0 commit comments