Skip to main content

perl_dap_shell/
lib.rs

1//! Shell-specific helpers for Perl DAP process launch.
2
3pub use perl_dap_command_args::format_command_args;
4pub use perl_dap_platform::setup_environment;
5
6#[cfg(test)]
7mod tests {
8    use super::*;
9    use std::path::PathBuf;
10
11    // ── setup_environment ──────────────────────────────────────
12
13    #[test]
14    fn test_setup_environment_empty() {
15        let env = setup_environment(&[]);
16        assert!(!env.contains_key("PERL5LIB"));
17    }
18
19    #[test]
20    fn test_setup_environment_with_paths() {
21        let env =
22            setup_environment(&[PathBuf::from("/workspace/lib"), PathBuf::from("/custom/lib")]);
23        assert!(env.contains_key("PERL5LIB"));
24    }
25
26    #[test]
27    fn test_setup_environment_single_path_value_matches() {
28        let env = setup_environment(&[PathBuf::from("/my/lib")]);
29        assert_eq!(env.get("PERL5LIB").map(String::as_str), Some("/my/lib"));
30    }
31
32    #[test]
33    fn test_setup_environment_multiple_paths_joined_with_separator() -> Result<(), String> {
34        let env = setup_environment(&[PathBuf::from("/a"), PathBuf::from("/b")]);
35        let perl5lib = env.get("PERL5LIB").ok_or_else(|| "PERL5LIB must be set".to_string())?;
36        assert!(perl5lib.contains("/a"), "first path present in PERL5LIB");
37        assert!(perl5lib.contains("/b"), "second path present in PERL5LIB");
38        #[cfg(not(windows))]
39        assert_eq!(perl5lib.as_str(), "/a:/b");
40        #[cfg(windows)]
41        assert_eq!(perl5lib.as_str(), "/a;/b");
42        Ok(())
43    }
44
45    #[test]
46    fn test_setup_environment_only_sets_perl5lib() {
47        let env = setup_environment(&[PathBuf::from("/lib")]);
48        assert_eq!(env.len(), 1);
49        assert!(env.contains_key("PERL5LIB"));
50    }
51
52    // ── format_command_args ────────────────────────────────────
53
54    #[test]
55    fn test_format_command_args_with_spaces() {
56        let args = vec!["file with spaces.txt".to_string()];
57        let formatted = format_command_args(&args);
58        assert_eq!(formatted.len(), 1);
59        assert!(formatted[0].contains("file with spaces.txt"));
60    }
61
62    #[test]
63    fn test_format_command_args_without_spaces_passthrough() {
64        let args = vec!["simple".to_string(), "/path/to/file.pl".to_string()];
65        let formatted = format_command_args(&args);
66        assert_eq!(formatted, args, "args without spaces pass through unchanged");
67    }
68
69    #[test]
70    fn test_format_command_args_empty_returns_empty() {
71        let formatted = format_command_args(&[]);
72        assert!(formatted.is_empty());
73    }
74
75    #[test]
76    fn test_format_command_args_space_arg_is_quoted() {
77        let args = vec!["plain".to_string(), "has space".to_string()];
78        let formatted = format_command_args(&args);
79        assert_eq!(formatted.len(), 2);
80        assert_eq!(formatted[0], "plain", "no-space arg unchanged");
81        assert!(formatted[1].contains("has space"), "space arg contains original text");
82        assert_ne!(formatted[1], "has space", "space arg must be wrapped in quotes");
83    }
84
85    #[test]
86    #[cfg(not(windows))]
87    fn test_format_command_args_space_no_single_quote_uses_single_quotes() {
88        let args = vec!["arg with space".to_string()];
89        let formatted = format_command_args(&args);
90        assert_eq!(formatted[0], "'arg with space'");
91    }
92
93    #[test]
94    #[cfg(not(windows))]
95    fn test_format_command_args_space_with_single_quote_uses_double_quotes() {
96        let args = vec!["it's here".to_string()];
97        let formatted = format_command_args(&args);
98        assert!(formatted[0].starts_with('"'), "double-quoted when arg contains single quote");
99        assert!(formatted[0].ends_with('"'), "double-quoted when arg contains single quote");
100        assert!(formatted[0].contains("it's here"), "original content preserved");
101    }
102
103    #[test]
104    #[cfg(not(windows))]
105    fn test_format_command_args_inner_double_quote_escaped_in_double_quoted_form() {
106        let args = vec!["say \"hello\" it's".to_string()];
107        let formatted = format_command_args(&args);
108        assert!(formatted[0].contains(r#"\""#), "inner double quotes should be escaped as \\\" ");
109    }
110}