test: isolate unit-test configuration paths

This commit is contained in:
rustdesk committed 2026-11-14 19:55:04 +08:00
1 parent bb416bc94e
commit f7842eb3e8
1 file changed
+42
+42
View File
@@ -916,6 +916,12 @@ impl Config {
}
}
#[cfg(test)]
pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
tests::CONFIG_DIR.join(p)
}
#[cfg(not(test))]
pub fn path<P: AsRef<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_::<Config>("_isolation_test"), config);
fs::remove_file(Config::file_("_isolation_test")).unwrap();
}
#[test]
fn test_serialize() {
let cfg: Config = Default::default();