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