Skip to content

Commit e0d1e79

Browse files
Copilotoleander
andcommitted
Make API key test more robust by using empty string and explicit environment cleanup
Co-authored-by: oleander <220827+oleander@users.noreply.github.com>
1 parent 88cc4b6 commit e0d1e79

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tests/api_key_error_test.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ async fn test_invalid_api_key_propagates_error() {
77
// Initialize logging to capture warnings
88
let _ = env_logger::builder().is_test(true).try_init();
99

10+
// Ensure no API key is available from environment to force early validation failure
11+
let original_key = std::env::var("OPENAI_API_KEY").ok();
12+
std::env::remove_var("OPENAI_API_KEY");
13+
1014
// Create settings with an invalid API key that fails early validation (no network calls)
1115
let settings = AppConfig {
12-
openai_api_key: Some("<PLACE HOLDER FOR YOUR API KEY>".to_string()),
16+
openai_api_key: Some("".to_string()), // Empty string triggers early validation failure
1317
model: Some("gpt-4o-mini".to_string()),
1418
max_tokens: Some(1024),
1519
max_commit_length: Some(72),
@@ -21,6 +25,11 @@ async fn test_invalid_api_key_propagates_error() {
2125
// This should fail with an API key error, not log a warning and continue
2226
let result = commit::generate(example_diff, 1024, Model::GPT41Mini, Some(&settings)).await;
2327

28+
// Restore original environment variable if it existed
29+
if let Some(key) = original_key {
30+
std::env::set_var("OPENAI_API_KEY", key);
31+
}
32+
2433
// Verify the behavior - it should return an error, not continue with other files
2534
assert!(result.is_err(), "Expected API key error to be propagated as error, not warning");
2635

0 commit comments

Comments
 (0)