Skip to content

Commit

Permalink
fix: ensure config_from_env test is isolated and reliable
Browse files Browse the repository at this point in the history
- Added explicit cleanup of pre-existing environment variables
- Clear default config data in from_env method
- Added more explicit comments about behavior
- Made test environment more isolated

This should fix the CI/CD test failures where the environment
config test was failing with unexpected values.
  • Loading branch information
jamesbrink committed Feb 9, 2025
1 parent 2d825fb commit 56d9e3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Config {
pub fn from_env() -> Result<Self> {
let mut config = Self::default();

// Create a clean environment-only config
if let Ok(api_key) = env::var("STRAINER_API_KEY") {
config.api.api_key = Some(api_key);
}
Expand All @@ -200,6 +201,9 @@ impl Config {
config.limits.tokens_per_minute = Some(tpm.parse()?);
}

// Clear any pre-existing provider-specific data that might have come from default
config.api.provider_specific.clear();

Ok(config)
}

Expand Down
15 changes: 13 additions & 2 deletions tests/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,18 @@ impl Drop for EnvGuard {

#[test]
fn test_config_from_env() -> Result<()> {
// Set test environment variables first
// First, clear any existing environment variables
for var in &[
"STRAINER_API_KEY",
"STRAINER_PROVIDER",
"STRAINER_BASE_URL",
"STRAINER_REQUESTS_PER_MINUTE",
"STRAINER_TOKENS_PER_MINUTE",
] {
env::remove_var(var);
}

// Set test environment variables
env::set_var("STRAINER_API_KEY", "env-key");
env::set_var("STRAINER_PROVIDER", "anthropic");
env::set_var("STRAINER_BASE_URL", "https://env.api.com");
Expand All @@ -94,7 +105,7 @@ fn test_config_from_env() -> Result<()> {
"STRAINER_TOKENS_PER_MINUTE",
]);

// Create config from environment
// Create a fresh config from environment
let config = Config::from_env()?;

// Verify the config values directly
Expand Down

0 comments on commit 56d9e3f

Please sign in to comment.