@@ -224,30 +224,38 @@ def run(self, config: Config, monkeypatch) -> None:
224224 response = w .files .download ("/test" ).contents
225225 response .read ()
226226 elif self .download_mode == DownloadMode .FILE : # FILE mode
227- with NamedTemporaryFile (delete = True ) as temp_file :
227+ with NamedTemporaryFile (delete = False ) as temp_file :
228+ file_path = temp_file .name
229+
230+ # We can't use 'with' because Windows doesn't allow reopening the file, and `download_to` can open it
231+ # only after we close it here.
232+ try :
228233 if self .expected_exception is None :
229234 w .files .download_to (
230235 "/test" ,
231- temp_file . name ,
236+ file_path ,
232237 overwrite = self .overwrite ,
233238 use_parallel = self .use_parallel ,
234239 parallelism = self .parallelism ,
235240 )
236241
237242 # Verify the downloaded file content
238- with open (temp_file . name , "rb" ) as f :
243+ with open (file_path , "rb" ) as f :
239244 actual_content = f .read ()
240245 assert len (actual_content ) == len (session .content )
241246 assert actual_content == session .content
242247 else :
243248 with pytest .raises (self .expected_exception ):
244249 w .files .download_to (
245250 "/test" ,
246- temp_file . name ,
251+ file_path ,
247252 overwrite = self .overwrite ,
248253 use_parallel = self .use_parallel ,
249254 parallelism = self .parallelism ,
250255 )
256+ finally :
257+ if os .path .exists (file_path ):
258+ os .remove (file_path )
251259
252260 received_requests = session .received_requests
253261
@@ -972,30 +980,38 @@ def custom_matcher(request: requests.Request) -> Optional[requests.Response]:
972980 if self .expected_download_api is not None :
973981 assert state .api_used == self .expected_download_api
974982 elif download_mode == DownloadMode .FILE :
975- with NamedTemporaryFile (delete = True ) as temp_file :
983+ with NamedTemporaryFile (delete = False ) as temp_file :
984+ file_path = temp_file .name
985+
986+ # We can't use 'with' because Windows doesn't allow reopening the file, and `download_to` can open it
987+ # only after we close it here.
988+ try :
976989 if self .expected_exception_type is not None :
977990 with pytest .raises (self .expected_exception_type ):
978991 w .files .download_to (
979992 PresignedUrlDownloadTestCase ._FILE_PATH ,
980- temp_file . name ,
993+ file_path ,
981994 overwrite = self .overwrite ,
982995 use_parallel = use_parallel ,
983996 parallelism = self .parallelism ,
984997 )
985998 else :
986999 w .files .download_to (
9871000 PresignedUrlDownloadTestCase ._FILE_PATH ,
988- temp_file . name ,
1001+ file_path ,
9891002 overwrite = self .overwrite ,
9901003 use_parallel = use_parallel ,
9911004 parallelism = self .parallelism ,
9921005 )
993- with open (temp_file . name , "rb" ) as f :
1006+ with open (file_path , "rb" ) as f :
9941007 actual_content = f .read ()
9951008 assert len (actual_content ) == len (state .content )
9961009 assert actual_content == state .content
9971010 if self .expected_download_api is not None :
9981011 assert state .api_used == self .expected_download_api
1012+ finally :
1013+ if os .path .exists (file_path ):
1014+ os .remove (file_path )
9991015 else :
10001016 raise ValueError ("Unexpected download mode" )
10011017
0 commit comments