diff --git a/src/commands/init/keys.rs b/src/commands/init/keys.rs index e2e2f249..7b028097 100644 --- a/src/commands/init/keys.rs +++ b/src/commands/init/keys.rs @@ -139,6 +139,8 @@ mod tests { #[test] fn test_generate_keys_includes_bls_keys() { + let home = tempfile::TempDir::new().unwrap(); + let _home = crate::paths::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..76434ebc 100644 --- a/src/commands/status/build_status.rs +++ b/src/commands/status/build_status.rs @@ -89,9 +89,8 @@ mod tests { #[test] fn test_print_build_status() { - // 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 + let home = tempfile::TempDir::new().unwrap(); + let _home = crate::paths::TestHome::new(home.path()); let result = print_build_status(); // We expect this to work even if no binaries exist assert!(result.is_ok()); diff --git a/src/commands/status/disk_usage.rs b/src/commands/status/disk_usage.rs index f5086c88..49fdc60e 100644 --- a/src/commands/status/disk_usage.rs +++ b/src/commands/status/disk_usage.rs @@ -107,7 +107,8 @@ mod tests { #[test] fn test_print_disk_usage() { - // This test verifies that the function doesn't panic + let home = tempfile::TempDir::new().unwrap(); + let _home = crate::paths::TestHome::new(home.path()); let result = print_disk_usage(); // We expect this to work even if directories don't exist assert!(result.is_ok()); 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 = Mutex::new(()); + static OVERRIDE: Mutex> = Mutex::new(None); + + fn lock() -> MutexGuard<'static, ()> { + HOME_LOCK.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// Test-only replacement for `~/.foc-devnet`. + /// + /// Holds the process-wide path lock for as long as it is alive. Declare the + /// backing `TempDir` before the guard so it outlives the override. + pub struct TestHome { + _lock: MutexGuard<'static, ()>, + } + + impl TestHome { + /// Redirect every path derived from `foc_devnet_home()` to `path`. + pub fn new(path: impl AsRef) -> Self { + let guard = lock(); + *OVERRIDE.lock().unwrap_or_else(PoisonError::into_inner) = + Some(path.as_ref().to_path_buf()); + TestHome { _lock: guard } + } + + /// Acquire the lock without redirecting the home directory. Use for + /// tests that set `FOC_DEVNET_BASEDIR` themselves. + pub fn lock_only() -> Self { + TestHome { _lock: lock() } + } + } + + impl Drop for TestHome { + fn drop(&mut self) { + *OVERRIDE.lock().unwrap_or_else(PoisonError::into_inner) = None; + } + } + + pub(super) fn override_path() -> Option { + OVERRIDE + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } +} + /// Returns the path to the foc-devnet home directory. /// First checks for $FOC_DEVNET_BASEDIR environment variable. /// If not set, defaults to ~/.foc-devnet /// Supports tilde expansion for paths like ~/my-foc-devnet pub fn foc_devnet_home() -> PathBuf { + // Tests must never read or write the developer's real home directory. + #[cfg(test)] + if let Some(path) = test_home::override_path() { + return path; + } + let default_path = || { dirs::home_dir() .unwrap_or_else(|| PathBuf::from("/tmp")) @@ -261,11 +325,6 @@ pub const CONTAINER_FILECOIN_PROOF_PARAMS_PATH: &str = "/var/tmp/filecoin-proof- 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 +354,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 +362,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 +374,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 +387,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 +400,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 +414,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..b770b5d7 100644 --- a/src/run_id/mod.rs +++ b/src/run_id/mod.rs @@ -140,36 +140,23 @@ mod tests { } #[test] - fn test_create_latest_symlink_handles_broken_symlinks() { - // This test verifies that create_latest_symlink properly handles - // removing broken symlinks and creating new ones - use std::os::unix::fs::symlink; - use tempfile::TempDir; + fn test_create_latest_symlink_replaces_broken_and_existing_links() { + let home = tempfile::TempDir::new().expect("Failed to create temp dir"); + let _home = crate::paths::TestHome::new(home.path()); + let latest_link = crate::paths::foc_devnet_state_latest(); - let temp_dir = TempDir::new().expect("Failed to create temp dir"); - - // Mock the paths by using temp directory structure - let runs_dir = temp_dir.path().join("run"); - let state_dir = temp_dir.path().join("state"); - let latest_link = state_dir.join("latest"); - - std::fs::create_dir_all(&runs_dir).expect("Failed to create runs dir"); - std::fs::create_dir_all(&state_dir).expect("Failed to create state dir"); - - // Create first run directory - let run_dir1 = runs_dir.join("run1"); + let run_dir1 = crate::paths::foc_devnet_run_dir("run1"); std::fs::create_dir_all(&run_dir1).expect("Failed to create run1 dir"); - - // Create initial symlink - symlink(&run_dir1, &latest_link).expect("Failed to create initial symlink"); - assert!(latest_link.is_symlink(), "Initial symlink should exist"); + create_latest_symlink("run1").expect("Failed to create latest symlink"); + assert!(latest_link.is_symlink(), "Symlink should exist"); assert_eq!( std::fs::read_link(&latest_link).unwrap(), run_dir1, "Symlink should point to run1" ); - // Delete the target to create a broken symlink + // Removing the target leaves a broken symlink, which must still be + // replaceable. std::fs::remove_dir_all(&run_dir1).expect("Failed to remove run1 dir"); assert!( latest_link.is_symlink(), @@ -180,23 +167,24 @@ mod tests { "Broken symlink target should not exist" ); - // Create a new run directory - let run_dir2 = runs_dir.join("run2"); + let run_dir2 = crate::paths::foc_devnet_run_dir("run2"); std::fs::create_dir_all(&run_dir2).expect("Failed to create run2 dir"); - - // Remove the broken symlink and create new one - if latest_link.is_symlink() { - std::fs::remove_file(&latest_link).expect("Failed to remove broken symlink"); - } - assert!(!latest_link.exists(), "Symlink should be removed"); - - symlink(&run_dir2, &latest_link).expect("Failed to create new symlink"); - assert!(latest_link.is_symlink(), "New symlink should exist"); + create_latest_symlink("run2").expect("Failed to replace broken symlink"); assert_eq!( std::fs::read_link(&latest_link).unwrap(), run_dir2, "Symlink should point to run2" ); assert!(latest_link.exists(), "New symlink target should exist"); + + // A valid existing link must also be replaced. + let run_dir3 = crate::paths::foc_devnet_run_dir("run3"); + std::fs::create_dir_all(&run_dir3).expect("Failed to create run3 dir"); + create_latest_symlink("run3").expect("Failed to replace existing symlink"); + assert_eq!( + std::fs::read_link(&latest_link).unwrap(), + run_dir3, + "Symlink should point to run3" + ); } } diff --git a/src/run_id/persistence.rs b/src/run_id/persistence.rs index f847fd0a..bf03111c 100644 --- a/src/run_id/persistence.rs +++ b/src/run_id/persistence.rs @@ -86,6 +86,8 @@ mod tests { #[test] fn test_save_and_load_run_id() { + let home = tempfile::TempDir::new().unwrap(); + let _home = crate::paths::TestHome::new(home.path()); let test_run_id = "251203-1246-test-wolf"; // Save