@@ -9,44 +9,57 @@ impl<T: Write> JsonFormatter<T> {
9
9
Self { out }
10
10
}
11
11
12
- fn write_message ( & mut self , s : & str ) -> io:: Result < ( ) > {
12
+ fn writeln_message ( & mut self , s : & str ) -> io:: Result < ( ) > {
13
13
assert ! ( !s. contains( '\n' ) ) ;
14
14
15
15
self . out . write_all ( s. as_ref ( ) ) ?;
16
16
self . out . write_all ( b"\n " )
17
17
}
18
18
19
+ fn write_message ( & mut self , s : & str ) -> io:: Result < ( ) > {
20
+ assert ! ( !s. contains( '\n' ) ) ;
21
+
22
+ self . out . write_all ( s. as_ref ( ) )
23
+ }
24
+
19
25
fn write_event (
20
26
& mut self ,
21
27
ty : & str ,
22
28
name : & str ,
23
29
evt : & str ,
24
- extra : Option < String > ,
30
+ stdout : Option < Cow < ' _ , str > > ,
31
+ extra : Option < & str > ,
25
32
) -> io:: Result < ( ) > {
26
- if let Some ( extras) = extra {
33
+ self . write_message ( & * format ! (
34
+ r#"{{ "type": "{}", "name": "{}", "event": "{}""# ,
35
+ ty, name, evt
36
+ ) ) ?;
37
+ if let Some ( stdout) = stdout {
27
38
self . write_message ( & * format ! (
28
- r#"{{ "type": "{}", "name": "{}", "event": "{}", {} }}"# ,
29
- ty, name, evt, extras
30
- ) )
31
- } else {
39
+ r#", "stdout": "{}""# ,
40
+ EscapedString ( stdout)
41
+ ) ) ?;
42
+ }
43
+ if let Some ( extra) = extra {
32
44
self . write_message ( & * format ! (
33
- r#"{{ "type": "{}", "name": "{}", "event": "{}" } }"# ,
34
- ty , name , evt
35
- ) )
45
+ r#", { }"# ,
46
+ extra
47
+ ) ) ? ;
36
48
}
49
+ self . writeln_message ( " }" )
37
50
}
38
51
}
39
52
40
53
impl < T : Write > OutputFormatter for JsonFormatter < T > {
41
54
fn write_run_start ( & mut self , test_count : usize ) -> io:: Result < ( ) > {
42
- self . write_message ( & * format ! (
55
+ self . writeln_message ( & * format ! (
43
56
r#"{{ "type": "suite", "event": "started", "test_count": {} }}"# ,
44
57
test_count
45
58
) )
46
59
}
47
60
48
61
fn write_test_start ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
49
- self . write_message ( & * format ! (
62
+ self . writeln_message ( & * format ! (
50
63
r#"{{ "type": "test", "event": "started", "name": "{}" }}"# ,
51
64
desc. name
52
65
) )
@@ -57,34 +70,30 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
57
70
desc : & TestDesc ,
58
71
result : & TestResult ,
59
72
stdout : & [ u8 ] ,
73
+ state : & ConsoleTestState ,
60
74
) -> io:: Result < ( ) > {
75
+ let stdout = if ( state. options . display_output || * result != TrOk ) && stdout. len ( ) > 0 {
76
+ Some ( String :: from_utf8_lossy ( stdout) )
77
+ } else {
78
+ None
79
+ } ;
61
80
match * result {
62
- TrOk => self . write_event ( "test" , desc. name . as_slice ( ) , "ok" , None ) ,
63
-
64
- TrFailed => {
65
- let extra_data = if stdout. len ( ) > 0 {
66
- Some ( format ! (
67
- r#""stdout": "{}""# ,
68
- EscapedString ( String :: from_utf8_lossy( stdout) )
69
- ) )
70
- } else {
71
- None
72
- } ;
81
+ TrOk => self . write_event ( "test" , desc. name . as_slice ( ) , "ok" , stdout, None ) ,
73
82
74
- self . write_event ( "test" , desc. name . as_slice ( ) , "failed" , extra_data)
75
- }
83
+ TrFailed => self . write_event ( "test" , desc. name . as_slice ( ) , "failed" , stdout, None ) ,
76
84
77
85
TrFailedMsg ( ref m) => self . write_event (
78
86
"test" ,
79
87
desc. name . as_slice ( ) ,
80
88
"failed" ,
81
- Some ( format ! ( r#""message": "{}""# , EscapedString ( m) ) ) ,
89
+ stdout,
90
+ Some ( & * format ! ( r#""message": "{}""# , EscapedString ( m) ) ) ,
82
91
) ,
83
92
84
- TrIgnored => self . write_event ( "test" , desc. name . as_slice ( ) , "ignored" , None ) ,
93
+ TrIgnored => self . write_event ( "test" , desc. name . as_slice ( ) , "ignored" , stdout , None ) ,
85
94
86
95
TrAllowedFail => {
87
- self . write_event ( "test" , desc. name . as_slice ( ) , "allowed_failure" , None )
96
+ self . write_event ( "test" , desc. name . as_slice ( ) , "allowed_failure" , stdout , None )
88
97
}
89
98
90
99
TrBench ( ref bs) => {
@@ -105,20 +114,20 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
105
114
desc. name, median, deviation, mbps
106
115
) ;
107
116
108
- self . write_message ( & * line)
117
+ self . writeln_message ( & * line)
109
118
}
110
119
}
111
120
}
112
121
113
122
fn write_timeout ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
114
- self . write_message ( & * format ! (
123
+ self . writeln_message ( & * format ! (
115
124
r#"{{ "type": "test", "event": "timeout", "name": "{}" }}"# ,
116
125
desc. name
117
126
) )
118
127
}
119
128
120
129
fn write_run_finish ( & mut self , state : & ConsoleTestState ) -> io:: Result < bool > {
121
- self . write_message ( & * format ! (
130
+ self . writeln_message ( & * format ! (
122
131
"{{ \" type\" : \" suite\" , \
123
132
\" event\" : \" {}\" , \
124
133
\" passed\" : {}, \
0 commit comments