Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(shallow_temp_dir): panic if not panicking #2172

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading