1- import gc
2- import os
3- import platform
4- import shutil
5- import sqlite3
61import tempfile
7- import time
82from typing import Generator
93
104import pytest
@@ -20,47 +14,5 @@ def runner() -> CliRunner:
2014@pytest .fixture
2115def temp_dir () -> Generator [str , None , None ]:
2216 """Provide a temporary directory for test files."""
23- tmp_dir = tempfile .mkdtemp ()
24- try :
17+ with tempfile .TemporaryDirectory () as tmp_dir :
2518 yield tmp_dir
26- finally :
27- # Force garbage collection to close any open file handles
28- gc .collect ()
29-
30- # On Windows, SQLite files may remain locked even after closing connections
31- # Try to close any remaining SQLite connections to the state.db file
32- if platform .system () == "Windows" :
33- state_db_path = os .path .join (tmp_dir , "__uipath" , "state.db" )
34- if os .path .exists (state_db_path ):
35- # Try to force close SQLite connections
36- try :
37- # Connect and immediately close to ensure WAL checkpoint
38- conn = sqlite3 .connect (state_db_path , timeout = 1.0 )
39- conn .execute ("PRAGMA wal_checkpoint(TRUNCATE)" )
40- conn .close ()
41- except Exception :
42- pass # Best effort
43-
44- # Give Windows time to release file handles
45- time .sleep (0.1 )
46-
47- # Attempt cleanup with retries on Windows
48- max_retries = 3
49- for attempt in range (max_retries ):
50- try :
51- shutil .rmtree (tmp_dir )
52- break
53- except (PermissionError , OSError ) as e :
54- if attempt < max_retries - 1 :
55- # Wait briefly and try again
56- time .sleep (0.2 )
57- gc .collect ()
58- else :
59- # On final attempt, log warning but don't fail the test
60- import warnings
61-
62- warnings .warn (
63- f"Failed to clean up temporary directory { tmp_dir } : { e } " ,
64- ResourceWarning ,
65- stacklevel = 2 ,
66- )
0 commit comments