41 lines
1.1 KiB
Rust
41 lines
1.1 KiB
Rust
//! Temporary directories for tests. Do not edit.
|
|
|
|
#![allow(dead_code)] // each test file uses a different part of this module
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
static NEXT: AtomicU32 = AtomicU32::new(0);
|
|
|
|
pub struct TempDir(PathBuf);
|
|
|
|
impl TempDir {
|
|
pub fn new(tag: &str) -> TempDir {
|
|
let n = NEXT.fetch_add(1, Ordering::SeqCst);
|
|
let path = std::env::temp_dir().join(format!("gw-{tag}-{}-{n}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&path);
|
|
std::fs::create_dir_all(&path).unwrap();
|
|
TempDir(path)
|
|
}
|
|
|
|
pub fn path(&self) -> &Path {
|
|
&self.0
|
|
}
|
|
|
|
/// Writes `text` to `name` inside the directory and returns the full path.
|
|
pub fn write(&self, name: &str, text: &str) -> PathBuf {
|
|
let path = self.0.join(name);
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent).unwrap();
|
|
}
|
|
std::fs::write(&path, text).unwrap();
|
|
path
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|