Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/commands/init/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/commands/status/build_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/commands/status/disk_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 0 additions & 10 deletions src/commands/status/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ fn format_container_ports(container_name: &str) -> Result<String, Box<dyn std::e
mod tests {
use super::*;

#[test]
fn test_print_running_status() {
// This test verifies that the function doesn't panic
// In a real environment with Docker, it would check actual containers
let result = print_running_status();
// We expect this to work even if Docker is not available
// (it will just show empty results)
assert!(result.is_ok());
}

#[test]
fn test_extract_base_image_name() {
assert_eq!(
Expand Down
96 changes: 84 additions & 12 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,88 @@ use std::path::PathBuf;

use tracing::warn;

/// Process-wide override used by tests to redirect foc-devnet paths away from
/// the developer's real home directory.
#[cfg(test)]
pub use test_home::TestHome;

#[cfg(test)]
mod test_home {
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};

/// Serializes every test that resolves or mutates foc-devnet paths.
/// `FOC_DEVNET_BASEDIR` tests share this lock through `lock_only`, so they
/// cannot race with tests using a `TestHome` override.
static HOME_LOCK: Mutex<()> = Mutex::new(());
static OVERRIDE: Mutex<Option<PathBuf>> = 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.
///
/// Every test that resolves foc-devnet paths must hold a `TestHome`,
/// otherwise it can observe another test's override.
pub struct TestHome {
_lock: MutexGuard<'static, ()>,
sets_override: bool,
}

impl TestHome {
/// Redirect every path derived from `foc_devnet_home()` to `path`.
pub fn new(path: impl AsRef<Path>) -> Self {
let guard = lock();
*OVERRIDE.lock().unwrap_or_else(PoisonError::into_inner) =
Some(path.as_ref().to_path_buf());
TestHome {
_lock: guard,
sets_override: true,
}
}

/// 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(),
sets_override: false,
}
}
}

impl Drop for TestHome {
fn drop(&mut self) {
if self.sets_override {
*OVERRIDE.lock().unwrap_or_else(PoisonError::into_inner) = None;
}
}
}

pub(super) fn override_path() -> Option<PathBuf> {
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.
/// In test builds, a `TestHome` override takes precedence.
/// Otherwise checks for the $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"))
Expand Down Expand Up @@ -261,11 +338,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 {
Expand Down Expand Up @@ -295,15 +367,15 @@ 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"));
}

#[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();

Expand All @@ -315,7 +387,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();

Expand All @@ -328,7 +400,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();

Expand All @@ -341,7 +413,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();
Expand All @@ -355,7 +427,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
Expand Down
54 changes: 21 additions & 33 deletions src/run_id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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"
);
}
}
2 changes: 2 additions & 0 deletions src/run_id/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down