Skip to content

Commit 8bfd057

Browse files
unhappychoiceclaude
andcommitted
test(fetcher): restore SPLASHBOARD_HOME via RAII guard for panic safety
The cache-hit thumbnail tests in forge_items, steam/games, and steam/player_summary restored `SPLASHBOARD_HOME` only on the happy-path tail, so a panic mid-test would leak the temp-dir override into later tests sharing the process env. Replace the manual save/restore (and steam/games' `restore_home` helper) with a `SplashHomeGuard` RAII struct that always restores on drop, matching the existing `EnvGuard` pattern in github/avatar and runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 2d18654 commit 8bfd057

3 files changed

Lines changed: 69 additions & 27 deletions

File tree

‎src/fetcher/forge_items.rs‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,29 @@ mod tests {
540540
assert!(b.items[0].title.starts_with("splashboard#1"));
541541
}
542542

543+
/// RAII guard: points `SPLASHBOARD_HOME` at a temp dir and restores it on drop, so a
544+
/// panic mid-test can't leak the override into later tests sharing the process env.
545+
struct SplashHomeGuard(Option<String>);
546+
547+
impl SplashHomeGuard {
548+
fn set(path: &std::path::Path) -> Self {
549+
let previous = std::env::var("SPLASHBOARD_HOME").ok();
550+
unsafe { std::env::set_var("SPLASHBOARD_HOME", path) };
551+
Self(previous)
552+
}
553+
}
554+
555+
impl Drop for SplashHomeGuard {
556+
fn drop(&mut self) {
557+
unsafe {
558+
match self.0.take() {
559+
Some(value) => std::env::set_var("SPLASHBOARD_HOME", value),
560+
None => std::env::remove_var("SPLASHBOARD_HOME"),
561+
}
562+
}
563+
}
564+
}
565+
543566
#[tokio::test]
544567
#[allow(clippy::await_holding_lock)]
545568
async fn dispatch_rows_async_image_linked_list_fills_avatar_path_from_cache_hit() {
@@ -551,8 +574,7 @@ mod tests {
551574
.lock()
552575
.unwrap_or_else(|e| e.into_inner());
553576
let tmp = tempfile::tempdir().unwrap();
554-
let previous = std::env::var("SPLASHBOARD_HOME").ok();
555-
unsafe { std::env::set_var("SPLASHBOARD_HOME", tmp.path()) };
577+
let _home = SplashHomeGuard::set(tmp.path());
556578

557579
let avatar = "https://avatars.example/seeded.png";
558580
let hash: String = Sha256::digest(avatar.as_bytes())
@@ -574,13 +596,6 @@ mod tests {
574596
}];
575597
let body = dispatch_rows_async(rows, Shape::ImageLinkedList, "open PR", "open PRs").await;
576598

577-
unsafe {
578-
match previous {
579-
Some(value) => std::env::set_var("SPLASHBOARD_HOME", value),
580-
None => std::env::remove_var("SPLASHBOARD_HOME"),
581-
}
582-
}
583-
584599
let Body::ImageLinkedList(b) = body else {
585600
panic!("expected image linked list");
586601
};

‎src/fetcher/steam/games.rs‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,25 @@ mod tests {
396396
.collect()
397397
}
398398

399-
fn restore_home(previous: Option<String>) {
400-
unsafe {
401-
match previous {
402-
Some(value) => std::env::set_var("SPLASHBOARD_HOME", value),
403-
None => std::env::remove_var("SPLASHBOARD_HOME"),
399+
/// RAII guard: points `SPLASHBOARD_HOME` at a temp dir and restores it on drop, so a
400+
/// panic mid-test can't leak the override into later tests sharing the process env.
401+
struct SplashHomeGuard(Option<String>);
402+
403+
impl SplashHomeGuard {
404+
fn set(path: &std::path::Path) -> Self {
405+
let previous = std::env::var("SPLASHBOARD_HOME").ok();
406+
unsafe { std::env::set_var("SPLASHBOARD_HOME", path) };
407+
Self(previous)
408+
}
409+
}
410+
411+
impl Drop for SplashHomeGuard {
412+
fn drop(&mut self) {
413+
unsafe {
414+
match self.0.take() {
415+
Some(value) => std::env::set_var("SPLASHBOARD_HOME", value),
416+
None => std::env::remove_var("SPLASHBOARD_HOME"),
417+
}
404418
}
405419
}
406420
}
@@ -420,9 +434,8 @@ mod tests {
420434
let _lock = crate::paths::TEST_ENV_LOCK
421435
.lock()
422436
.unwrap_or_else(|e| e.into_inner());
423-
let previous = std::env::var("SPLASHBOARD_HOME").ok();
424437
let dir = tempfile::tempdir().unwrap();
425-
unsafe { std::env::set_var("SPLASHBOARD_HOME", dir.path()) };
438+
let _home = SplashHomeGuard::set(dir.path());
426439

427440
let rows = vec![
428441
row(1, 730, "CS2", 60, "60h"),
@@ -436,7 +449,6 @@ mod tests {
436449
}
437450

438451
let body = block_on(image_linked_body(&rows));
439-
restore_home(previous);
440452

441453
let Body::ImageLinkedList(d) = body else {
442454
panic!("expected image_linked_list");
@@ -461,9 +473,8 @@ mod tests {
461473
let _lock = crate::paths::TEST_ENV_LOCK
462474
.lock()
463475
.unwrap_or_else(|e| e.into_inner());
464-
let previous = std::env::var("SPLASHBOARD_HOME").ok();
465476
let dir = tempfile::tempdir().unwrap();
466-
unsafe { std::env::set_var("SPLASHBOARD_HOME", dir.path()) };
477+
let _home = SplashHomeGuard::set(dir.path());
467478

468479
let rows = vec![
469480
row(1, 730, "CS2", 60, "60h"),
@@ -475,7 +486,6 @@ mod tests {
475486
std::fs::write(&file, b"\x89PNG\r\n\x1a\n").unwrap();
476487

477488
let body = block_on(image_body(&rows));
478-
restore_home(previous);
479489

480490
let Body::Image(img) = body else {
481491
panic!("expected image");

‎src/fetcher/steam/player_summary.rs‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -777,15 +777,37 @@ mod tests {
777777
(format!("http://{addr}/avatar.png"), handle)
778778
}
779779

780+
/// RAII guard: points `SPLASHBOARD_HOME` at a temp dir and restores it on drop, so a
781+
/// panic mid-test can't leak the override into later tests sharing the process env.
782+
struct SplashHomeGuard(Option<String>);
783+
784+
impl SplashHomeGuard {
785+
fn set(path: &std::path::Path) -> Self {
786+
let previous = std::env::var("SPLASHBOARD_HOME").ok();
787+
unsafe { std::env::set_var("SPLASHBOARD_HOME", path) };
788+
Self(previous)
789+
}
790+
}
791+
792+
impl Drop for SplashHomeGuard {
793+
fn drop(&mut self) {
794+
unsafe {
795+
match self.0.take() {
796+
Some(value) => std::env::set_var("SPLASHBOARD_HOME", value),
797+
None => std::env::remove_var("SPLASHBOARD_HOME"),
798+
}
799+
}
800+
}
801+
}
802+
780803
#[tokio::test]
781804
#[allow(clippy::await_holding_lock)]
782805
async fn image_bodies_resolve_avatar_through_thumbnail_cache() {
783806
let _lock = crate::paths::TEST_ENV_LOCK
784807
.lock()
785808
.unwrap_or_else(|e| e.into_inner());
786809
let tmp = tempfile::tempdir().unwrap();
787-
let previous = std::env::var("SPLASHBOARD_HOME").ok();
788-
unsafe { std::env::set_var("SPLASHBOARD_HOME", tmp.path()) };
810+
let _home = SplashHomeGuard::set(tmp.path());
789811

790812
let (url, server) = serve_png_once();
791813
let snap = Snapshot {
@@ -803,11 +825,6 @@ mod tests {
803825
panic!("expected image");
804826
};
805827

806-
match previous {
807-
Some(v) => unsafe { std::env::set_var("SPLASHBOARD_HOME", v) },
808-
None => unsafe { std::env::remove_var("SPLASHBOARD_HOME") },
809-
}
810-
811828
let thumb = list.items[0]
812829
.thumbnail_path
813830
.as_deref()

0 commit comments

Comments
 (0)