Skip to content

Commit 1d0b9ff

Browse files
committed
Use try!() instead of task::try() in test failure reporting
… when shelling out to `colordiff`
1 parent 2beda95 commit 1d0b9ff

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/tests.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use std::io;
6-
use std::io::{File, Command, Writer, TempDir};
7-
use std::task;
6+
use std::io::{File, Command, Writer, TempDir, IoResult};
87
use serialize::{json};
98
use serialize::json::ToJson;
109
use test;
@@ -23,11 +22,31 @@ macro_rules! JList {
2322
}
2423

2524

26-
fn write_whole_file(path: &Path, data: &str) {
27-
match File::open_mode(path, io::Open, io::Write) {
28-
Ok(mut writer) => { writer.write(data.as_bytes()).unwrap(); },
29-
_ => fail!("could not open file"),
30-
}
25+
fn write_whole_file(path: &Path, data: &str) -> IoResult<()> {
26+
(try!(File::open_mode(path, io::Open, io::Write))).write(data.as_bytes())
27+
}
28+
29+
30+
fn print_json_diff(results: &json::Json, expected: &json::Json) -> IoResult<()> {
31+
let temp = match TempDir::new("rust-cssparser-tests") {
32+
Some(temp) => temp,
33+
None => return Err(io::standard_error(io::OtherIoError)),
34+
};
35+
let results = results.to_pretty_str().append("\n");
36+
let expected = expected.to_pretty_str().append("\n");
37+
let mut result_path = temp.path().clone();
38+
result_path.push("results.json");
39+
let mut expected_path = temp.path().clone();
40+
expected_path.push("expected.json");
41+
try!(write_whole_file(&result_path, results.as_slice()));
42+
try!(write_whole_file(&expected_path, expected.as_slice()));
43+
try!(Command::new("colordiff")
44+
.arg("-u1000")
45+
.arg(result_path.display().to_string())
46+
.arg(expected_path.display().to_string())
47+
.status()
48+
.map_err(|_| io::standard_error(io::OtherIoError)));
49+
Ok(())
3150
}
3251

3352

@@ -53,25 +72,7 @@ fn almost_equals(a: &json::Json, b: &json::Json) -> bool {
5372

5473
fn assert_json_eq(results: json::Json, expected: json::Json, message: String) {
5574
if !almost_equals(&results, &expected) {
56-
let temp = TempDir::new("rust-cssparser-tests").unwrap();
57-
let results = results.to_pretty_str().append("\n");
58-
let expected = expected.to_pretty_str().append("\n");
59-
// NB: The task try is to prevent error message generation from
60-
// stopping us, we don't care about the result.
61-
let _ = task::try(proc() {
62-
let mut result_path = temp.path().clone();
63-
result_path.push("results.json");
64-
let mut expected_path = temp.path().clone();
65-
expected_path.push("expected.json");
66-
write_whole_file(&result_path, results.as_slice());
67-
write_whole_file(&expected_path, expected.as_slice());
68-
Command::new("colordiff")
69-
.arg("-u1000")
70-
.arg(result_path.display().to_string())
71-
.arg(expected_path.display().to_string())
72-
.status().unwrap()
73-
});
74-
75+
let _ = print_json_diff(&results, &expected).unwrap();
7576
fail!(message)
7677
}
7778
}

0 commit comments

Comments
 (0)