3
3
use std:: fs:: File ;
4
4
use std:: io;
5
5
use std:: io:: prelude:: Write ;
6
+ use std:: path:: PathBuf ;
6
7
use std:: time:: Instant ;
7
8
use std:: vec;
8
9
@@ -73,6 +74,33 @@ struct OutputMultiplexer {
73
74
pub outputs : Vec < Box < dyn Output > > ,
74
75
}
75
76
77
+ impl OutputMultiplexer {
78
+ pub fn new ( lock_stdout : bool , logfile : & Option < PathBuf > ) -> io:: Result < Self > {
79
+ let mut outputs: Vec < Box < dyn Output > > = vec ! [ ] ;
80
+
81
+ if lock_stdout {
82
+ let output = match term:: stdout ( ) {
83
+ None => OutputLocation :: Raw ( io:: stdout ( ) . lock ( ) ) ,
84
+ Some ( t) => OutputLocation :: Pretty ( t) ,
85
+ } ;
86
+ outputs. push ( Box :: new ( output) )
87
+ } else {
88
+ let output = match term:: stdout ( ) {
89
+ None => OutputLocation :: Raw ( io:: stdout ( ) ) ,
90
+ Some ( t) => OutputLocation :: Pretty ( t) ,
91
+ } ;
92
+ outputs. push ( Box :: new ( output) )
93
+ }
94
+
95
+ match logfile {
96
+ Some ( ref path) => outputs. push ( Box :: new ( OutputLocation :: Raw ( File :: create ( path) ?) ) ) ,
97
+ None => ( ) ,
98
+ } ;
99
+
100
+ Ok ( Self { outputs } )
101
+ }
102
+ }
103
+
76
104
impl Output for OutputMultiplexer {
77
105
fn write_pretty ( & mut self , word : & str , color : term:: color:: Color ) -> io:: Result < ( ) > {
78
106
for output in & mut self . outputs {
@@ -92,33 +120,14 @@ impl Output for OutputMultiplexer {
92
120
}
93
121
94
122
pub struct ConsoleTestDiscoveryState {
95
- log_out : OutputMultiplexer ,
96
123
pub tests : usize ,
97
124
pub benchmarks : usize ,
98
125
pub ignored : usize ,
99
126
}
100
127
101
128
impl ConsoleTestDiscoveryState {
102
129
pub fn new ( opts : & TestOpts ) -> io:: Result < ConsoleTestDiscoveryState > {
103
- let mut log_out = OutputMultiplexer { outputs : vec ! [ ] } ;
104
- match opts. logfile {
105
- Some ( ref path) => {
106
- log_out. outputs . push ( Box :: new ( OutputLocation :: Raw ( File :: create ( path) ?) ) )
107
- }
108
- None => ( ) ,
109
- } ;
110
-
111
- Ok ( ConsoleTestDiscoveryState { log_out, tests : 0 , benchmarks : 0 , ignored : 0 } )
112
- }
113
-
114
- pub fn write_log < F , S > ( & mut self , msg : F ) -> io:: Result < ( ) >
115
- where
116
- S : AsRef < str > ,
117
- F : FnOnce ( ) -> S ,
118
- {
119
- let msg = msg ( ) ;
120
- let msg = msg. as_ref ( ) ;
121
- self . log_out . write_plain ( msg)
130
+ Ok ( ConsoleTestDiscoveryState { tests : 0 , benchmarks : 0 , ignored : 0 } )
122
131
}
123
132
}
124
133
@@ -219,19 +228,16 @@ impl ConsoleTestState {
219
228
220
229
// List the tests to console, and optionally to logfile. Filters are honored.
221
230
pub fn list_tests_console ( opts : & TestOpts , tests : Vec < TestDescAndFn > ) -> io:: Result < ( ) > {
222
- let mut output = match term:: stdout ( ) {
223
- None => OutputLocation :: Raw ( io:: stdout ( ) . lock ( ) ) ,
224
- Some ( t) => OutputLocation :: Pretty ( t) ,
225
- } ;
231
+ let mut st = ConsoleTestDiscoveryState :: new ( opts) ?;
226
232
233
+ let mut multiplexer = OutputMultiplexer :: new ( true , & opts. logfile ) ?;
227
234
let mut out: Box < dyn OutputFormatter > = match opts. format {
228
235
OutputFormat :: Pretty | OutputFormat :: Junit => {
229
- Box :: new ( PrettyFormatter :: new ( & mut output , false , 0 , false , None ) )
236
+ Box :: new ( PrettyFormatter :: new ( & mut multiplexer , false , 0 , false , None ) )
230
237
}
231
- OutputFormat :: Terse => Box :: new ( TerseFormatter :: new ( & mut output , false , 0 , false ) ) ,
232
- OutputFormat :: Json => Box :: new ( JsonFormatter :: new ( & mut output ) ) ,
238
+ OutputFormat :: Terse => Box :: new ( TerseFormatter :: new ( & mut multiplexer , false , 0 , false ) ) ,
239
+ OutputFormat :: Json => Box :: new ( JsonFormatter :: new ( & mut multiplexer ) ) ,
233
240
} ;
234
- let mut st = ConsoleTestDiscoveryState :: new ( opts) ?;
235
241
236
242
out. write_discovery_start ( ) ?;
237
243
for test in filter_tests ( opts, tests) . into_iter ( ) {
@@ -253,7 +259,6 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
253
259
st. ignored += if desc. ignore { 1 } else { 0 } ;
254
260
255
261
out. write_test_discovered ( & desc, fntype) ?;
256
- st. write_log ( || format ! ( "{fntype} {}\n " , desc. name) ) ?;
257
262
}
258
263
259
264
out. write_discovery_finish ( & st)
0 commit comments