Skip to content

Commit

Permalink
fix: correct environment variable handling in config tests
Browse files Browse the repository at this point in the history
- Fix test_config_from_env to properly validate config values instead of raw env vars
- Fix test_load_with_env_override to correctly test env var precedence over file values
- Make test values consistent across all config tests
  • Loading branch information
jamesbrink committed Feb 9, 2025
1 parent 849637e commit 3f0ee12
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions tests/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Drop for EnvGuard {
#[test]
fn test_config_from_env() -> Result<()> {
// Set test environment variables first
env::set_var("STRAINER_API_KEY", "env-test-key");
env::set_var("STRAINER_API_KEY", "env-key");
env::set_var("STRAINER_PROVIDER", "anthropic");
env::set_var("STRAINER_BASE_URL", "https://env.api.com");
env::set_var("STRAINER_REQUESTS_PER_MINUTE", "30");
Expand All @@ -97,13 +97,9 @@ fn test_config_from_env() -> Result<()> {
// Create config from environment
let config = Config::from_env()?;

// Verify each environment variable was properly read
assert_eq!(
env::var("STRAINER_API_KEY").ok(),
Some("env-test-key".to_string())
);
// Verify the config values directly
assert_eq!(config.api.api_key, Some("env-key".to_string()));
assert_eq!(config.api.provider, "anthropic");
assert_eq!(config.api.api_key, Some("env-test-key".to_string()));
assert_eq!(config.api.base_url, Some("https://env.api.com".to_string()));
assert_eq!(config.limits.requests_per_minute, Some(30));
assert_eq!(config.limits.tokens_per_minute, Some(50_000));
Expand Down Expand Up @@ -193,6 +189,7 @@ fn test_load_with_env_override() -> Result<()> {

// Set environment variables
env::set_var("STRAINER_API_KEY", "env-key");
env::set_var("STRAINER_BASE_URL", "https://env.api.com");
env::set_var("STRAINER_TOKENS_PER_MINUTE", "50000");
env::set_var("STRAINER_REQUESTS_PER_MINUTE", "60");

Expand All @@ -205,13 +202,11 @@ fn test_load_with_env_override() -> Result<()> {
// Restore original directory
env::set_current_dir(original_dir)?;

// Environment variables should override file values
assert_eq!(config.api.api_key, Some("env-key".to_string()));
assert_eq!(
config.api.base_url,
Some("https://file.api.com".to_string())
);
assert_eq!(config.api.base_url, Some("https://env.api.com".to_string()));
assert_eq!(config.limits.requests_per_minute, Some(60));
assert_eq!(config.limits.tokens_per_minute, Some(50_000));

Ok(())
}
}

0 comments on commit 3f0ee12

Please sign in to comment.