#[cfg(test)]
mod app_name_tests {
use confers::Config;
use serde::{Deserialize, Serialize};
use std::fs;
use tempfile::TempDir;
#[derive(Debug, Clone, Serialize, Deserialize, Config)]
pub struct NoAppNameConfig {
pub name: String,
pub port: u16,
pub debug: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Config)]
#[config(app_name = "test-app")]
pub struct WithAppNameConfig {
pub name: String,
pub port: u16,
}
#[derive(Debug, Clone, Serialize, Deserialize, Config)]
#[allow(dead_code)]
pub struct MultiFormatConfig {
pub service: String,
pub port: u16,
pub enabled: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Config)]
#[config(env_prefix = "TEST")]
#[allow(dead_code)]
pub struct EnvOverrideConfig {
pub name: String,
pub port: u16,
}
#[test]
fn test_load_file_ignores_app_name() {
let temp_dir = TempDir::new().unwrap();
let custom_path = temp_dir.path().join("custom.toml");
let config_content = r#"name = "custom-path"
port = 7777
debug = true
"#;
fs::write(&custom_path, config_content).unwrap();
let config = WithAppNameConfig::load_file(&custom_path)
.load_sync()
.unwrap();
assert_eq!(config.name, "custom-path");
assert_eq!(config.port, 7777);
}
#[test]
fn test_load_sync_equivalent() {
let temp_dir = TempDir::new().unwrap();
let _original_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(temp_dir.path()).unwrap();
let config_content = r#"name = "sync-test"
port = 4444
debug = true
"#;
fs::write("config.toml", config_content).unwrap();
let async_config = NoAppNameConfig::load().unwrap();
let sync_config = NoAppNameConfig::load_sync().unwrap();
assert_eq!(async_config.name, sync_config.name);
assert_eq!(async_config.port, sync_config.port);
assert_eq!(async_config.debug, sync_config.debug);
std::env::set_current_dir(&_original_dir).unwrap();
}
}