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

Commit

Permalink
Merge #49
Browse files Browse the repository at this point in the history
49: Run and add rustfmt r=killercup a=killercup
  • Loading branch information
bors[bot] committed Oct 10, 2017
2 parents 177493e + cc255a1 commit c17c1aa
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 57 deletions.
15 changes: 12 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
format_strings = false
reorder_imports = true

28 changes: 17 additions & 11 deletions src/assert.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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,
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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()?;

Expand All @@ -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(
Expand Down Expand Up @@ -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
}

Expand Down
30 changes: 21 additions & 9 deletions src/diff.rs
Original file line number Diff line number Diff line change
@@ -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<String, fmtError> {
let mut t = String::new();
Expand Down Expand Up @@ -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"
);
}
}
25 changes: 17 additions & 8 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,25 @@ error_chain! {
err = err,
)
}
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>, out: String, err: String) {
ExitCodeMismatch(
cmd: Vec<String>,
expected: Option<i32>,
got: Option<i32>,
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<String>, output_err: ::output::Error, kind: ::output::OutputKind) {
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 9 additions & 16 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use serde_json;
use std::borrow::Cow;

/// Easily construct an `Assert` with a custom command.
///
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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"
);
}
}
21 changes: 14 additions & 7 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()));
}
Expand All @@ -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()));
}
Expand All @@ -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(())
}
Expand Down

0 comments on commit c17c1aa

Please sign in to comment.