diff --git a/src/config.rs b/src/config.rs index 9ee5cacde..8cf1a5e4f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -916,6 +916,12 @@ impl Config { } } + #[cfg(test)] + pub fn path>(p: P) -> PathBuf { + tests::CONFIG_DIR.join(p) + } + + #[cfg(not(test))] pub fn path>(p: P) -> PathBuf { #[cfg(any(target_os = "android", target_os = "ios"))] { @@ -3039,6 +3045,14 @@ impl Status { mod tests { use super::{permanent_password::PERMANENT_PASSWORD_ENC_VERSION, *}; + lazy_static::lazy_static! { + pub(super) static ref CONFIG_DIR: PathBuf = { + let path = std::env::temp_dir().join(format!("hbb-common-test-{}", uuid::Uuid::new_v4())); + fs::create_dir(&path).expect("create isolated test config directory"); + path + }; + } + static CONFIG_STATE_TEST_LOCK: Mutex<()> = Mutex::new(()); struct ConfigStateTestGuard { @@ -3104,6 +3118,34 @@ mod tests { test() } + #[test] + fn test_config_files_use_isolated_temporary_paths() { + let root = Config::path(""); + assert!(root.starts_with(std::env::temp_dir())); + assert_ne!(root, std::env::temp_dir()); + assert_eq!( + std::thread::spawn(|| Config::path("")).join().unwrap(), + root + ); + for path in [ + Config::file(), + Config2::file(), + Config::file_("_local"), + Config::file_("_default"), + PeerConfig::path("isolated-peer"), + ] { + assert!(path.starts_with(&root)); + } + + let config = Config { + id: "isolated-test-id".to_owned(), + ..Default::default() + }; + Config::store_(&config, "_isolation_test"); + assert_eq!(Config::load_::("_isolation_test"), config); + fs::remove_file(Config::file_("_isolation_test")).unwrap(); + } + #[test] fn test_serialize() { let cfg: Config = Default::default();