From 91c4ad3039b122cec8cfca4fdd214e42bedeea15 Mon Sep 17 00:00:00 2001 From: OzzyHelper <346962152+OzzyHelper@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:08:42 +0000 Subject: [PATCH] test: isolate filesystem tests from the real ~/.foc-devnet Add a test-only TestHome override in paths that redirects foc_devnet_home() into a temp directory. It shares one process-wide lock with the FOC_DEVNET_BASEDIR tests so env-based and override-based path resolution cannot race. Use it for keys/mnemonic, run-ID persistence, latest symlink, build status, and disk usage tests. Rewrite the latest-symlink test to call the real create_latest_symlink and cover broken-link recreation and valid-link replacement. Remove test_print_running_status, which only asserted is_ok() while shelling out to Docker. --- src/commands/init/keys.rs | 5 ++ src/commands/status/build_status.rs | 5 ++ src/commands/status/disk_usage.rs | 5 ++ src/commands/status/running.rs | 10 --- src/paths.rs | 98 +++++++++++++++++++++++++---- src/run_id/mod.rs | 55 +++++++--------- src/run_id/persistence.rs | 4 ++ 7 files changed, 129 insertions(+), 53 deletions(-) diff --git a/src/commands/init/keys.rs b/src/commands/init/keys.rs index e2e2f249..e35d3896 100644 --- a/src/commands/init/keys.rs +++ b/src/commands/init/keys.rs @@ -135,10 +135,15 @@ pub fn load_keys() -> Result, Box> { #[cfg(test)] mod tests { + use crate::paths::TestHome; + use super::*; #[test] fn test_generate_keys_includes_bls_keys() { + let home = tempfile::TempDir::new().unwrap(); + let _home = TestHome::new(home.path()); + let keys = generate_keys(false).unwrap(); // Check that we have the expected number of keys diff --git a/src/commands/status/build_status.rs b/src/commands/status/build_status.rs index 4ad0283f..fa051fa1 100644 --- a/src/commands/status/build_status.rs +++ b/src/commands/status/build_status.rs @@ -85,10 +85,15 @@ fn get_binary_version(binary_path: &std::path::Path) -> String { #[cfg(test)] mod tests { + use crate::paths::TestHome; + use super::*; #[test] fn test_print_build_status() { + let home = tempfile::TempDir::new().unwrap(); + let _home = TestHome::new(home.path()); + // This test mainly verifies that the function doesn't panic // In a real scenario, you'd mock the foc_devnet_bin function // or set up the environment properly diff --git a/src/commands/status/disk_usage.rs b/src/commands/status/disk_usage.rs index f5086c88..7285b296 100644 --- a/src/commands/status/disk_usage.rs +++ b/src/commands/status/disk_usage.rs @@ -103,10 +103,15 @@ pub fn print_disk_usage() -> Result<(), Box> { #[cfg(test)] mod tests { + use crate::paths::TestHome; + use super::*; #[test] fn test_print_disk_usage() { + let home = tempfile::TempDir::new().unwrap(); + let _home = TestHome::new(home.path()); + // This test verifies that the function doesn't panic let result = print_disk_usage(); // We expect this to work even if directories don't exist diff --git a/src/commands/status/running.rs b/src/commands/status/running.rs index 670f9fff..c51555b9 100644 --- a/src/commands/status/running.rs +++ b/src/commands/status/running.rs @@ -172,16 +172,6 @@ fn format_container_ports(container_name: &str) -> Result PathBuf { + #[cfg(test)] + if let Some(test_home) = test_home::current() { + return test_home; + } + let default_path = || { dirs::home_dir() .unwrap_or_else(|| PathBuf::from("/tmp")) @@ -257,15 +263,83 @@ pub fn project_root() -> Result { /// Container path where Filecoin proof parameters are mounted pub const CONTAINER_FILECOIN_PROOF_PARAMS_PATH: &str = "/var/tmp/filecoin-proof-parameters"; +/// Test-only redirect for [`foc_devnet_home`]. +/// +/// Unit tests must never touch the real `~/.foc-devnet`: tests run in parallel +/// and writing shared user state makes them order-dependent and destructive. +/// The override and the `FOC_DEVNET_BASEDIR` tests share one process-wide lock, +/// because both mutate how paths resolve globally. +#[cfg(test)] +mod test_home { + use std::path::{Path, PathBuf}; + use std::sync::{Mutex, MutexGuard}; + + static HOME_OVERRIDE: Mutex> = Mutex::new(None); + static HOME_MUTEX: Mutex<()> = Mutex::new(()); + + fn lock() -> MutexGuard<'static, ()> { + // A panicking test poisons the lock; recovering keeps it serializing + // rather than turning every later test into a failure. + HOME_MUTEX.lock().unwrap_or_else(|error| error.into_inner()) + } + + pub(super) fn current() -> Option { + HOME_OVERRIDE + .lock() + .unwrap_or_else(|error| error.into_inner()) + .clone() + } + + /// Holds the process-wide path lock for the duration of a test and points + /// `foc_devnet_home()` at `path`. + #[must_use = "the override is cleared when the guard drops; bind it to a local"] + pub struct TestHome { + _lock: MutexGuard<'static, ()>, + installed_override: bool, + } + + impl TestHome { + pub fn new(path: &Path) -> Self { + let lock = lock(); + *HOME_OVERRIDE + .lock() + .unwrap_or_else(|error| error.into_inner()) = Some(path.to_path_buf()); + TestHome { + _lock: lock, + installed_override: true, + } + } + + /// Takes the shared path lock without changing the home directory. + /// + /// Use this for tests that set `FOC_DEVNET_BASEDIR` so they cannot run + /// concurrently with a `TestHome` override. + pub fn lock_only() -> Self { + TestHome { + _lock: lock(), + installed_override: false, + } + } + } + + impl Drop for TestHome { + fn drop(&mut self) { + if self.installed_override { + *HOME_OVERRIDE + .lock() + .unwrap_or_else(|error| error.into_inner()) = None; + } + } + } +} + +#[cfg(test)] +pub use test_home::TestHome; + #[cfg(test)] mod tests { use super::*; use std::env; - use std::sync::Mutex; - - /// Serializes all tests that mutate FOC_DEVNET_BASEDIR to prevent - /// race conditions when the test suite uses multiple threads. - static ENV_MUTEX: Mutex<()> = Mutex::new(()); /// Helper to safely set and restore environment variables in tests struct EnvGuard { @@ -295,7 +369,7 @@ mod tests { #[test] fn test_foc_devnet_home_with_custom_basedir() { - let _lock = ENV_MUTEX.lock().unwrap(); + let _lock = TestHome::lock_only(); let _guard = EnvGuard::new("FOC_DEVNET_BASEDIR", Some("/tmp/my-foc-devnet")); let path = foc_devnet_home(); assert_eq!(path, PathBuf::from("/tmp/my-foc-devnet")); @@ -303,7 +377,7 @@ mod tests { #[test] fn test_foc_devnet_home_with_tilde_expansion() { - let _lock = ENV_MUTEX.lock().unwrap(); + let _lock = TestHome::lock_only(); let _guard = EnvGuard::new("FOC_DEVNET_BASEDIR", Some("~/my-foc-devnet")); let path = foc_devnet_home(); @@ -315,7 +389,7 @@ mod tests { #[test] fn test_foc_devnet_home_with_empty_basedir() { - let _lock = ENV_MUTEX.lock().unwrap(); + let _lock = TestHome::lock_only(); let _guard = EnvGuard::new("FOC_DEVNET_BASEDIR", Some("")); let path = foc_devnet_home(); @@ -328,7 +402,7 @@ mod tests { #[test] fn test_foc_devnet_home_with_whitespace_basedir() { - let _lock = ENV_MUTEX.lock().unwrap(); + let _lock = TestHome::lock_only(); let _guard = EnvGuard::new("FOC_DEVNET_BASEDIR", Some(" ")); let path = foc_devnet_home(); @@ -341,7 +415,7 @@ mod tests { #[test] fn test_foc_devnet_home_no_env_var() { - let _lock = ENV_MUTEX.lock().unwrap(); + let _lock = TestHome::lock_only(); let _guard = EnvGuard::new("FOC_DEVNET_BASEDIR", None); let path = foc_devnet_home(); @@ -355,7 +429,7 @@ mod tests { #[test] fn test_dependent_paths_use_foc_devnet_home() { - let _lock = ENV_MUTEX.lock().unwrap(); + let _lock = TestHome::lock_only(); let _guard = EnvGuard::new("FOC_DEVNET_BASEDIR", Some("/tmp/test-foc")); // All dependent paths should use the custom base directory diff --git a/src/run_id/mod.rs b/src/run_id/mod.rs index feb3affe..f7c32a19 100644 --- a/src/run_id/mod.rs +++ b/src/run_id/mod.rs @@ -112,9 +112,11 @@ pub fn create_latest_symlink(run_id: &str) -> Result<(), Box Result<(), Box> { #[cfg(test)] mod tests { + use crate::paths::TestHome; + use super::*; #[test] fn test_save_and_load_run_id() { let test_run_id = "251203-1246-test-wolf"; + let home = tempfile::TempDir::new().unwrap(); + let _home = TestHome::new(home.path()); // Save save_current_run_id(test_run_id).unwrap();