| 1 | //! A fresh directory per test, removed afterwards. |
| 2 | |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 5 | |
| 6 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 7 | |
| 8 | pub struct TestDir(PathBuf); |
| 9 | |
| 10 | impl TestDir { |
| 11 | pub fn new(name: &str) -> TestDir { |
| 12 | let n = NEXT.fetch_add(1, Ordering::Relaxed); |
| 13 | let path = std::env::temp_dir().join(format!("grove-test-{}-{name}-{n}", std::process::id())); |
| 14 | let _ = std::fs::remove_dir_all(&path); |
| 15 | std::fs::create_dir_all(&path).unwrap(); |
| 16 | TestDir(path) |
| 17 | } |
| 18 | |
| 19 | pub fn path(&self) -> &Path { |
| 20 | &self.0 |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl Drop for TestDir { |
| 25 | fn drop(&mut self) { |
| 26 | let _ = std::fs::remove_dir_all(&self.0); |
| 27 | } |
| 28 | } |