Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

make configuration tests parallel-safe

Raine Virta9df4b74ee003
diff --git a/src/config.rs b/src/config.rs
index 4aa7e1d..f11a1f9 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -124,9 +124,28 @@ fn read_file_config(config_dir: &Path) -> Option<FileConfig> {
}
pub fn load_config() -> LoadedConfig {
- let config_dir = paths::config_dir();
+ let env = paths::DirResolverEnv::default();
+ let config_dir = paths::resolve_config_dir(&env);
+ load_config_from_env(&env.env, config_dir)
+}
+
+pub fn load_config_for_env(env: &HashMap<String, String>) -> LoadedConfig {
+ let home = env
+ .get("HOME")
+ .or_else(|| env.get("USERPROFILE"))
+ .cloned()
+ .unwrap_or_else(|| "/".to_string());
+ let resolver_env = paths::DirResolverEnv {
+ platform: std::env::consts::OS.to_string(),
+ env: env.clone(),
+ home,
+ };
+ let config_dir = paths::resolve_config_dir(&resolver_env);
+ load_config_from_env(env, config_dir)
+}
+
+fn load_config_from_env(env: &HashMap<String, String>, config_dir: PathBuf) -> LoadedConfig {
let file = read_file_config(&config_dir);
- let env: HashMap<_, _> = std::env::vars().collect();
let mut out = LoadedConfig {
bind_address: "127.0.0.1".to_string(),
@@ -801,31 +820,34 @@ mod tests {
}
}
+ fn config_env(config: &tempfile::TempDir) -> HashMap<String, String> {
+ HashMap::from([(
+ "CCP_CONFIG_DIR".to_string(),
+ config.path().to_string_lossy().into_owned(),
+ )])
+ }
+
#[test]
fn bind_address_defaults_to_loopback() {
- let _guard = ENV_LOCK.lock().unwrap();
- clear_env();
let config = tempfile::TempDir::new().unwrap();
- let _config_env = EnvGuard::set("CCP_CONFIG_DIR", config.path());
+ let env = config_env(&config);
- assert_eq!(load_config().bind_address, "127.0.0.1");
+ assert_eq!(load_config_for_env(&env).bind_address, "127.0.0.1");
}
#[test]
fn bind_address_reads_config_and_env_takes_precedence() {
- let _guard = ENV_LOCK.lock().unwrap();
- clear_env();
let config = tempfile::TempDir::new().unwrap();
std::fs::write(
config.path().join("config.json"),
r#"{"bindAddress":"192.0.2.10"}"#,
)
.unwrap();
- let _config_env = EnvGuard::set("CCP_CONFIG_DIR", config.path());
+ let mut env = config_env(&config);
- assert_eq!(load_config().bind_address, "192.0.2.10");
- let _bind_env = EnvGuard::set("CCP_BIND_ADDRESS", "0.0.0.0");
- assert_eq!(load_config().bind_address, "0.0.0.0");
+ assert_eq!(load_config_for_env(&env).bind_address, "192.0.2.10");
+ env.insert("CCP_BIND_ADDRESS".to_string(), "0.0.0.0".to_string());
+ assert_eq!(load_config_for_env(&env).bind_address, "0.0.0.0");
}
struct EnvGuard {
@@ -924,31 +946,27 @@ mod tests {
#[test]
fn log_env_presence_enables_legacy_verbose_and_stderr() {
- let _guard = ENV_LOCK.lock().unwrap();
- clear_env();
let config = tempfile::TempDir::new().unwrap();
- let _config_env = EnvGuard::set("CCP_CONFIG_DIR", config.path());
- let _verbose_env = EnvGuard::set("CCP_LOG_VERBOSE", "0");
- let _stderr_env = EnvGuard::set("CCP_LOG_STDERR", "");
+ let mut env = config_env(&config);
+ env.insert("CCP_LOG_VERBOSE".to_string(), "0".to_string());
+ env.insert("CCP_LOG_STDERR".to_string(), String::new());
- let loaded = load_config();
+ let loaded = load_config_for_env(&env);
assert!(loaded.log_verbose);
assert!(loaded.log_stderr);
}
#[test]
fn log_config_values_apply_without_env() {
- let _guard = ENV_LOCK.lock().unwrap();
- clear_env();
let config = tempfile::TempDir::new().unwrap();
std::fs::write(
config.path().join("config.json"),
r#"{"log":{"verbose":true,"stderr":true}}"#,
)
.unwrap();
- let _config_env = EnvGuard::set("CCP_CONFIG_DIR", config.path());
+ let env = config_env(&config);
- let loaded = load_config();
+ let loaded = load_config_for_env(&env);
assert!(loaded.log_verbose);
assert!(loaded.log_stderr);
}
diff --git a/tests/foundation.rs b/tests/foundation.rs
index 86366e6..f4bded2 100644
--- a/tests/foundation.rs
+++ b/tests/foundation.rs
@@ -2,7 +2,7 @@ use claude_code_proxy::anthropic::{
encode_sse_event, parse_sse_events, parse_sse_events_with_stats, schema::MessagesRequest,
};
use claude_code_proxy::auth::{AuthStorage, InMemoryAuthStore};
-use claude_code_proxy::config::{AliasProvider, load_config};
+use claude_code_proxy::config::{AliasProvider, load_config_for_env};
use claude_code_proxy::logging::{create_logger, redact_value};
use claude_code_proxy::paths::{self, DirResolverEnv};
use claude_code_proxy::retry::{RETRY_INITIAL_DELAY_MS, RETRY_MAX_DELAY_MS, compute_backoff_delay};
@@ -227,41 +227,34 @@ async fn auth_store_read_write_and_logout() {
#[test]
fn config_env_precedence_and_defaults() {
- let original = env::var("PORT").ok();
- unsafe {
- env::remove_var("PORT");
- }
- let cfg = load_config();
- assert_eq!(cfg.port, 18765);
-
- unsafe {
- env::set_var("PORT", "19999");
- }
- let cfg = load_config();
- assert_eq!(cfg.port, 19999);
+ let config = TempDir::new().unwrap();
+ let mut env = HashMap::from([(
+ "CCP_CONFIG_DIR".to_string(),
+ config.path().to_string_lossy().into_owned(),
+ )]);
+ assert_eq!(load_config_for_env(&env).port, 18765);
- if let Some(v) = original {
- unsafe {
- env::set_var("PORT", v);
- }
- } else {
- unsafe {
- env::remove_var("PORT");
- }
- }
+ env.insert("PORT".to_string(), "19999".to_string());
+ assert_eq!(load_config_for_env(&env).port, 19999);
- unsafe {
- env::set_var("CCP_ALIAS_PROVIDER", "kimi");
- }
- assert!(matches!(load_config().alias_provider, AliasProvider::Kimi));
- unsafe {
- env::remove_var("CCP_ALIAS_PROVIDER");
- }
+ env.insert("CCP_ALIAS_PROVIDER".to_string(), "kimi".to_string());
+ assert!(matches!(
+ load_config_for_env(&env).alias_provider,
+ AliasProvider::Kimi
+ ));
}
#[test]
fn alias_provider_has_expected_default() {
- assert!(matches!(load_config().alias_provider, AliasProvider::Codex));
+ let config = TempDir::new().unwrap();
+ let env = HashMap::from([(
+ "CCP_CONFIG_DIR".to_string(),
+ config.path().to_string_lossy().into_owned(),
+ )]);
+ assert!(matches!(
+ load_config_for_env(&env).alias_provider,
+ AliasProvider::Codex
+ ));
}
#[test]