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)); std::fs::write(&path, "REGISTRY_PORT=1234\nREGISTRY_HOST=localhost\n").unwrap(); let config = Config::from_file( &path, HashMap::from([("REGISTRY_PORT".into(), "4321".into())]), ) .unwrap(); assert_eq!(config.port, 4321); assert_eq!(config.host, "localhost"); } #[test] fn invalid_configuration_fails() { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join(".env"); for (key, value) in [ ("REGISTRY_PORT", "0"), ("REGISTRY_PORT", "65536"), ("REGISTRY_HEALTH_INTERVAL_SECS", "0"), ("REGISTRY_HEALTH_TIMEOUT_SECS", "abc"), ("REGISTRY_HOST", " "), ("REGISTRY_DATA_PATH", ""), ] { assert!(Config::from_file(&path, HashMap::from([(key.into(), value.into())])).is_err()); } std::fs::write(&path, "REGISTRY_PORT='unterminated").unwrap(); assert!(Config::from_file(&path, HashMap::new()).is_err()); }