From a952ff44e0c73323f87f0669112f4a76db6dfc61 Mon Sep 17 00:00:00 2001 From: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> Date: Fri, 6 Sep 2024 22:30:14 +0530 Subject: [PATCH] chore(shallow_temp_dir): panic if not panicking (#2172) ## Linked Issues/PRs - https://github.com/FuelLabs/fuel-core/pull/2170 ## Description Panics while dropping the database if there is no pre-existing panic, similar to how its done in stdlib :) ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog - [x] New behavior is reflected in tests - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [x] I have reviewed the code myself - [x] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --- 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(); } }