|
43 | 43 | .unwrap_or_else(|error| panic!("[{name}] {error}")); |
44 | 44 | let mut cx = Context::new(&client, &gateway.workspace, Cli::from_env(&gateway)); |
45 | 45 | eprintln!("[{name}] run id {}", cx.run_id()); |
| 46 | + run_with_cleanup(name, &mut cx, body, async |cx: &mut Context| { |
| 47 | + cx.cleanup().await |
| 48 | + }) |
| 49 | + .await; |
| 50 | +} |
46 | 51 |
|
47 | | - let outcome = AssertUnwindSafe(body(&mut cx)).catch_unwind().await; |
48 | | - let cleanup_failures = cx.cleanup().await; |
| 52 | +/// Run `body`, then `cleanup` whether the body passed, failed, or panicked. |
| 53 | +async fn run_with_cleanup<C, F, G>(name: &str, cx: &mut C, body: F, cleanup: G) |
| 54 | +where |
| 55 | + F: AsyncFnOnce(&mut C) -> Result, |
| 56 | + G: AsyncFnOnce(&mut C) -> Vec<String>, |
| 57 | +{ |
| 58 | + let outcome = AssertUnwindSafe(body(cx)).catch_unwind().await; |
| 59 | + let cleanup_failures = cleanup(cx).await; |
49 | 60 | for failure in &cleanup_failures { |
50 | 61 | eprintln!("[{name}] cleanup failed: {failure}"); |
51 | 62 | } |
|
60 | 71 | ), |
61 | 72 | } |
62 | 73 | } |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use super::*; |
| 78 | + use crate::error::Error; |
| 79 | + |
| 80 | + /// Runs `body` with a cleanup that records it ran and fails with |
| 81 | + /// `cleanup_failures`. Returns whether cleanup ran and the panic message. |
| 82 | + async fn run_test( |
| 83 | + body: impl AsyncFnOnce(&mut bool) -> Result, |
| 84 | + cleanup_failures: Vec<String>, |
| 85 | + ) -> (bool, Option<String>) { |
| 86 | + let mut cleaned_up = false; |
| 87 | + let outcome = AssertUnwindSafe(run_with_cleanup( |
| 88 | + "t", |
| 89 | + &mut cleaned_up, |
| 90 | + body, |
| 91 | + async |cleaned_up: &mut bool| { |
| 92 | + *cleaned_up = true; |
| 93 | + cleanup_failures |
| 94 | + }, |
| 95 | + )) |
| 96 | + .catch_unwind() |
| 97 | + .await; |
| 98 | + let message = outcome.err().map(|panic| { |
| 99 | + panic |
| 100 | + .downcast_ref::<String>() |
| 101 | + .cloned() |
| 102 | + .or_else(|| panic.downcast_ref::<&str>().map(ToString::to_string)) |
| 103 | + .unwrap_or_default() |
| 104 | + }); |
| 105 | + (cleaned_up, message) |
| 106 | + } |
| 107 | + |
| 108 | + #[tokio::test] |
| 109 | + async fn passes_after_cleanup() { |
| 110 | + let (cleaned_up, panic) = run_test(async |_| Ok(()), vec![]).await; |
| 111 | + assert!(cleaned_up); |
| 112 | + assert_eq!(panic, None); |
| 113 | + } |
| 114 | + |
| 115 | + #[tokio::test] |
| 116 | + async fn cleans_up_after_failure() { |
| 117 | + let (cleaned_up, panic) = run_test(async |_| Err(Error::msg("boom")), vec![]).await; |
| 118 | + assert!(cleaned_up); |
| 119 | + assert_eq!(panic.as_deref(), Some("[t] boom")); |
| 120 | + } |
| 121 | + |
| 122 | + #[tokio::test] |
| 123 | + async fn cleans_up_after_panic() { |
| 124 | + let (cleaned_up, panic) = run_test(async |_| panic!("kaboom"), vec![]).await; |
| 125 | + assert!(cleaned_up); |
| 126 | + assert_eq!(panic.as_deref(), Some("kaboom")); |
| 127 | + } |
| 128 | + |
| 129 | + #[tokio::test] |
| 130 | + async fn fails_when_cleanup_fails_after_pass() { |
| 131 | + let failures = vec!["delete sandbox a: gone wrong".to_string()]; |
| 132 | + let (cleaned_up, panic) = run_test(async |_| Ok(()), failures).await; |
| 133 | + assert!(cleaned_up); |
| 134 | + assert_eq!( |
| 135 | + panic.as_deref(), |
| 136 | + Some("[t] passed, but cleanup failed: delete sandbox a: gone wrong") |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments