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
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ reqwest = { version = "0.11", features = ["blocking"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
sha2 = "0.10"
num_cpus = "1.16"
num_cpus = "1.16"

[dev-dependencies]
serial_test = "3.0"
73 changes: 69 additions & 4 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ use std::path::PathBuf;

/// Returns the path to the foc-devnet home directory.
/// First checks for $FOC_DEVNET_BASEDIR environment variable.
/// If not set, defaults to ~/.foc-devnet
/// If not set or empty, defaults to ~/.foc-devnet
/// Supports tilde expansion for paths like ~/my-foc-devnet
pub fn foc_devnet_home() -> PathBuf {
if let Ok(base_dir) = std::env::var("FOC_DEVNET_BASEDIR") {
PathBuf::from(shellexpand::tilde(&base_dir).as_ref())
} else {
let default_path = || {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("/tmp"))
.join(".foc-devnet")
};

if let Ok(base_dir) = std::env::var("FOC_DEVNET_BASEDIR") {
if !base_dir.trim().is_empty() {
PathBuf::from(shellexpand::tilde(&base_dir).as_ref())
} else {
default_path()
}
} else {
default_path()
}
}

Expand Down Expand Up @@ -251,3 +259,60 @@ pub fn project_root() -> Result<PathBuf, std::io::Error> {
// Constants for container paths
/// Container path where Filecoin proof parameters are mounted
pub const CONTAINER_FILECOIN_PROOF_PARAMS_PATH: &str = "/var/tmp/filecoin-proof-parameters";

#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::env;

#[test]
#[serial]
fn test_foc_devnet_home_with_valid_path() {
env::set_var("FOC_DEVNET_BASEDIR", "/custom/path");
let path = foc_devnet_home();
assert_eq!(path, PathBuf::from("/custom/path"));
env::remove_var("FOC_DEVNET_BASEDIR");
}

#[test]
#[serial]
fn test_foc_devnet_home_with_empty_string() {
env::set_var("FOC_DEVNET_BASEDIR", "");
let path = foc_devnet_home();
// Should fall back to default, which includes ".foc-devnet"
assert!(path.to_string_lossy().contains(".foc-devnet"));
env::remove_var("FOC_DEVNET_BASEDIR");
}

#[test]
#[serial]
fn test_foc_devnet_home_with_whitespace() {
env::set_var("FOC_DEVNET_BASEDIR", " ");
let path = foc_devnet_home();
// Should fall back to default, which includes ".foc-devnet"
assert!(path.to_string_lossy().contains(".foc-devnet"));
env::remove_var("FOC_DEVNET_BASEDIR");
}

#[test]
#[serial]
fn test_foc_devnet_home_unset() {
env::remove_var("FOC_DEVNET_BASEDIR");
let path = foc_devnet_home();
// Should use default, which includes ".foc-devnet"
assert!(path.to_string_lossy().contains(".foc-devnet"));
}

#[test]
#[serial]
fn test_foc_devnet_home_with_tilde() {
env::set_var("FOC_DEVNET_BASEDIR", "~/my-custom-foc");
let path = foc_devnet_home();
// Tilde should be expanded, so shouldn't start with ~
assert!(!path.to_string_lossy().starts_with("~"));
// Should contain the custom directory name
assert!(path.to_string_lossy().contains("my-custom-foc"));
env::remove_var("FOC_DEVNET_BASEDIR");
}
}