33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
55use std:: io;
6- use std:: io:: { File , Command , Writer , TempDir } ;
7- use std:: task;
6+ use std:: io:: { File , Command , Writer , TempDir , IoResult } ;
87use serialize:: { json} ;
98use serialize:: json:: ToJson ;
109use 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
5473fn 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