Skip to content

Commit

Permalink
chore(shallow_temp_dir): panic if not panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Sep 6, 2024
1 parent d5ba082 commit 517abed
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion benches/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -125,12 +127,31 @@ 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
shallow_temp_dir__drops_if_env_var_is_set();
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();
}
}

0 comments on commit 517abed

Please sign in to comment.