From 9da896ad4e8ed9fc65dadc312b0c6322ada59201 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 8 Sep 2019 14:00:04 +1200 Subject: [PATCH] Attempt so simplify classified --- src/commands/classified.rs | 135 ++++++++++++------------------------- tests/helpers/mod.rs | 31 +++------ tests/tests.rs | 4 +- 3 files changed, 52 insertions(+), 118 deletions(-) diff --git a/src/commands/classified.rs b/src/commands/classified.rs index 959a8f6125..277a925341 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -220,111 +220,60 @@ impl ExternalCommand { let mut process; - #[cfg(windows)] - { - process = Exec::shell(&self.name); + process = Exec::cmd(&self.name); - if arg_string.contains("$it") { - let mut first = true; - - for i in &inputs { - if i.as_string().is_err() { - let mut span = None; - for arg in &self.args { - if arg.item.contains("$it") { - span = Some(arg.span()); - } - } - if let Some(span) = span { - return Err(ShellError::labeled_error( - "External $it needs string data", - "given row instead of string data", - span, - )); - } else { - return Err(ShellError::string("Error: $it needs string data")); - } - } - if !first { - process = process.arg("&&"); - process = process.arg(&self.name); - } else { - first = false; - } + if arg_string.contains("$it") { + let mut first = true; + for i in &inputs { + if i.as_string().is_err() { + let mut span = None; for arg in &self.args { - if arg.chars().all(|c| c.is_whitespace()) { - continue; + if arg.item.contains("$it") { + span = Some(arg.span()); } - - process = process.arg(&arg.replace("$it", &i.as_string()?)); + } + if let Some(span) = span { + return Err(ShellError::labeled_error( + "External $it needs string data", + "given row instead of string data", + span, + )); + } else { + return Err(ShellError::string("Error: $it needs string data")); } } - } else { + if !first { + process = process.arg("&&"); + process = process.arg(&self.name); + } else { + first = false; + } + for arg in &self.args { - let arg_chars: Vec<_> = arg.chars().collect(); - if arg_chars.len() > 1 - && arg_chars[0] == '"' - && arg_chars[arg_chars.len() - 1] == '"' - { - // quoted string - let new_arg: String = arg_chars[1..arg_chars.len() - 1].iter().collect(); - process = process.arg(new_arg); - } else { - process = process.arg(arg.item.clone()); + if arg.chars().all(|c| c.is_whitespace()) { + continue; } + + process = process.arg(&arg.replace("$it", &i.as_string()?)); + } + } + } else { + for arg in &self.args { + let arg_chars: Vec<_> = arg.chars().collect(); + if arg_chars.len() > 1 + && arg_chars[0] == '"' + && arg_chars[arg_chars.len() - 1] == '"' + { + // quoted string + let new_arg: String = arg_chars[1..arg_chars.len() - 1].iter().collect(); + process = process.arg(new_arg); + } else { + process = process.arg(arg.item.clone()); } } } - #[cfg(not(windows))] - { - let mut new_arg_string = self.name.to_string(); - if arg_string.contains("$it") { - let mut first = true; - for i in &inputs { - let i = match i.as_string() { - Err(_err) => { - let mut span = name_span; - for arg in &self.args { - if arg.item.contains("$it") { - span = arg.span(); - } - } - return Err(ShellError::labeled_error( - "External $it needs string data", - "given row instead of string data", - span, - )); - } - Ok(val) => val, - }; - - if !first { - new_arg_string.push_str("&&"); - new_arg_string.push_str(&self.name); - } else { - first = false; - } - - for arg in &self.args { - if arg.chars().all(|c| c.is_whitespace()) { - continue; - } - - new_arg_string.push_str(" "); - new_arg_string.push_str(&arg.replace("$it", &i)); - } - } - } else { - for arg in &self.args { - new_arg_string.push_str(" "); - new_arg_string.push_str(&arg); - } - } - - process = Exec::shell(new_arg_string); - } process = process.cwd(context.shell_manager.path()); let mut process = match stream_next { diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs index c27738c79a..6aa7cb67b9 100644 --- a/tests/helpers/mod.rs +++ b/tests/helpers/mod.rs @@ -310,8 +310,7 @@ pub fn file_contents(full_path: impl AsRef) -> String { pub fn file_contents_binary(full_path: impl AsRef) -> Vec { let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file"); let mut contents = Vec::new(); - file.read_to_end(&mut contents) - .expect("can not read file"); + file.read_to_end(&mut contents).expect("can not read file"); contents } @@ -327,18 +326,6 @@ pub fn line_ending() -> String { } } -pub fn normalize_string(input: &str) -> String { - #[cfg(windows)] - { - input.to_string() - } - - #[cfg(not(windows))] - { - format!("\"{}\"", input) - } -} - pub fn create_file_at(full_path: impl AsRef) -> Result<(), std::io::Error> { let full_path = full_path.as_ref(); @@ -377,13 +364,13 @@ pub fn in_directory(str: impl AsRef) -> String { str.as_ref().display().to_string() } - pub fn pipeline(commands: &str) -> String { - commands.lines() - .skip(1) - .map(|line| line.trim()) - .collect::>() - .join(" ") - .trim_end() - .to_string() + commands + .lines() + .skip(1) + .map(|line| line.trim()) + .collect::>() + .join(" ") + .trim_end() + .to_string() } diff --git a/tests/tests.rs b/tests/tests.rs index f3f810eb09..25337edb09 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -38,9 +38,7 @@ fn external_has_correct_quotes() { r#"echo "hello world""# ); - let actual = h::normalize_string(&actual); - - assert_eq!(actual, r#""hello world""#); + assert_eq!(actual, r#"hello world"#); } #[test]