mirror of
https://github.com/watchexec/watchexec.git
synced 2024-09-28 22:21:33 +02:00
Also wrap when there’s quotes but no whitespace
This commit is contained in:
parent
21d4080183
commit
7375db5ce9
100
src/process.rs
100
src/process.rs
@ -8,10 +8,9 @@ pub fn spawn(cmd: &Vec<String>, updated_paths: Vec<PathOp>, no_shell: bool) -> P
|
|||||||
|
|
||||||
pub use self::imp::Process;
|
pub use self::imp::Process;
|
||||||
|
|
||||||
fn has_whitespace(s: &String) -> bool {
|
fn needs_wrapping(s: &String) -> bool {
|
||||||
s.contains(|ch| match ch {
|
s.contains(|ch| match ch {
|
||||||
' ' => true,
|
' ' | '\t' | '\'' | '"' => true,
|
||||||
'\t' => true,
|
|
||||||
_ => false
|
_ => false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -36,7 +35,7 @@ fn wrap_in_quotes(s: &String) -> String {
|
|||||||
|
|
||||||
fn wrap_commands(cmd: &Vec<String>) -> Vec<String> {
|
fn wrap_commands(cmd: &Vec<String>) -> Vec<String> {
|
||||||
cmd.iter().map(|fragment| {
|
cmd.iter().map(|fragment| {
|
||||||
if has_whitespace(fragment) {
|
if needs_wrapping(fragment) {
|
||||||
wrap_in_quotes(fragment)
|
wrap_in_quotes(fragment)
|
||||||
} else {
|
} else {
|
||||||
fragment.clone()
|
fragment.clone()
|
||||||
@ -426,12 +425,53 @@ mod tests {
|
|||||||
use super::spawn;
|
use super::spawn;
|
||||||
use super::get_longest_common_path;
|
use super::get_longest_common_path;
|
||||||
use super::collect_path_env_vars;
|
use super::collect_path_env_vars;
|
||||||
|
use super::wrap_commands;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_start() {
|
fn test_start() {
|
||||||
let _ = spawn(&vec!["echo".into(), "hi".into()], vec![], true);
|
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<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<String>
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
wrap_commands(&vec!["echo".into(), "hello'world".into()]),
|
||||||
|
vec!["echo".into(), "'hello'\"'\"'world'".into()] as Vec<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<String>
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
wrap_commands(&vec!["echo".into(), "hello\"world".into()]),
|
||||||
|
vec!["echo".into(), "'hello\"world'".into()] as Vec<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn longest_common_path_should_return_correct_value() {
|
fn longest_common_path_should_return_correct_value() {
|
||||||
let single_path = vec![PathBuf::from("/tmp/random/")];
|
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<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<String>
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
wrap_commands(&vec!["echo".into(), "hello'world".into()]),
|
||||||
|
vec!["echo".into(), "\"hello'world\"".into()] as Vec<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<String>
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
wrap_commands(&vec!["echo".into(), "hello\"world".into()]),
|
||||||
|
vec!["echo".into(), "\"hello\"\"world\"".into()] as Vec<String>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user