fix(bash): escape interpretable characters (#2404)

* fix(bash): escape interpretable characters

* also escape backticks
This commit is contained in:
David Knaack 2021-03-05 08:47:07 +01:00 committed by GitHub
parent dc8fe1bb6c
commit 7385dc27a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -207,7 +207,15 @@ CMake suite maintained and supported by Kitware (kitware.com/cmake).\n",
}
/// Wraps ANSI color escape sequences in the shell-appropriate wrappers.
pub fn wrap_colorseq_for_shell(ansi: String, shell: Shell) -> String {
pub fn wrap_colorseq_for_shell(mut ansi: String, shell: Shell) -> String {
// Bash might interepret baskslashes, backticks and $
// see #658 for more details
if shell == Shell::Bash {
ansi = ansi.replace('\\', r"\\");
ansi = ansi.replace('$', r"\$");
ansi = ansi.replace('`', r"\`");
}
const ESCAPE_BEGIN: char = '\u{1b}';
const ESCAPE_END: char = 'm';
wrap_seq_for_shell(ansi, shell, ESCAPE_BEGIN, ESCAPE_END)
@ -465,4 +473,37 @@ mod tests {
assert_eq!(&bresult4, "herpaderp");
assert_eq!(&bresult5, "");
}
#[test]
fn test_bash_escape() {
let test = "$(echo a)";
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::Bash),
r"\$(echo a)"
);
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell),
test
);
let test = r"\$(echo a)";
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::Bash),
r"\\\$(echo a)"
);
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell),
test
);
let test = r"`echo a`";
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::Bash),
r"\`echo a\`"
);
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell),
test
);
}
}