use std::io;
use std::path::Path;
use std::process::{Command, Stdio};
use std::thread;
pub fn remove_dir_in_background(dir: &Path) -> io::Result<()> {
let mut child = Command::new("rm")
.arg("-rf")
.arg(dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
thread::spawn(move || {
let _ = child.wait();
});
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{Duration, Instant};
#[test]
fn the_directory_is_gone_after_the_call_returns() {
let root = tempfile::tempdir().unwrap();
let dir = root.path().join("ws");
std::fs::create_dir_all(dir.join("nested/deeper")).unwrap();
std::fs::write(dir.join("nested/deeper/file"), "x").unwrap();
remove_dir_in_background(&dir).unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
while dir.exists() {
assert!(Instant::now() < deadline, "{} was never removed", dir.display());
thread::sleep(Duration::from_millis(20));
}
}
#[test]
fn a_missing_directory_is_not_an_error() {
let root = tempfile::tempdir().unwrap();
remove_dir_in_background(&root.path().join("never-existed")).unwrap();
}
}