fix: cleanup entire temp directory when using testing_node (#18399)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
CPerezz
2026-01-31 17:46:11 +01:00
committed by GitHub
parent a500fb22ba
commit 9127563914
2 changed files with 41 additions and 1 deletions

View File

@@ -251,6 +251,8 @@ impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
}
/// Creates a preconfigured node for testing purposes with a specific datadir.
///
/// The entire `datadir` will be cleaned up when the node is dropped.
#[cfg(feature = "test-utils")]
pub fn testing_node_with_datadir(
mut self,
@@ -268,7 +270,7 @@ impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
let data_dir =
path.unwrap_or_chain_default(self.config.chain.chain(), self.config.datadir.clone());
let db = reth_db::test_utils::create_test_rw_db_with_path(data_dir.db());
let db = reth_db::test_utils::create_test_rw_db_with_datadir(data_dir.data_dir());
WithLaunchContext { builder: self.with_database(db), task_executor }
}

View File

@@ -203,6 +203,26 @@ pub mod test_utils {
Arc::new(TempDatabase::new(db, path))
}
/// Create read/write database for testing within a data directory.
///
/// The database is created at `datadir/db`, and `TempDatabase` will clean up the entire
/// `datadir` on drop.
#[track_caller]
pub fn create_test_rw_db_with_datadir<P: AsRef<Path>>(
datadir: P,
) -> Arc<TempDatabase<DatabaseEnv>> {
let datadir = datadir.as_ref().to_path_buf();
let db_path = datadir.join("db");
let emsg = format!("{ERROR_DB_CREATION}: {db_path:?}");
let db = init_db(
&db_path,
DatabaseArguments::new(ClientVersion::default())
.with_max_read_transaction_duration(Some(MaxReadTransactionDuration::Unbounded)),
)
.expect(&emsg);
Arc::new(TempDatabase::new(db, datadir))
}
/// Create read only database for testing
#[track_caller]
pub fn create_test_ro_db() -> Arc<TempDatabase<DatabaseEnv>> {
@@ -235,6 +255,24 @@ mod tests {
use std::time::Duration;
use tempfile::tempdir;
#[test]
fn test_temp_database_cleanup() {
// Test that TempDatabase properly cleans up its directory when dropped
let temp_path = {
let db = crate::test_utils::create_test_rw_db();
let path = db.path().to_path_buf();
assert!(path.exists(), "Database directory should exist while TempDatabase is alive");
path
// TempDatabase dropped here
};
// Verify the directory was cleaned up
assert!(
!temp_path.exists(),
"Database directory should be cleaned up after TempDatabase is dropped"
);
}
#[test]
fn db_version() {
let path = tempdir().unwrap();