pub fn shell_quote(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wraps_in_single_quotes() {
assert_eq!(shell_quote("plain"), "'plain'");
assert_eq!(shell_quote("/abs/path"), "'/abs/path'");
}
#[test]
fn neutralizes_shell_metacharacters() {
assert_eq!(
shell_quote("a b; rm -rf $HOME `id`"),
"'a b; rm -rf $HOME `id`'"
);
}
#[test]
fn closes_and_reopens_for_an_embedded_single_quote() {
assert_eq!(shell_quote("it's"), "'it'\\''s'");
assert_eq!(shell_quote("''"), "''\\'''\\'''");
}
}