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
+5 -33
View File
@@ -620,41 +620,13 @@ pub async fn get_game_file_descriptions(
mod tests {
use std::{
collections::{HashMap, HashSet},
path::{Path, PathBuf},
path::Path,
};
use lanspread_proto::Availability;
use super::*;
use crate::context::OperationKind;
struct TempDir(PathBuf);
impl TempDir {
fn new() -> Self {
let mut path = std::env::temp_dir();
path.push(format!(
"lanspread-local-games-{}-{}",
std::process::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)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
use crate::{context::OperationKind, test_support::TempDir};
fn write_file(path: &Path, bytes: &[u8]) {
if let Some(parent) = path.parent() {
@@ -665,7 +637,7 @@ mod tests {
#[tokio::test]
async fn scan_uses_version_ini_and_local_dir_as_independent_state() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-games");
let catalog = HashSet::from([
"ready".to_string(),
"local-only".to_string(),
@@ -718,7 +690,7 @@ mod tests {
#[tokio::test]
async fn rescan_promotes_installed_only_game_to_ready_when_sentinel_appears() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-games");
let catalog = HashSet::from(["game".to_string()]);
std::fs::create_dir_all(temp.path().join("game").join("local"))
.expect("local install dir should be created");
@@ -751,7 +723,7 @@ mod tests {
#[tokio::test]
async fn local_download_available_gates_on_catalog_operation_and_sentinel() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-games");
let game_root = temp.path().join("game");
write_file(&game_root.join("version.ini"), b"20250101");