diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 7990bccb6..774890b2f 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -188,8 +188,7 @@ pub fn test_multiply(config_dir: &Path, https: bool) { } pub fn test_network(https: bool) { - // set to true to always keep the temp dir after test finishes - let dir = TempDir::new(false); + let dir = TempDir::new_delete_on_drop(); let path = dir.path(); println!("generating configuration in {}", path.display()); @@ -206,7 +205,7 @@ pub fn test_ipa(mode: IpaSecurityModel, https: bool) { pub fn test_ipa_with_config(mode: IpaSecurityModel, https: bool, config: IpaQueryConfig) { const INPUT_SIZE: usize = 10; // set to true to always keep the temp dir after test finishes - let dir = TempDir::new(false); + let dir = TempDir::new_delete_on_drop(); let path = dir.path(); println!("generating configuration in {}", path.display()); diff --git a/tests/common/tempdir.rs b/tests/common/tempdir.rs index f1fd69465..3c056ab44 100644 --- a/tests/common/tempdir.rs +++ b/tests/common/tempdir.rs @@ -1,4 +1,4 @@ -use std::{mem, path::Path, thread}; +use std::{path::Path, thread}; use tempfile::tempdir; @@ -12,16 +12,16 @@ pub struct TempDir { } impl TempDir { - /// Creates a new temporary directory. If `delete` is set to `false`, then it won't be cleaned up - /// after drop. + /// Creates a new temporary directory that will be deleted when this instance is dropped. + /// There is an exception if thread is panicking, then it won't be. /// /// ## Panics /// Panics if a new temp dir cannot be created. #[must_use] - pub fn new(delete: bool) -> Self { + pub fn new_delete_on_drop() -> Self { Self { inner: Some(tempdir().expect("Can create temp directory")), - delete, + delete: true, } } @@ -37,7 +37,7 @@ impl Drop for TempDir { fn drop(&mut self) { if !self.delete || thread::panicking() { let td = self.inner.take().unwrap(); - mem::forget(td); + let _ = td.into_path(); } } } diff --git a/tests/helper_networks.rs b/tests/helper_networks.rs index d00340f75..28ffb6f35 100644 --- a/tests/helper_networks.rs +++ b/tests/helper_networks.rs @@ -39,7 +39,7 @@ fn https_semi_honest_ipa() { #[test] #[cfg(all(test, web_test))] fn keygen_confgen() { - let dir = TempDir::new(false); + let dir = TempDir::new_delete_on_drop(); let path = dir.path(); let sockets: [_; 3] = array::from_fn(|_| TcpListener::bind("127.0.0.1:0").unwrap());