2020-07-23 15:26:25 -05:00
|
|
|
use std::{
|
|
|
|
fs, io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::atomic::{AtomicUsize, Ordering},
|
|
|
|
};
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) struct TestDir {
|
2020-07-23 15:26:25 -05:00
|
|
|
path: PathBuf,
|
|
|
|
keep: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestDir {
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn new() -> TestDir {
|
2020-07-23 15:26:25 -05:00
|
|
|
let base = std::env::temp_dir().join("testdir");
|
|
|
|
let pid = std::process::id();
|
|
|
|
|
|
|
|
static CNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
for _ in 0..100 {
|
|
|
|
let cnt = CNT.fetch_add(1, Ordering::Relaxed);
|
|
|
|
let path = base.join(format!("{}_{}", pid, cnt));
|
|
|
|
if path.is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fs::create_dir_all(&path).unwrap();
|
|
|
|
return TestDir { path, keep: false };
|
|
|
|
}
|
|
|
|
panic!("Failed to create a temporary directory")
|
|
|
|
}
|
|
|
|
#[allow(unused)]
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn keep(mut self) -> TestDir {
|
2020-07-23 15:26:25 -05:00
|
|
|
self.keep = true;
|
|
|
|
self
|
|
|
|
}
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn path(&self) -> &Path {
|
2020-07-23 15:26:25 -05:00
|
|
|
&self.path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestDir {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.keep {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
remove_dir_all(&self.path).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn remove_dir_all(path: &Path) -> io::Result<()> {
|
|
|
|
fs::remove_dir_all(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn remove_dir_all(path: &Path) -> io::Result<()> {
|
|
|
|
for _ in 0..99 {
|
|
|
|
if fs::remove_dir_all(path).is_ok() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10))
|
|
|
|
}
|
|
|
|
fs::remove_dir_all(path)
|
|
|
|
}
|