diff --git a/src/config.rs b/src/config.rs index 7a93ba07f..1cd032ceb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3274,6 +3274,11 @@ mod tests { original_hard_settings: HashMap, } + struct ConfigFileRestoreGuard { + path: PathBuf, + original_content: Option>, + } + impl ConfigStateTestGuard { fn new(config: Config, hard_settings: HashMap) -> Self { let original_config = Config::get(); @@ -3294,6 +3299,29 @@ mod tests { } } + impl ConfigFileRestoreGuard { + fn new(path: PathBuf) -> Self { + let original_content = fs::read(&path).ok(); + Self { + path, + original_content, + } + } + } + + impl Drop for ConfigFileRestoreGuard { + fn drop(&mut self) { + if let Some(content) = &self.original_content { + if let Some(parent) = self.path.parent() { + fs::create_dir_all(parent).ok(); + } + fs::write(&self.path, content).ok(); + } else { + fs::remove_file(&self.path).ok(); + } + } + } + fn with_config_and_hard_settings( config: Config, hard_settings: HashMap, @@ -3522,6 +3550,8 @@ mod tests { #[test] fn test_config2_store_keeps_existing_unlock_pin_when_pin_is_unchanged() { + let _guard = CONFIG_STATE_TEST_LOCK.lock().unwrap(); + let _file_guard = ConfigFileRestoreGuard::new(Config::file_("2")); let pin = "123456"; let original_unlock_pin = encrypt_str_or_original(pin, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);