# Testing ENACT_* Environment Variable Support
This guide shows you how to test the environment variable functionality in `enact-config`.
## Quick Test Methods
### 1. Run Unit Tests
The simplest way to test is to run the existing unit tests:
```bash
# Run all keychain tests
cargo test -p enact-config keychain::tests --lib
# Run specific test
cargo test -p enact-config keychain::tests::test_get_from_env_var --lib -- --nocapture
```
### 2. Test with Environment Variables
Set environment variables and run tests:
```bash
# Test with inline environment variable
ENACT_PROVIDERS_AZURE_APIKEY=test-key-123 \
cargo test -p enact-config keychain::tests::test_get_from_env_var --lib -- --nocapture
```
### 3. Use the Example Program
Run the example program to see it in action:
```bash
# Without environment variables (shows fallback behavior)
cargo run --example test-env-vars
# With environment variables
ENACT_PROVIDERS_AZURE_APIKEY=my-azure-key \
ENACT_PROVIDERS_ANTHROPIC_APIKEY=my-anthropic-key \
ENACT_PROVIDERS_OPENAI_APIKEY=my-openai-key \
cargo run --example test-env-vars
```
### 4. Use the Test Script
Run the provided test script:
```bash
cd crates/enact-config
./test-env-vars.sh
```
## Manual Testing
### Test 1: Environment Variable Takes Precedence
```bash
# Set an environment variable
export ENACT_PROVIDERS_AZURE_APIKEY=from-env-123
# In Rust code or REPL:
use enact_config::ConfigManager;
let manager = ConfigManager::new("/tmp/test_config.encrypted").await?;
let value = manager.get_secret("providers.azure.apiKey").await?;
// Should return Some("from-env-123")
```
### Test 2: Fallback to Keychain
```bash
# Don't set the environment variable
unset ENACT_PROVIDERS_AZURE_APIKEY
# Set in keychain
let manager = ConfigManager::new("/tmp/test_config.encrypted").await?;
manager.set_secret("providers.azure.apiKey", "from-keychain").await?;
let value = manager.get_secret("providers.azure.apiKey").await?;
// Should return Some("from-keychain")
```
### Test 3: Test Key Conversion
The key `providers.azure.apiKey` should map to `ENACT_PROVIDERS_AZURE_APIKEY`:
```bash
# Test the conversion
export ENACT_PROVIDERS_AZURE_APIKEY=test-value
cargo test -p enact-config keychain::tests::test_key_to_env_var_name --lib
```
## Testing Different Sources
### System-wide Environment Variables
```bash
# Set system-wide (requires admin on some systems)
sudo export ENACT_PROVIDERS_AZURE_APIKEY=system-wide-key
# Or add to /etc/environment (Linux) or ~/.zshrc/~/.bashrc
echo 'export ENACT_PROVIDERS_AZURE_APIKEY=my-key' >> ~/.zshrc
source ~/.zshrc
```
### Shell Session Variables
```bash
# Set for current session
export ENACT_PROVIDERS_AZURE_APIKEY=session-key
# Verify
echo $ENACT_PROVIDERS_AZURE_APIKEY
```
### .env File (with dotenvy)
```bash
# Create .env file
cat > .env << EOF
ENACT_PROVIDERS_AZURE_APIKEY=dotenv-key
ENACT_PROVIDERS_ANTHROPIC_APIKEY=anthropic-key
EOF
# In your Rust code, load it:
dotenvy::dotenv().ok();
```
## Expected Behavior
1. **Environment variables are checked first** - Any `ENACT_*` env var takes precedence
2. **Fallback to keychain** - If env var not found, uses OS keychain
3. **Mock keychain in tests** - Tests use in-memory mock, but env vars still take precedence
4. **All global env vars work** - System-wide, user-level, session, and .env-loaded vars all work
## Troubleshooting
### Environment variable not being picked up?
1. Check the variable name matches exactly (uppercase, underscores):
```bash
echo $ENACT_PROVIDERS_AZURE_APIKEY
```
2. Verify it's in the process environment:
```bash
env | grep ENACT
```
3. Make sure you're not in a subshell that doesn't have the variable
### Test failing?
1. Clean up any leftover environment variables:
```bash
unset ENACT_PROVIDERS_AZURE_APIKEY
```
2. Run tests in isolation:
```bash
cargo test --lib -- --test-threads=1
```