test(peer): consolidate temp dir helper
Move the repeated test TempDir implementations into a single peer test_support module. The shared helper keeps the existing automatic cleanup behavior and uses an atomic suffix plus timestamp so parallel tests do not collide on the same path. This is intentionally limited to test hygiene. It does not change the availability model, split download.rs, or touch production scan/install behavior beyond importing the shared helper from test modules. Test Plan: - git diff --check - just fmt - just clippy - just test Follow-up-Plan: FOLLOW_UP_2.md
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
static NEXT_TEMP_ID: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
pub(crate) struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
pub(crate) fn new(prefix: &str) -> Self {
|
||||
let mut path = std::env::temp_dir();
|
||||
let unique_id = NEXT_TEMP_ID.fetch_add(1, Ordering::Relaxed);
|
||||
path.push(format!(
|
||||
"{prefix}-{}-{}-{}",
|
||||
std::process::id(),
|
||||
unique_id,
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos()
|
||||
));
|
||||
std::fs::create_dir_all(&path).expect("temp dir should be created");
|
||||
Self(path)
|
||||
}
|
||||
|
||||
pub(crate) fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub(crate) fn game_root(&self) -> PathBuf {
|
||||
self.0.join("game")
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user