Skip to content

Commit

Permalink
chore(shallow_temp_dir): panic if not panicking (#2172)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
<!-- List of related issues/PRs -->
- #2170

## Description
<!-- List of detailed changes -->
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?
  • Loading branch information
rymnc authored Sep 6, 2024
1 parent 195a129 commit a952ff4
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 a952ff4

Please sign in to comment.