From e2a21afca8d4c053507d39dacf0902d1d846c633 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Thu, 28 Jul 2022 16:35:55 +0100 Subject: [PATCH] maintain quotes for arguments (#6161) --- crates/nu-command/src/system/run_external.rs | 43 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 6e134cf530..cfc47da660 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -531,12 +531,12 @@ fn trim_enclosing_quotes(input: &str) -> (String, bool) { fn remove_quotes(input: String) -> String { let mut chars = input.chars(); - match chars.next_back() { - Some('"') => chars + match (chars.next_back(), input.contains('=')) { + (Some('"'), true) => chars .collect::() .replacen('"', "", 1) .replace(r#"\""#, "\""), - Some('\'') => chars.collect::().replacen('\'', "", 1), + (Some('\''), true) => chars.collect::().replacen('\'', "", 1), _ => input, } } @@ -586,3 +586,40 @@ impl Iterator for ValueReceiver { } } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn remove_quotes_argument_with_equal_test() { + let input = r#"--file="my_file.txt""#.into(); + let res = remove_quotes(input); + + assert_eq!("--file=my_file.txt", res) + } + + #[test] + fn argument_without_equal_test() { + let input = r#"--file "my_file.txt""#.into(); + let res = remove_quotes(input); + + assert_eq!(r#"--file "my_file.txt""#, res) + } + + #[test] + fn remove_quotes_argument_with_single_quotes_test() { + let input = r#"--file='my_file.txt'"#.into(); + let res = remove_quotes(input); + + assert_eq!("--file=my_file.txt", res) + } + + #[test] + fn argument_with_inner_quotes_test() { + let input = r#"bash -c 'echo a'"#.into(); + let res = remove_quotes(input); + + assert_eq!("bash -c 'echo a'", res) + } +}