1use std::collections::HashMap;
4use std::path::PathBuf;
5
6#[cfg(windows)]
7const PATH_SEPARATOR: char = ';';
8#[cfg(not(windows))]
9const PATH_SEPARATOR: char = ':';
10
11pub fn setup_environment(include_paths: &[PathBuf]) -> HashMap<String, String> {
13 let mut env = HashMap::new();
14
15 if !include_paths.is_empty() {
16 let perl5lib = include_paths
17 .iter()
18 .map(|p| p.to_string_lossy().to_string())
19 .collect::<Vec<_>>()
20 .join(&PATH_SEPARATOR.to_string());
21
22 env.insert("PERL5LIB".to_string(), perl5lib);
23 }
24
25 env
26}
27
28pub fn format_command_args(args: &[String]) -> Vec<String> {
30 args.iter()
31 .map(|arg| {
32 if arg.contains(' ') {
33 #[cfg(windows)]
34 {
35 format!("\"{}\"", arg.replace('"', "\\\""))
36 }
37 #[cfg(not(windows))]
38 {
39 if arg.contains('\'') {
40 format!("\"{}\"", arg.replace('"', "\\\""))
41 } else {
42 format!("'{}'", arg)
43 }
44 }
45 } else {
46 arg.clone()
47 }
48 })
49 .collect()
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_setup_environment_empty() {
58 let env = setup_environment(&[]);
59 assert!(!env.contains_key("PERL5LIB"));
60 }
61
62 #[test]
63 fn test_setup_environment_with_paths() {
64 let env =
65 setup_environment(&[PathBuf::from("/workspace/lib"), PathBuf::from("/custom/lib")]);
66 assert!(env.contains_key("PERL5LIB"));
67 }
68
69 #[test]
70 fn test_format_command_args_with_spaces() {
71 let args = vec!["file with spaces.txt".to_string()];
72 let formatted = format_command_args(&args);
73 assert_eq!(formatted.len(), 1);
74 assert!(formatted[0].contains("file with spaces.txt"));
75 }
76}