From 2269cb0d848cd6dc919464f56b493bece0acf668 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 16 Jul 2021 15:34:29 -0700 Subject: [PATCH] cargo_test: remove test roots after success The test directories under `.../cit/` can take up a lot of space, and they're not useful to keep around for successful tests. The test guard now removes each "root" directory, using `panicking` as a proxy for failure to instead leave it for inspection. --- crates/cargo-test-support/src/paths.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs index ade9a3525ae..52575f15a45 100644 --- a/crates/cargo-test-support/src/paths.rs +++ b/crates/cargo-test-support/src/paths.rs @@ -63,28 +63,36 @@ thread_local! { static TEST_ID: RefCell> = RefCell::new(None); } -pub struct TestIdGuard { - _private: (), +pub struct TestGuard { + root: Option, } -pub fn init_root(tmp_dir: Option<&'static str>) -> TestIdGuard { +pub fn init_root(tmp_dir: Option<&'static str>) -> TestGuard { static NEXT_ID: AtomicUsize = AtomicUsize::new(0); let id = NEXT_ID.fetch_add(1, Ordering::SeqCst); TEST_ID.with(|n| *n.borrow_mut() = Some(id)); - let guard = TestIdGuard { _private: () }; + let mut guard = TestGuard { root: None }; set_global_root(tmp_dir); let r = root(); r.rm_rf(); r.mkdir_p(); + guard.root = Some(r); guard } -impl Drop for TestIdGuard { +impl Drop for TestGuard { fn drop(&mut self) { + if let Some(r) = self.root.take() { + // Clean up the root directory only after successful tests, + // using `panicking` as a proxy for failure. + if !std::thread::panicking() { + r.rm_rf(); + } + } TEST_ID.with(|n| *n.borrow_mut() = None); } }