|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +/// Escape a value for safe inclusion in a POSIX single-quoted string. |
| 4 | +/// |
| 5 | +/// Replaces each `'` with `'\''` (end quote, escaped literal quote, start new quote) |
| 6 | +/// and wraps the result in single quotes. POSIX single-quoted strings treat all |
| 7 | +/// characters as literal, so this handles `$`, backticks, newlines, etc. |
| 8 | +fn shell_escape_value(value: &str) -> String { |
| 9 | + let mut escaped = String::with_capacity(value.len() + 2); |
| 10 | + escaped.push('\''); |
| 11 | + for ch in value.chars() { |
| 12 | + if ch == '\'' { |
| 13 | + escaped.push_str("'\\''"); |
| 14 | + } else { |
| 15 | + escaped.push(ch); |
| 16 | + } |
| 17 | + } |
| 18 | + escaped.push('\''); |
| 19 | + escaped |
| 20 | +} |
| 21 | + |
| 22 | +/// Build `export K='V'\n` lines for all env vars. |
| 23 | +/// |
| 24 | +/// Returns an empty string if the map is empty. |
| 25 | +pub fn build_env_exports(env_vars: &HashMap<String, String>) -> String { |
| 26 | + if env_vars.is_empty() { |
| 27 | + return String::new(); |
| 28 | + } |
| 29 | + |
| 30 | + let mut out = String::new(); |
| 31 | + for (key, value) in env_vars { |
| 32 | + out.push_str("export "); |
| 33 | + out.push_str(key); |
| 34 | + out.push('='); |
| 35 | + out.push_str(&shell_escape_value(value)); |
| 36 | + out.push('\n'); |
| 37 | + } |
| 38 | + out |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod tests { |
| 43 | + use super::*; |
| 44 | + |
| 45 | + #[test] |
| 46 | + fn test_shell_escape_simple_value() { |
| 47 | + assert_eq!(shell_escape_value("hello"), "'hello'"); |
| 48 | + } |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn test_shell_escape_empty_value() { |
| 52 | + assert_eq!(shell_escape_value(""), "''"); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_shell_escape_value_with_single_quotes() { |
| 57 | + assert_eq!(shell_escape_value("it's"), "'it'\\''s'"); |
| 58 | + } |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_shell_escape_value_with_special_chars() { |
| 62 | + assert_eq!(shell_escape_value("$HOME"), "'$HOME'"); |
| 63 | + assert_eq!(shell_escape_value("`cmd`"), "'`cmd`'"); |
| 64 | + assert_eq!(shell_escape_value("a\nb"), "'a\nb'"); |
| 65 | + assert_eq!(shell_escape_value("a b"), "'a b'"); |
| 66 | + assert_eq!(shell_escape_value("a\"b"), "'a\"b'"); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_build_env_exports_empty_map() { |
| 71 | + let map = HashMap::new(); |
| 72 | + assert_eq!(build_env_exports(&map), ""); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_build_env_exports_single_var() { |
| 77 | + let mut map = HashMap::new(); |
| 78 | + map.insert("FOO".to_string(), "bar".to_string()); |
| 79 | + assert_eq!(build_env_exports(&map), "export FOO='bar'\n"); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_build_env_exports_special_value() { |
| 84 | + let mut map = HashMap::new(); |
| 85 | + map.insert("VAR".to_string(), "it's $complex".to_string()); |
| 86 | + assert_eq!(build_env_exports(&map), "export VAR='it'\\''s $complex'\n"); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_build_env_exports_multiple_vars() { |
| 91 | + let mut map = HashMap::new(); |
| 92 | + map.insert("A".to_string(), "1".to_string()); |
| 93 | + map.insert("B".to_string(), "2".to_string()); |
| 94 | + |
| 95 | + let result = build_env_exports(&map); |
| 96 | + // HashMap order is not guaranteed, so check both lines are present |
| 97 | + assert!(result.contains("export A='1'\n")); |
| 98 | + assert!(result.contains("export B='2'\n")); |
| 99 | + // Should have exactly 2 lines |
| 100 | + assert_eq!(result.lines().count(), 2); |
| 101 | + } |
| 102 | +} |
0 commit comments