33use std:: fs:: File ;
44use std:: io;
55use std:: io:: prelude:: Write ;
6+ use std:: path:: PathBuf ;
67use std:: time:: Instant ;
78use std:: vec;
89
@@ -73,6 +74,33 @@ struct OutputMultiplexer {
7374 pub outputs : Vec < Box < dyn Output > > ,
7475}
7576
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+
76104impl Output for OutputMultiplexer {
77105 fn write_pretty ( & mut self , word : & str , color : term:: color:: Color ) -> io:: Result < ( ) > {
78106 for output in & mut self . outputs {
@@ -92,33 +120,14 @@ impl Output for OutputMultiplexer {
92120}
93121
94122pub struct ConsoleTestDiscoveryState {
95- log_out : OutputMultiplexer ,
96123 pub tests : usize ,
97124 pub benchmarks : usize ,
98125 pub ignored : usize ,
99126}
100127
101128impl ConsoleTestDiscoveryState {
102- 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)
129+ pub fn new ( ) -> io:: Result < ConsoleTestDiscoveryState > {
130+ Ok ( ConsoleTestDiscoveryState { tests : 0 , benchmarks : 0 , ignored : 0 } )
122131 }
123132}
124133
@@ -219,19 +228,16 @@ impl ConsoleTestState {
219228
220229// List the tests to console, and optionally to logfile. Filters are honored.
221230pub 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 ( ) ?;
226232
233+ let mut multiplexer = OutputMultiplexer :: new ( true , & opts. logfile ) ?;
227234 let mut out: Box < dyn OutputFormatter > = match opts. format {
228235 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 ) )
230237 }
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 ) ) ,
233240 } ;
234- let mut st = ConsoleTestDiscoveryState :: new ( opts) ?;
235241
236242 out. write_discovery_start ( ) ?;
237243 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
253259 st. ignored += if desc. ignore { 1 } else { 0 } ;
254260
255261 out. write_test_discovered ( & desc, fntype) ?;
256- st. write_log ( || format ! ( "{fntype} {}\n " , desc. name) ) ?;
257262 }
258263
259264 out. write_discovery_finish ( & st)
0 commit comments