Skip to content

cargo_test: remove test roots after success #9701

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 13 additions & 5 deletions crates/cargo-test-support/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,36 @@ thread_local! {
static TEST_ID: RefCell<Option<usize>> = RefCell::new(None);
}

pub struct TestIdGuard {
_private: (),
pub struct TestGuard {
root: Option<PathBuf>,
}

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);
}
}
Expand Down