Skip to content

Stop using temp files and an external program for diffs in test failures #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2017
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude = ["src/css-parsing-tests"]

[dev-dependencies]
rustc-serialize = "0.3"
tempdir = "0.3"
difference = "1.0"
encoding_rs = "0.5"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
#[macro_use] extern crate procedural_masquerade;
#[doc(hidden)] pub extern crate phf as _internal__phf;
#[cfg(test)] extern crate encoding_rs;
#[cfg(test)] extern crate tempdir;
#[cfg(test)] extern crate difference;
#[cfg(test)] extern crate rustc_serialize;
#[cfg(feature = "serde")] extern crate serde;
#[cfg(feature = "heapsize")] #[macro_use] extern crate heapsize;
Expand Down
34 changes: 5 additions & 29 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ extern crate test;

use encoding_rs;
use std::borrow::Cow::{self, Borrowed};
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;
use rustc_serialize::json::{self, Json, ToJson};
use tempdir::TempDir;

#[cfg(feature = "bench")]
use self::test::Bencher;
Expand All @@ -30,29 +25,6 @@ macro_rules! JArray {
($($e: expr),*) => { Json::Array(vec!( $( $e.to_json() ),* )) }
}


fn write_whole_file(path: &Path, data: &str) -> io::Result<()> {
(try!(File::create(path))).write_all(data.as_bytes())
}


fn print_json_diff(results: &Json, expected: &Json) -> io::Result<()> {
let temp = try!(TempDir::new("rust-cssparser-tests"));
let results = results.pretty().to_string() + "\n";
let expected = expected.pretty().to_string() + "\n";
let result_path = temp.path().join("results.json");
let expected_path = temp.path().join("expected.json");
try!(write_whole_file(&result_path, &results));
try!(write_whole_file(&expected_path, &expected));
try!(Command::new("colordiff")
.arg("-u1000")
.arg(&result_path)
.arg(&expected_path)
.status());
Ok(())
}


fn almost_equals(a: &Json, b: &Json) -> bool {
match (a, b) {
(&Json::I64(a), _) => almost_equals(&Json::F64(a as f64), b),
Expand Down Expand Up @@ -93,7 +65,11 @@ fn normalize(json: &mut Json) {
fn assert_json_eq(results: json::Json, mut expected: json::Json, message: String) {
normalize(&mut expected);
if !almost_equals(&results, &expected) {
print_json_diff(&results, &expected).unwrap();
println!("{}", ::difference::Changeset::new(
&results.pretty().to_string(),
&expected.pretty().to_string(),
"\n",
));
panic!(message)
}
}
Expand Down