diff --git a/.travis.yml b/.travis.yml index 6af8429..05ac307 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,10 +8,19 @@ rust: matrix: include: - rust: nightly-2017-10-09 - env: CLIPPY=YESPLEASE + env: CLIPPY_VERS="0.0.165" + before_script: | + [[ "$(cargo +nightly-2017-10-09 clippy --version)" != "$CLIPPY_VERS" ]] && \ + cargo +nightly-2017-10-09 install clippy --vers "$CLIPPY_VERS" --force script: | - cargo +nightly-2017-10-09 install clippy --vers "0.0.165" - cargo +nightly-2017-10-09 clippy -- -D warnings + cargo +nightly-2017-10-09 clippy -- -D warnings + - rust: nightly-2017-10-09 + env: RUSTFMT_VERS="0.2.8" + before_script: | + [[ "$(cargo +nightly-2017-10-09 fmt -- --version)" != "$RUSTFMT_VERS"* ]] && \ + cargo +nightly-2017-10-09 install rustfmt-nightly --vers "$RUSTFMT_VERS" --force + script: | + cargo +nightly-2017-10-09 fmt --all -- --write-mode=diff before_script: - | pip install 'travis-cargo<0.2' --user && diff --git a/rustfmt.toml b/rustfmt.toml index 193734b..7bcfcb7 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,2 @@ format_strings = false reorder_imports = true - diff --git a/src/assert.rs b/src/assert.rs index 08566ca..e50e9bb 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -1,11 +1,10 @@ +use errors::*; +use output::{OutputAssertion, OutputKind}; use std::default; -use std::process::{Command, Stdio}; +use std::io::Write; use std::path::PathBuf; +use std::process::{Command, Stdio}; use std::vec::Vec; -use std::io::Write; - -use errors::*; -use output::{OutputAssertion, OutputKind}; /// Assertions for a specific command. #[derive(Debug)] @@ -25,7 +24,9 @@ impl default::Default for Assert { fn default() -> Self { Assert { cmd: vec!["cargo", "run", "--"] - .into_iter().map(String::from).collect(), + .into_iter() + .map(String::from) + .collect(), current_dir: None, expect_success: Some(true), expect_exit_code: None, @@ -49,7 +50,9 @@ impl Assert { pub fn cargo_binary(name: &str) -> Self { Assert { cmd: vec!["cargo", "run", "--bin", name, "--"] - .into_iter().map(String::from).collect(), + .into_iter() + .map(String::from) + .collect(), ..Self::default() } } @@ -265,7 +268,11 @@ impl Assert { let mut spawned = command.spawn()?; if let Some(ref contents) = self.stdin_contents { - spawned.stdin.as_mut().expect("Couldn't get mut ref to command stdin").write_all(contents.as_bytes())?; + spawned + .stdin + .as_mut() + .expect("Couldn't get mut ref to command stdin") + .write_all(contents.as_bytes())?; } let output = spawned.wait_with_output()?; @@ -282,8 +289,7 @@ impl Assert { } } - if self.expect_exit_code.is_some() && - self.expect_exit_code != output.status.code() { + if self.expect_exit_code.is_some() && self.expect_exit_code != output.status.code() { let out = String::from_utf8_lossy(&output.stdout).to_string(); let err = String::from_utf8_lossy(&output.stderr).to_string(); bail!(ErrorKind::ExitCodeMismatch( @@ -344,7 +350,7 @@ impl OutputAssertionBuilder { // No clippy, we don't want to implement std::ops::Not :) #[cfg_attr(feature = "cargo-clippy", allow(should_implement_trait))] pub fn not(mut self) -> Self { - self.expected_result = ! self.expected_result; + self.expected_result = !self.expected_result; self } diff --git a/src/diff.rs b/src/diff.rs index 4ec1eb9..207578a 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,8 +1,7 @@ extern crate colored; use self::colored::Colorize; - -use difference::{Difference, Changeset}; -use std::fmt::{Write, Error as fmtError}; +use difference::{Changeset, Difference}; +use std::fmt::{Error as fmtError, Write}; pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result { let mut t = String::new(); @@ -54,22 +53,35 @@ mod tests { fn basic_diff() { let diff = Changeset::new("lol", "yay", "\n"); println!("{}", render(&diff).unwrap()); - assert_eq!(render(&diff).unwrap(), - " \n\u{1b}[31m-lol\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[7;32myay\u{1b}[0m \n") + assert_eq!( + render(&diff).unwrap(), + " \n\u{1b}[31m-lol\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[7;32myay\u{1b}[0m \n" + ) } #[test] fn multiline_diff() { - let diff = Changeset::new("Lorem ipsum dolor sit amet, consectetur adipisicing elit, + let diff = Changeset::new( + "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", - "Lorem ipsum dolor sit amet, consectetur adipisicing elit, + "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor **incididunt** ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", - "\n"); + "\n", + ); println!("{}", render(&diff).unwrap()); - assert_eq!(render(&diff).unwrap(), " Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\u{1b}[31m-sed do eiusmod tempor incididunt ut labore et dolore magna\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32msed do eiusmod tempor\u{1b}[0m \u{1b}[7;32m**incididunt**\u{1b}[0m \u{1b}[32mut labore et dolore magna\u{1b}[0m \n aliqua. Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo consequat.\n"); + assert_eq!( + render(&diff).unwrap(), + " Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\ + \u{1b}[31m-sed do eiusmod tempor incididunt ut labore et dolore \ + magna\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32msed do eiusmod tempor\ + \u{1b}[0m \u{1b}[7;32m**incididunt**\u{1b}[0m \u{1b}[32mut labore \ + et dolore magna\u{1b}[0m \n aliqua. Ut enim ad minim veniam, quis \ + nostrud exercitation\nullamco laboris nisi ut aliquip ex ea \ + commodo consequat.\n" + ); } } diff --git a/src/errors.rs b/src/errors.rs index 9f0c7ea..af9ab5e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -18,16 +18,25 @@ error_chain! { err = err, ) } - ExitCodeMismatch(cmd: Vec, expected: Option, got: Option, out: String, err: String) { + ExitCodeMismatch( + cmd: Vec, + expected: Option, + got: Option, + out: String, + err: String + ) { description("Wrong exit code") display( - "{}: (exit code of `{}` expected to be `{:?}`)\nexit code=`{:?}`\nstdout=```{}```\nstderr=```{}```", - ERROR_PREFIX, - cmd.join(" "), - expected, - got, - out, - err, + "{prefix}: (exit code of `{cmd}` expected to be `{expected:?}`)\n\ + exit code=`{code:?}`\n\ + stdout=```{stdout}```\n\ + stderr=```{stderr}```", + prefix=ERROR_PREFIX, + cmd=cmd.join(" "), + expected=expected, + code=got, + stdout=out, + stderr=err, ) } OutputMismatch(cmd: Vec, output_err: ::output::Error, kind: ::output::OutputKind) { diff --git a/src/lib.rs b/src/lib.rs index 5d103da..c902497 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,12 +118,14 @@ #![deny(missing_docs)] extern crate difference; -#[macro_use] extern crate error_chain; +#[macro_use] +extern crate error_chain; extern crate serde_json; mod errors; -#[macro_use] mod macros; +#[macro_use] +mod macros; pub use macros::flatten_escaped_string; mod output; diff --git a/src/macros.rs b/src/macros.rs index 1078032..9b5350d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,5 @@ -use std::borrow::Cow; use serde_json; +use std::borrow::Cow; /// Easily construct an `Assert` with a custom command. /// @@ -50,8 +50,7 @@ macro_rules! assert_cmd { /// If `x` can not be decoded as `String`. #[doc(hidden)] fn deserialize_json_string(x: &str) -> String { - serde_json::from_str(x) - .expect(&format!("Unable to deserialize `{:?}` as string.", x)) + serde_json::from_str(x).expect(&format!("Unable to deserialize `{:?}` as string.", x)) } /// Deserialize a JSON-encoded `String`. @@ -103,26 +102,20 @@ mod test { use super::*; #[test] - fn flatten_unquoted() - { - assert_eq!( - flatten_escaped_string("hello world"), - "hello world"); + fn flatten_unquoted() { + assert_eq!(flatten_escaped_string("hello world"), "hello world"); } #[test] - fn flatten_quoted() - { - assert_eq!( - flatten_escaped_string(r#""hello world""#), - "hello world"); + fn flatten_quoted() { + assert_eq!(flatten_escaped_string(r#""hello world""#), "hello world"); } #[test] - fn flatten_escaped() - { + fn flatten_escaped() { assert_eq!( flatten_escaped_string(r#""hello world \u0042 A""#), - "hello world B A"); + "hello world B A" + ); } } diff --git a/src/output.rs b/src/output.rs index e2d7ba3..d3e9d7b 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,10 +1,8 @@ -use std::process::Output; - -use difference::Changeset; - use self::errors::*; pub use self::errors::{Error, ErrorKind}; use diff; +use difference::Changeset; +use std::process::Output; #[derive(Debug, Clone)] pub struct OutputAssertion { @@ -19,7 +17,10 @@ impl OutputAssertion { let result = got.contains(&self.expect); if result != self.expected_result { if self.expected_result { - bail!(ErrorKind::OutputDoesntContain(self.expect.clone(), got.into())); + bail!(ErrorKind::OutputDoesntContain( + self.expect.clone(), + got.into() + )); } else { bail!(ErrorKind::OutputContains(self.expect.clone(), got.into())); } @@ -35,7 +36,11 @@ impl OutputAssertion { if result != self.expected_result { if self.expected_result { let nice_diff = diff::render(&differences)?; - bail!(ErrorKind::OutputDoesntMatch(self.expect.clone(), got.to_owned(), nice_diff)); + bail!(ErrorKind::OutputDoesntMatch( + self.expect.clone(), + got.to_owned(), + nice_diff + )); } else { bail!(ErrorKind::OutputMatches(got.to_owned())); } @@ -52,7 +57,9 @@ impl OutputAssertion { } else { self.matches_exact(&observed) }; - result.map_err(|e| super::errors::ErrorKind::OutputMismatch(cmd.to_vec(), e, self.kind))?; + result.map_err(|e| { + super::errors::ErrorKind::OutputMismatch(cmd.to_vec(), e, self.kind) + })?; Ok(()) }