use std::collections::HashMap; use std::time::Duration; use crate::Config; #[test] fn defaults_file_and_environment_precedence() { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join(".env"); let config = Config::from_file(&path, HashMap::new()).unwrap(); assert_eq!(config.port, 50051); assert_eq!(config.health_interval, Duration::from_secs(10)); assert_eq!(config.log_level, "info"); assert_eq!(config.log_dir, std::path::Path::new("logs/registry")); assert_eq!(config.log_max_files, 10); std::fs::write(&path, "PORT=1234\nHOST=localhost\nLOG_LEVEL=warn\nLOG_DIR=custom-logs\nLOG_MAX_FILES=4\nDATA_FILE_COUNT=7\n").unwrap(); let config = Config::from_file( &path, HashMap::from([ ("PORT".into(), "4321".into()), ("LOG_LEVEL".into(), "DEBUG".into()), ]), ) .unwrap(); assert_eq!(config.port, 4321); assert_eq!(config.host, "localhost"); assert_eq!(config.log_level, "DEBUG"); assert_eq!(config.log_dir, std::path::Path::new("custom-logs")); assert_eq!(config.log_max_files, 4); assert_eq!(config.data_file_count, 7); } #[test] fn invalid_configuration_fails() { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join(".env"); for (key, value) in [ ("PORT", "0"), ("PORT", "65536"), ("HEALTH_INTERVAL_SECS", "0"), ("HEALTH_TIMEOUT_SECS", "abc"), ("HOST", " "), ("DATA_PATH", ""), ("LOG_LEVEL", ""), ("LOG_LEVEL", "verbose"), ("LOG_DIR", " "), ("LOG_MAX_FILES", "0"), ("LOG_MAX_FILES", "-1"), ("LOG_MAX_FILES", "18446744073709551616"), ] { assert!(Config::from_file(&path, HashMap::from([(key.into(), value.into())])).is_err()); } let config = Config::from_file(&path, HashMap::from([("LOG_LEVEL".into(), "off".into())])).unwrap(); assert_eq!(config.log_level, "off"); std::fs::write(&path, "PORT='unterminated").unwrap(); assert!(Config::from_file(&path, HashMap::new()).is_err()); }