maintain quotes for arguments (#6161)

This commit is contained in:
Fernando Herrera 2022-07-28 16:35:55 +01:00 committed by GitHub
parent 2ea209bcc0
commit e2a21afca8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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::<String>()
.replacen('"', "", 1)
.replace(r#"\""#, "\""),
Some('\'') => chars.collect::<String>().replacen('\'', "", 1),
(Some('\''), true) => chars.collect::<String>().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)
}
}