From 7375db5ce9f2713a21cd1a1734fa830b51012f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Wed, 22 Aug 2018 08:17:36 +1200 Subject: [PATCH] =?UTF-8?q?Also=20wrap=20when=20there=E2=80=99s=20quotes?= =?UTF-8?q?=20but=20no=20whitespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/process.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/src/process.rs b/src/process.rs index 3096f3e..36a9978 100644 --- a/src/process.rs +++ b/src/process.rs @@ -8,10 +8,9 @@ pub fn spawn(cmd: &Vec, updated_paths: Vec, no_shell: bool) -> P pub use self::imp::Process; -fn has_whitespace(s: &String) -> bool { +fn needs_wrapping(s: &String) -> bool { s.contains(|ch| match ch { - ' ' => true, - '\t' => true, + ' ' | '\t' | '\'' | '"' => true, _ => false }) } @@ -36,7 +35,7 @@ fn wrap_in_quotes(s: &String) -> String { fn wrap_commands(cmd: &Vec) -> Vec { cmd.iter().map(|fragment| { - if has_whitespace(fragment) { + if needs_wrapping(fragment) { wrap_in_quotes(fragment) } else { fragment.clone() @@ -426,12 +425,53 @@ mod tests { use super::spawn; use super::get_longest_common_path; use super::collect_path_env_vars; + use super::wrap_commands; #[test] fn test_start() { let _ = spawn(&vec!["echo".into(), "hi".into()], vec![], true); } + #[test] + fn wrap_commands_that_have_whitespace() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello world".into()]), + vec!["echo".into(), "'hello world'".into()] as Vec + ); + } + + #[test] + fn wrap_commands_that_have_long_whitespace() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello world".into()]), + vec!["echo".into(), "'hello world'".into()] as Vec + ); + } + + #[test] + fn wrap_commands_that_have_single_quotes() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello ' world".into()]), + vec!["echo".into(), "'hello '\"'\"' world'".into()] as Vec + ); + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello'world".into()]), + vec!["echo".into(), "'hello'\"'\"'world'".into()] as Vec + ); + } + + #[test] + fn wrap_commands_that_have_double_quotes() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello \" world".into()]), + vec!["echo".into(), "'hello \" world'".into()] as Vec + ); + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello\"world".into()]), + vec!["echo".into(), "'hello\"world'".into()] as Vec + ); + } + #[test] fn longest_common_path_should_return_correct_value() { let single_path = vec![PathBuf::from("/tmp/random/")]; @@ -477,3 +517,55 @@ mod tests { } } +#[cfg(test)] +#[cfg(target_family = "windows")] +mod tests { + use super::spawn; + use super::wrap_commands; + + #[test] + fn test_start() { + let _ = spawn(&vec!["echo".into(), "hi".into()], vec![], true); + } + + #[test] + fn wrap_commands_that_have_whitespace() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello world".into()]), + vec!["echo".into(), "\"hello world\"".into()] as Vec + ); + } + + #[test] + fn wrap_commands_that_have_long_whitespace() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello world".into()]), + vec!["echo".into(), "\"hello world\"".into()] as Vec + ); + } + + #[test] + fn wrap_commands_that_have_single_quotes() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello ' world".into()]), + vec!["echo".into(), "\"hello ' world\"".into()] as Vec + ); + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello'world".into()]), + vec!["echo".into(), "\"hello'world\"".into()] as Vec + ); + } + + #[test] + fn wrap_commands_that_have_double_quotes() { + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello \" world".into()]), + vec!["echo".into(), "\"hello \"\" world\"".into()] as Vec + ); + assert_eq!( + wrap_commands(&vec!["echo".into(), "hello\"world".into()]), + vec!["echo".into(), "\"hello\"\"world\"".into()] as Vec + ); + } +} +