Skip to content
This repository has been archived by the owner on Dec 29, 2021. It is now read-only.

Introduce a glorious new cli arg splitter #29

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 28 additions & 81 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,20 @@ extern crate difference;
#[macro_use] extern crate error_chain;
extern crate rustc_serialize;

use std::process::{Command, Output};
use std::fmt;

use difference::Changeset;
use std::process::Command;

mod errors;
use errors::*;

#[macro_use] mod macros;
pub use macros::flatten_escaped_string;

mod output;
use output::{OutputAssertion, StdErr, StdOut};

mod parse_cmd;
use parse_cmd::ToCmd;

mod diff;

/// Assertions for a specific command.
Expand All @@ -127,38 +130,8 @@ pub struct Assert {
cmd: Vec<String>,
expect_success: Option<bool>,
expect_exit_code: Option<i32>,
expect_stdout: Option<OutputAssertion>,
expect_stderr: Option<OutputAssertion>,
}

#[derive(Debug)]
struct OutputAssertion {
expect: String,
fuzzy: bool,
}

#[derive(Debug, Copy, Clone)]
enum OutputType {
StdOut,
StdErr,
}

impl OutputType {
fn select<'a>(&self, o: &'a Output) -> &'a [u8] {
match *self {
OutputType::StdOut => &o.stdout,
OutputType::StdErr => &o.stderr,
}
}
}

impl fmt::Display for OutputType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OutputType::StdOut => write!(f, "stdout"),
OutputType::StdErr => write!(f, "stderr"),
}
}
expect_stdout: Option<OutputAssertion<StdOut>>,
expect_stderr: Option<OutputAssertion<StdErr>>,
}

impl std::default::Default for Assert {
Expand Down Expand Up @@ -208,9 +181,16 @@ impl Assert {
/// assert_cli::Assert::command(&["echo", "1337"])
/// .unwrap();
/// ```
pub fn command(cmd: &[&str]) -> Self {
///
/// ```rust
/// extern crate assert_cli;
///
/// assert_cli::Assert::command("echo 1337")
/// .unwrap();
/// ```
pub fn command<'a, T: ToCmd<'a> + ?Sized>(cmd: &'a T) -> Self {
Assert {
cmd: cmd.into_iter().cloned().map(String::from).collect(),
cmd: cmd.to_cmd(),
..Self::default()
}
}
Expand Down Expand Up @@ -318,6 +298,7 @@ impl Assert {
self.expect_stdout = Some(OutputAssertion {
expect: output.into(),
fuzzy: true,
kind: StdOut,
});
self
}
Expand All @@ -337,6 +318,7 @@ impl Assert {
self.expect_stdout = Some(OutputAssertion {
expect: output.into(),
fuzzy: false,
kind: StdOut,
});
self
}
Expand All @@ -358,6 +340,7 @@ impl Assert {
self.expect_stderr = Some(OutputAssertion {
expect: output.into(),
fuzzy: true,
kind: StdErr,
});
self
}
Expand All @@ -379,6 +362,7 @@ impl Assert {
self.expect_stderr = Some(OutputAssertion {
expect: output.into(),
fuzzy: false,
kind: StdErr,
});
self
}
Expand Down Expand Up @@ -421,52 +405,15 @@ impl Assert {
));
}

self.assert_output(OutputType::StdOut, &output)?;
self.assert_output(OutputType::StdErr, &output)?;

Ok(())
}

/// Perform the appropriate output assertion.
fn assert_output(&self, output_type: OutputType, output: &Output) -> Result<()> {
let observed = String::from_utf8_lossy(output_type.select(output));
match *self.expect_output(output_type) {
Some(OutputAssertion {
expect: ref expected_output,
fuzzy: true,
}) if !observed.contains(expected_output) => {
bail!(ErrorKind::OutputMismatch(
output_type.to_string(),
self.cmd.clone(),
expected_output.clone(),
observed.into(),
));
},
Some(OutputAssertion {
expect: ref expected_output,
fuzzy: false,
}) => {
let differences = Changeset::new(expected_output.trim(), observed.trim(), "\n");
if differences.distance > 0 {
let nice_diff = diff::render(&differences)?;
bail!(ErrorKind::ExactOutputMismatch(
output_type.to_string(),
self.cmd.clone(),
nice_diff
));
}
},
_ => {},
if let Some(ouput_assertion) = self.expect_stdout {
ouput_assertion.execute(&output)?;
}
Ok(())
}

/// Return a reference to the appropriate output assertion.
fn expect_output(&self, output_type: OutputType) -> &Option<OutputAssertion> {
match output_type {
OutputType::StdOut => &self.expect_stdout,
OutputType::StdErr => &self.expect_stderr,
if let Some(ouput_assertion) = self.expect_stderr {
ouput_assertion.execute(&output)?;
}

Ok(())
}

/// Execute the command, check the assertions, and panic when they fail.
Expand Down
91 changes: 91 additions & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::fmt;
use std::process::Output;

use difference::Changeset;

use errors::*;
use diff;

#[derive(Debug, Clone)]
pub struct OutputAssertion<T> {
pub expect: String,
pub fuzzy: bool,
pub kind: T,
}

impl<T: OutputType> OutputAssertion<T> {
fn matches_fuzzy(&self, got: &str) -> Result<()> {
if !got.contains(&self.expect) {
bail!(ErrorKind::OutputMismatch(
self.kind.to_string(),
vec!["Foo".to_string()],
self.expect.clone(),
got.into(),
));
}

Ok(())
}

fn matches_exact(&self, got: &str) -> Result<()> {
let differences = Changeset::new(self.expect.trim(), got.trim(), "\n");

if differences.distance > 0 {
let nice_diff = diff::render(&differences)?;
bail!(ErrorKind::ExactOutputMismatch(
self.kind.to_string(),
vec!["Foo".to_string()],
nice_diff
));
}

Ok(())
}

pub fn execute(&self, output: &Output) -> Result<()> {
let observed = String::from_utf8_lossy(self.kind.select(output));

if self.fuzzy {
self.matches_fuzzy(&observed)
} else {
self.matches_exact(&observed)
}
}
}


pub trait OutputType: fmt::Display {
fn select<'a>(&self, o: &'a Output) -> &'a [u8];
}


#[derive(Debug, Clone, Copy)]
pub struct StdOut;

impl fmt::Display for StdOut {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "stdout")
}
}

impl OutputType for StdOut {
fn select<'a>(&self, o: &'a Output) -> &'a [u8] {
&o.stdout
}
}


#[derive(Debug, Clone, Copy)]
pub struct StdErr;

impl fmt::Display for StdErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "stderr")
}
}

impl OutputType for StdErr {
fn select<'a>(&self, o: &'a Output) -> &'a [u8] {
&o.stderr
}
}
98 changes: 98 additions & 0 deletions src/parse_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pub trait ToCmd<'a> {
fn to_cmd(&'a self) -> Vec<String>;
}

impl<'a> ToCmd<'a> for str {
fn to_cmd(&'a self) -> Vec<String> {
let mut args = Vec::new();
let mut current_arg = String::new();
let mut in_quote = Vec::new();

for c in self.chars() {
if in_quote.is_empty() && c.is_whitespace() {
args.push(current_arg);
current_arg = String::new();
continue;
}

current_arg.push(c);

if c == '"' || c == '\'' {
if in_quote.last() == Some(&c) {
in_quote.pop();
} else {
in_quote.push(c);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ich glaube es gibt Randfälle, in denen sich das noch unerwartet verhalten könnte. Ich spiele mal Advocatus Diaboli:

assert_eq!(
    r#"echo lorem                           ipsum"#.to_cmd(),
    vec!["echo", "lorem", "ipsum"]
);
assert_eq!(
    r#"echo "lorem' ipsum" dolor "sit' amet" '"#.to_cmd(),
    vec!["echo", "lorem' ipsum", "dolor", "sit' amet"]
);
assert_eq!(
    r#"echo "lorem\" ipsum" dolor "sit\" amet"'"#.to_cmd(),
    vec!["echo", "lorem\" ipsum", "dolor", "sit\" amet"]
);

Bei echo ist das unkritisch, bei anderen Befehlen könnte es schon problematisch sein, wenn Argument 3 plötzlich an Position 4 steht, etc. Vielleicht kann man sich hier auch irgendein fuzzy-testing überlegen? Das macht es natürlich alles sehr sehr kompliziert. Wichtig wäre aber, dass es sich vorhersehbar verhält.

M.E. wäre folgende Invariante sehr erstrebenswert:

  • Wenn "..." ein gültiger Rust String ist (... sind Platzhalter), dann sollte stringify!("...").to_cmd()] gleich ["..."].to_cmd() sein.

Ich kenne die stringify! Methode nicht im Detail, vermute aber, dass auf einen String angewendet, JSON::from_str(stringify!("...")) weitestgehend die Identität sein sollte. Auf dieser Annahme basiert ja die aktuelle Makro-Logik. Auch hier wäre vielleicht mal eine genauere Recherche oder Fuzzy Testing sinnvoll. :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r#"echo "lorem' ipsum" dolor "sit' amet" '"#.to_cmd()

Great test case! I was so focused on getting other stuff that I think I actually went a bit overboard with this: We don't really have to deal with nested quotes at all – just recognize and ignore escaped quotes, right?

I've also just seen this crate, but I think it's more complex than we need here. I don't want to emulate shell syntax itself, just split some args :)


I think I can add quickcheck to test the invariant, but I'm not sure JSON::from_str(stringify!("...")) is that reliable. We'll see :)

}

if !current_arg.is_empty() {
args.push(current_arg);
}

args
}
}

impl<'a, 'b, T> ToCmd<'a> for T where
&'a T: AsRef<[&'b str]>,
T: 'a,
{
fn to_cmd(&'a self) -> Vec<String> {
self.as_ref().into_iter().map(|x| x.to_string()).collect()
}
}

#[cfg(test)]
mod test {
use super::ToCmd;

#[test]
fn slices() {
assert_eq!(
ToCmd::to_cmd(&["echo", "42"]),
vec!["echo", "42"]
);
}

#[test]
fn simple() {
assert_eq!(
"echo 42".to_cmd(),
vec!["echo", "42"]
);
assert_eq!(
r#"echo "42""#.to_cmd(),
vec!["echo", "\"42\""]
);
assert_eq!(
r#"echo '42'"#.to_cmd(),
vec!["echo", "\'42\'"]
);
assert_eq!(
r#"echo '42 is the answer'"#.to_cmd(),
vec!["echo", "\'42 is the answer\'"]
);
}

#[test]
fn real_world() {
assert_eq!(
r#"cargo run --bin whatever -- --input="Lorem ipsum" -f"#.to_cmd(),
vec!["cargo", "run", "--bin", "whatever", "--", "--input=\"Lorem ipsum\"", "-f"]
);
}

#[test]
fn nested_quotes() {
assert_eq!(
r#"echo "lorem ipsum 'dolor' sit amet""#.to_cmd(),
vec!["echo", "\"lorem ipsum 'dolor' sit amet\""]
);

assert_eq!(
r#"echo "lorem ipsum ('dolor "doloris" septetur') sit amet""#.to_cmd(),
vec!["echo", "\"lorem ipsum ('dolor \"doloris\" septetur') sit amet\""]
);
}
}