#![allow(non_snake_case)]
use std::time::Duration;
use serial_test::serial;
use xet_runtime::config_group;
use xet_runtime::utils::{ByteSize, EnvVarGuard};
mod example {
use super::*;
config_group!({
ref TEST_INT: usize = 42;
ref TEST_STRING: String = "default".to_string();
ref TEST_BOOL: bool = false;
ref TEST_DURATION: Duration = Duration::from_secs(60);
ref TEST_BYTE_SIZE: ByteSize = ByteSize::from("1mb");
ref TEST_RELEASE_FIXED: u64 = 100;
ref TEST_HIGH_PERF: usize = 10;
ref TEST_OPTIONAL: Option<String> = None;
});
}
#[test]
#[serial(config_env)]
fn test_basic_configuration() {
unsafe {
std::env::remove_var("HF_XET_EXAMPLE_TEST_INT");
std::env::remove_var("HF_XET_EXAMPLE_TEST_STRING");
std::env::remove_var("HF_XET_EXAMPLE_TEST_BOOL");
std::env::remove_var("HF_XET_EXAMPLE_TEST_DURATION");
std::env::remove_var("HF_XET_EXAMPLE_TEST_BYTE_SIZE");
std::env::remove_var("HF_XET_EXAMPLE_TEST_RELEASE_FIXED");
std::env::remove_var("HF_XET_EXAMPLE_TEST_OPTIONAL");
std::env::remove_var("HF_XET_EXAMPLE_TEST_HIGH_PERF");
}
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
assert_eq!(config.TEST_INT, 42);
assert_eq!(config.TEST_STRING, "default");
assert!(!config.TEST_BOOL);
assert_eq!(config.TEST_DURATION, Duration::from_secs(60));
assert_eq!(config.TEST_BYTE_SIZE.as_u64(), 1_000_000);
assert_eq!(config.TEST_RELEASE_FIXED, 100);
assert_eq!(config.TEST_HIGH_PERF, 10);
assert_eq!(config.TEST_OPTIONAL, None);
}
#[test]
#[serial(config_env)]
fn test_environment_override() {
let _guard1 = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_INT", "100");
let _guard2 = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_STRING", "override");
let _guard3 = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_BOOL", "true");
let _guard4 = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_DURATION", "2m");
let _guard5 = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_BYTE_SIZE", "2mb");
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
assert_eq!(config.TEST_INT, 100);
assert_eq!(config.TEST_STRING, "override");
assert!(config.TEST_BOOL);
assert_eq!(config.TEST_DURATION, Duration::from_secs(120));
assert_eq!(config.TEST_BYTE_SIZE.as_u64(), 2_000_000);
}
#[test]
#[serial(config_env)]
fn test_optional_value() {
unsafe {
std::env::remove_var("HF_XET_EXAMPLE_TEST_OPTIONAL");
}
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
assert_eq!(config.TEST_OPTIONAL, None);
let _guard = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_OPTIONAL", "some_value");
let mut config2 = example::ConfigValueGroup::new();
config2.apply_env_overrides();
assert_eq!(config2.TEST_OPTIONAL, Some("some_value".to_string()));
}
#[test]
#[serial(config_env)]
fn test_high_performance_mode() {
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
assert_eq!(config.TEST_HIGH_PERF, 10);
}
#[test]
#[serial(config_env)]
fn test_release_fixed_in_release() {
let _guard = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_RELEASE_FIXED", "200");
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
assert_eq!(config.TEST_RELEASE_FIXED, 200);
}
#[test]
#[serial(config_env)]
fn test_configuration_clone() {
let mut config1 = example::ConfigValueGroup::new();
config1.apply_env_overrides();
let config2 = config1.clone();
assert_eq!(config1.TEST_INT, config2.TEST_INT);
assert_eq!(config1.TEST_STRING, config2.TEST_STRING);
}
#[test]
#[serial(config_env)]
fn test_configuration_debug() {
unsafe {
std::env::remove_var("HF_XET_EXAMPLE_TEST_INT");
}
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
let debug_str = format!("{:?}", config);
assert!(debug_str.contains("TEST_INT"));
assert!(debug_str.contains("TEST_INT"));
}
#[test]
#[serial(config_env)]
fn test_invalid_env_value_falls_back_to_default() {
let _guard = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_INT", "not_a_number");
let mut config = example::ConfigValueGroup::new();
config.apply_env_overrides();
assert_eq!(config.TEST_INT, 42);
}
#[test]
#[serial(config_env)]
fn test_multiple_instances() {
unsafe {
std::env::remove_var("HF_XET_EXAMPLE_TEST_INT");
}
let mut config1 = example::ConfigValueGroup::new();
config1.apply_env_overrides();
let initial_value = config1.TEST_INT;
let _guard = EnvVarGuard::set("HF_XET_EXAMPLE_TEST_INT", "999");
let mut config2 = example::ConfigValueGroup::new();
config2.apply_env_overrides();
assert_eq!(config2.TEST_INT, 999);
drop(_guard);
unsafe {
std::env::remove_var("HF_XET_EXAMPLE_TEST_INT");
}
let mut config3 = example::ConfigValueGroup::new();
config3.apply_env_overrides();
assert_eq!(config3.TEST_INT, initial_value);
}
#[test]
fn test_config_values_name() {
assert_eq!(example::CONFIG_VALUES_NAME, "ConfigValueGroup");
}