97 lines
2.7 KiB
Rust
97 lines
2.7 KiB
Rust
#[cfg(test)]
|
|
mod env_parser_tests {
|
|
use env_guard::env_parser::{EnvFile, EnvEntry, parse_dotenv, extract_key_value};
|
|
|
|
#[test]
|
|
fn test_parse_simple_env() {
|
|
let content = r#"
|
|
DATABASE_URL=postgresql://user:pass@localhost:5432/db
|
|
SECRET_KEY=mysecret123
|
|
DEBUG=true
|
|
"#;
|
|
let env_file = EnvFile::parse(content).unwrap();
|
|
assert_eq!(env_file.len(), 3);
|
|
assert!(env_file.entries.contains_key("DATABASE_URL"));
|
|
assert!(env_file.entries.contains_key("SECRET_KEY"));
|
|
assert!(env_file.entries.contains_key("DEBUG"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_quoted_values() {
|
|
let content = r#"
|
|
API_KEY="my-secret-key-with-spaces"
|
|
DATABASE_URL='postgresql://user:pass@localhost:5432/db'
|
|
"#;
|
|
let env_file = EnvFile::parse(content).unwrap();
|
|
assert_eq!(env_file.len(), 2);
|
|
|
|
let api_key = env_file.entries.get("API_KEY").unwrap();
|
|
assert_eq!(api_key.value, "\"my-secret-key-with-spaces\"");
|
|
assert!(api_key.is_quoted);
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_comments() {
|
|
let content = r#"
|
|
# This is a comment
|
|
DATABASE_URL=postgresql://localhost:5432/db
|
|
# Another comment
|
|
SECRET_KEY=secret
|
|
"#;
|
|
let env_file = EnvFile::parse(content).unwrap();
|
|
assert_eq!(env_file.len(), 2);
|
|
assert_eq!(env_file.comments.len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_empty_lines() {
|
|
let content = r#"
|
|
DATABASE_URL=postgres://localhost/db
|
|
|
|
|
|
SECRET_KEY=secret
|
|
|
|
|
|
DEBUG=true
|
|
"#;
|
|
let env_file = EnvFile::parse(content).unwrap();
|
|
assert_eq!(env_file.len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn test_extract_key_value() {
|
|
let line = "DATABASE_URL=postgresql://localhost:5432/db";
|
|
let (key, value) = extract_key_value(line).unwrap();
|
|
assert_eq!(key, "DATABASE_URL");
|
|
assert_eq!(value, "postgresql://localhost:5432/db");
|
|
}
|
|
|
|
#[test]
|
|
fn test_unquoted_value() {
|
|
let entry = EnvEntry::new("TEST".to_string(), "\"quoted value\"".to_string(), 1);
|
|
assert_eq!(entry.unquoted_value(), "quoted value");
|
|
|
|
let unquoted = EnvEntry::new("TEST2".to_string(), "plain value".to_string(), 2);
|
|
assert_eq!(unquoted.unquoted_value(), "plain value");
|
|
}
|
|
|
|
#[test]
|
|
fn test_special_characters_in_value() {
|
|
let content = r#"SPECIAL=value_with_underscores-and-hyphens"#;
|
|
let env_file = EnvFile::parse(content).unwrap();
|
|
assert!(env_file.entries.contains_key("SPECIAL"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_numeric_values() {
|
|
let content = r#"
|
|
PORT=3000
|
|
TIMEOUT=60
|
|
RATIO=3.14
|
|
"#;
|
|
let env_file = EnvFile::parse(content).unwrap();
|
|
assert_eq!(env_file.len(), 3);
|
|
assert_eq!(env_file.entries.get("PORT").unwrap().value, "3000");
|
|
}
|
|
}
|