|
| 1 | +use anyhow::Result; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +/// Load environment variables from .env file if it exists. |
| 6 | +/// Returns a HashMap of key-value pairs. |
| 7 | +/// If the .env file doesn't exist, returns an empty HashMap (no error). |
| 8 | +pub fn load_env_file(dir: &Path) -> Result<HashMap<String, String>> { |
| 9 | + let env_path = dir.join(".env"); |
| 10 | + |
| 11 | + if !env_path.exists() { |
| 12 | + return Ok(HashMap::new()); |
| 13 | + } |
| 14 | + |
| 15 | + let content = std::fs::read_to_string(&env_path)?; |
| 16 | + let mut vars = HashMap::new(); |
| 17 | + |
| 18 | + for line in content.lines() { |
| 19 | + let line = line.trim(); |
| 20 | + |
| 21 | + // Skip empty lines and comments |
| 22 | + if line.is_empty() || line.starts_with('#') { |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + // Parse KEY=VALUE |
| 27 | + if let Some((key, value)) = line.split_once('=') { |
| 28 | + let key = key.trim().to_string(); |
| 29 | + let value = value.trim().to_string(); |
| 30 | + |
| 31 | + // Remove quotes from value if present |
| 32 | + let value = if (value.starts_with('"') && value.ends_with('"')) |
| 33 | + || (value.starts_with('\'') && value.ends_with('\'')) |
| 34 | + { |
| 35 | + value[1..value.len() - 1].to_string() |
| 36 | + } else { |
| 37 | + value |
| 38 | + }; |
| 39 | + |
| 40 | + vars.insert(key, value); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Ok(vars) |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use super::*; |
| 50 | + use std::fs; |
| 51 | + use tempfile::TempDir; |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn test_load_env_file_exists() { |
| 55 | + let dir = TempDir::new().unwrap(); |
| 56 | + let env_path = dir.path().join(".env"); |
| 57 | + fs::write(&env_path, "FOO=bar\nBAZ=qux").unwrap(); |
| 58 | + |
| 59 | + let vars = load_env_file(dir.path()).unwrap(); |
| 60 | + assert_eq!(vars.get("FOO"), Some(&"bar".to_string())); |
| 61 | + assert_eq!(vars.get("BAZ"), Some(&"qux".to_string())); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn test_load_env_file_missing() { |
| 66 | + let dir = TempDir::new().unwrap(); |
| 67 | + let vars = load_env_file(dir.path()).unwrap(); |
| 68 | + assert!(vars.is_empty()); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn test_load_env_with_quotes() { |
| 73 | + let dir = TempDir::new().unwrap(); |
| 74 | + let env_path = dir.path().join(".env"); |
| 75 | + fs::write(&env_path, "QUOTED=\"hello world\"\nSINGLE='test'").unwrap(); |
| 76 | + |
| 77 | + let vars = load_env_file(dir.path()).unwrap(); |
| 78 | + assert_eq!(vars.get("QUOTED"), Some(&"hello world".to_string())); |
| 79 | + assert_eq!(vars.get("SINGLE"), Some(&"test".to_string())); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_load_env_with_comments() { |
| 84 | + let dir = TempDir::new().unwrap(); |
| 85 | + let env_path = dir.path().join(".env"); |
| 86 | + fs::write(&env_path, "# Comment\nKEY=value\n# Another comment\nFOO=bar").unwrap(); |
| 87 | + |
| 88 | + let vars = load_env_file(dir.path()).unwrap(); |
| 89 | + assert_eq!(vars.len(), 2); |
| 90 | + assert_eq!(vars.get("KEY"), Some(&"value".to_string())); |
| 91 | + assert_eq!(vars.get("FOO"), Some(&"bar".to_string())); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_load_env_with_empty_lines() { |
| 96 | + let dir = TempDir::new().unwrap(); |
| 97 | + let env_path = dir.path().join(".env"); |
| 98 | + fs::write(&env_path, "KEY1=value1\n\nKEY2=value2\n\n").unwrap(); |
| 99 | + |
| 100 | + let vars = load_env_file(dir.path()).unwrap(); |
| 101 | + assert_eq!(vars.len(), 2); |
| 102 | + assert_eq!(vars.get("KEY1"), Some(&"value1".to_string())); |
| 103 | + assert_eq!(vars.get("KEY2"), Some(&"value2".to_string())); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_load_env_with_spaces() { |
| 108 | + let dir = TempDir::new().unwrap(); |
| 109 | + let env_path = dir.path().join(".env"); |
| 110 | + fs::write(&env_path, "KEY = value with spaces").unwrap(); |
| 111 | + |
| 112 | + let vars = load_env_file(dir.path()).unwrap(); |
| 113 | + assert_eq!(vars.get("KEY"), Some(&"value with spaces".to_string())); |
| 114 | + } |
| 115 | +} |
0 commit comments