@@ -203,6 +203,26 @@ pub mod test_utils {
203203 Arc :: new ( TempDatabase :: new ( db, path) )
204204 }
205205
206+ /// Create read/write database for testing within a data directory.
207+ ///
208+ /// The database is created at `datadir/db`, and `TempDatabase` will clean up the entire
209+ /// `datadir` on drop.
210+ #[ track_caller]
211+ pub fn create_test_rw_db_with_datadir < P : AsRef < Path > > (
212+ datadir : P ,
213+ ) -> Arc < TempDatabase < DatabaseEnv > > {
214+ let datadir = datadir. as_ref ( ) . to_path_buf ( ) ;
215+ let db_path = datadir. join ( "db" ) ;
216+ let emsg = format ! ( "{ERROR_DB_CREATION}: {db_path:?}" ) ;
217+ let db = init_db (
218+ & db_path,
219+ DatabaseArguments :: new ( ClientVersion :: default ( ) )
220+ . with_max_read_transaction_duration ( Some ( MaxReadTransactionDuration :: Unbounded ) ) ,
221+ )
222+ . expect ( & emsg) ;
223+ Arc :: new ( TempDatabase :: new ( db, datadir) )
224+ }
225+
206226 /// Create read only database for testing
207227 #[ track_caller]
208228 pub fn create_test_ro_db ( ) -> Arc < TempDatabase < DatabaseEnv > > {
@@ -235,6 +255,24 @@ mod tests {
235255 use std:: time:: Duration ;
236256 use tempfile:: tempdir;
237257
258+ #[ test]
259+ fn test_temp_database_cleanup ( ) {
260+ // Test that TempDatabase properly cleans up its directory when dropped
261+ let temp_path = {
262+ let db = crate :: test_utils:: create_test_rw_db ( ) ;
263+ let path = db. path ( ) . to_path_buf ( ) ;
264+ assert ! ( path. exists( ) , "Database directory should exist while TempDatabase is alive" ) ;
265+ path
266+ // TempDatabase dropped here
267+ } ;
268+
269+ // Verify the directory was cleaned up
270+ assert ! (
271+ !temp_path. exists( ) ,
272+ "Database directory should be cleaned up after TempDatabase is dropped"
273+ ) ;
274+ }
275+
238276 #[ test]
239277 fn db_version ( ) {
240278 let path = tempdir ( ) . unwrap ( ) ;
0 commit comments