From da959b551e819dba7a8ce5d45b187651771468ff Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:09:12 +0530 Subject: [PATCH] chore(shallow_temp_dir): panic if not panicking --- benches/src/utils.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/benches/src/utils.rs b/benches/src/utils.rs index 812d4f922a5..03f3ba9368c 100644 --- a/benches/src/utils.rs +++ b/benches/src/utils.rs @@ -45,7 +45,9 @@ impl Drop for ShallowTempDir { if should_clean_up { if let Err(e) = std::fs::remove_dir_all(&self.path) { - eprintln!("Failed to remove temp directory: {:?}", e); + if !std::thread::panicking() { + panic!("Failed to remove ShallowTempDir: {}", e); + } } } } @@ -125,6 +127,24 @@ mod tests { env::remove_var(DB_CLEAN_UP_ENV_VAR); } + fn shallow_temp_dir__panics_while_dropping_if_not_panicking() { + // given + env::set_var(DB_CLEAN_UP_ENV_VAR, "true"); + + let result = std::panic::catch_unwind(|| { + let _ = ShallowTempDir::new(); + // when: out of scope, tries to drop + // it will panic when trying to drop, since there + // are no other panics + }); + + // then + assert!(result.is_err()); + + // clean up + env::remove_var(DB_CLEAN_UP_ENV_VAR); + } + #[test] fn test_shallow_temp_dir_behaviour() { // run tests sequentially to avoid conflicts due to env var usage @@ -132,5 +152,6 @@ mod tests { shallow_temp_dir__does_not_drop_if_env_var_is_set(); shallow_temp_dir__drops_if_env_var_is_not_set(); shallow_temp_dir__drops_if_env_var_malformed(); + shallow_temp_dir__panics_while_dropping_if_not_panicking(); } }