Skip to content

Commit 5529df3

Browse files
authored
Clean up e2e test temp dirs on panic (payjoin#565)
Close payjoin#564
2 parents 8c51c8d + 31f206e commit 5529df3

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

payjoin-cli/tests/e2e.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[cfg(feature = "_danger-local-https")]
22
mod e2e {
33
use std::env;
4+
use std::path::PathBuf;
45
use std::process::{ExitStatus, Stdio};
56

67
use nix::sys::signal::{kill, Signal};
78
use nix::unistd::Pid;
89
use payjoin_test_utils::{init_bitcoind_sender_receiver, BoxError};
9-
use tokio::fs;
1010
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
1111
use tokio::process::Command;
1212

@@ -17,6 +17,18 @@ mod e2e {
1717
child.wait().await
1818
}
1919

20+
struct CleanupGuard {
21+
paths: Vec<PathBuf>,
22+
}
23+
24+
impl Drop for CleanupGuard {
25+
fn drop(&mut self) {
26+
for path in &self.paths {
27+
cleanup_temp_file(path);
28+
}
29+
}
30+
}
31+
2032
const RECEIVE_SATS: &str = "54321";
2133

2234
#[cfg(feature = "v1")]
@@ -26,6 +38,8 @@ mod e2e {
2638
let temp_dir = env::temp_dir();
2739
let receiver_db_path = temp_dir.join("receiver_db");
2840
let sender_db_path = temp_dir.join("sender_db");
41+
let _cleanup_guard =
42+
CleanupGuard { paths: vec![receiver_db_path.clone(), sender_db_path.clone()] };
2943
let receiver_db_path_clone = receiver_db_path.clone();
3044
let sender_db_path_clone = sender_db_path.clone();
3145
let port = find_free_port()?;
@@ -130,8 +144,6 @@ mod e2e {
130144
})
131145
.await?;
132146

133-
cleanup_temp_file(&receiver_db_path).await;
134-
cleanup_temp_file(&sender_db_path).await;
135147
assert!(payjoin_sent, "Payjoin send was not detected");
136148

137149
fn find_free_port() -> Result<u16, BoxError> {
@@ -157,14 +169,15 @@ mod e2e {
157169
let temp_dir = env::temp_dir();
158170
let receiver_db_path = temp_dir.join("receiver_db");
159171
let sender_db_path = temp_dir.join("sender_db");
172+
let _cleanup_guard =
173+
CleanupGuard { paths: vec![receiver_db_path.clone(), sender_db_path.clone()] };
174+
160175
let result = tokio::select! {
161176
res = services.take_ohttp_relay_handle() => Err(format!("Ohttp relay is long running: {:?}", res).into()),
162177
res = services.take_directory_handle() => Err(format!("Directory server is long running: {:?}", res).into()),
163178
res = send_receive_cli_async(&services, receiver_db_path.clone(), sender_db_path.clone()) => res,
164179
};
165180

166-
cleanup_temp_file(&receiver_db_path).await;
167-
cleanup_temp_file(&sender_db_path).await;
168181
assert!(result.is_ok(), "send_receive failed: {:#?}", result.unwrap_err());
169182

170183
async fn send_receive_cli_async(
@@ -361,8 +374,8 @@ mod e2e {
361374
Ok(())
362375
}
363376

364-
async fn cleanup_temp_file(path: &std::path::Path) {
365-
if let Err(e) = fs::remove_dir_all(path).await {
377+
fn cleanup_temp_file(path: &std::path::Path) {
378+
if let Err(e) = std::fs::remove_dir_all(path) {
366379
eprintln!("Failed to remove {:?}: {}", path, e);
367380
}
368381
}

0 commit comments

Comments
 (0)