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:
2026-05-16 09:21:43 +02:00
parent 7731a9daa0
commit 894eb5af6a
10 changed files with 103 additions and 307 deletions
+9 -42
View File
@@ -99,44 +99,11 @@ pub fn validate_game_file_path(game_dir: &Path, relative_path: &str) -> eyre::Re
#[cfg(test)]
mod tests {
use super::*;
fn create_temp_dir() -> std::io::Result<std::path::PathBuf> {
let mut dir = std::env::temp_dir();
let unique = format!(
"lanspread_test_{}_{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
);
dir.push(unique);
std::fs::create_dir_all(&dir)?;
Ok(dir)
}
struct TempDir(std::path::PathBuf);
impl TempDir {
fn new() -> std::io::Result<Self> {
let path = create_temp_dir()?;
Ok(TempDir(path))
}
fn path(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
use crate::test_support::TempDir;
#[test]
fn test_valid_paths() {
let temp_dir = TempDir::new().expect("Failed to create temp dir for test");
let temp_dir = TempDir::new("lanspread-path-validation");
let base = temp_dir.path();
// Valid relative paths
@@ -156,7 +123,7 @@ mod tests {
#[test]
fn test_traversal_attempts() {
let temp_dir = TempDir::new().expect("Failed to create temp dir for test");
let temp_dir = TempDir::new("lanspread-path-validation");
let base = temp_dir.path();
// These should all fail
@@ -167,7 +134,7 @@ mod tests {
#[test]
fn test_double_dot_in_filename_allowed() {
let temp_dir = TempDir::new().expect("Failed to create temp dir for test");
let temp_dir = TempDir::new("lanspread-path-validation");
let base = temp_dir.path();
assert!(validate_game_file_path(base, "data/file..txt").is_ok());
@@ -175,7 +142,7 @@ mod tests {
#[test]
fn test_missing_file_stays_within_base() {
let temp_dir = TempDir::new().expect("Failed to create temp dir for test");
let temp_dir = TempDir::new("lanspread-path-validation");
let base = temp_dir.path();
#[allow(clippy::unwrap_used)]
@@ -191,7 +158,7 @@ mod tests {
#[test]
fn test_absolute_paths() {
let temp_dir = TempDir::new().expect("Failed to create temp dir for test");
let temp_dir = TempDir::new("lanspread-path-validation");
let base = temp_dir.path();
// Absolute paths should fail
@@ -203,7 +170,7 @@ mod tests {
#[test]
fn test_windows_specific() {
let temp_dir = TempDir::new().expect("Failed to create temp dir for test");
let temp_dir = TempDir::new("lanspread-path-validation");
let base = temp_dir.path();
// Windows-specific paths that should fail
@@ -217,8 +184,8 @@ mod tests {
fn test_symlink_escape_rejected() {
use std::os::unix::fs::symlink;
let base_dir = TempDir::new().expect("Failed to create base temp dir");
let outside_dir = TempDir::new().expect("Failed to create outside temp dir");
let base_dir = TempDir::new("lanspread-path-validation-base");
let outside_dir = TempDir::new("lanspread-path-validation-outside");
let base = base_dir.path();
let outside = outside_dir.path();