Skip to content

Commit e9a4a61

Browse files
committed
Reimplement logfile output via console::Output trait.
Make logfile output more similar to normal test output.
1 parent fc6c737 commit e9a4a61

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

library/test/src/console.rs

+35-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fs::File;
44
use std::io;
55
use std::io::prelude::Write;
66
use std::time::Instant;
7+
use std::vec;
78

89
use super::{
910
bench::fmt_bench_samples,
@@ -34,14 +35,14 @@ impl<T: Write> Write for OutputLocation<T> {
3435
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
3536
match *self {
3637
OutputLocation::Pretty(ref mut term) => term.write(buf),
37-
OutputLocation::Raw(ref mut stdout) => stdout.write(buf),
38+
OutputLocation::Raw(ref mut stdout_or_file) => stdout_or_file.write(buf),
3839
}
3940
}
4041

4142
fn flush(&mut self) -> io::Result<()> {
4243
match *self {
4344
OutputLocation::Pretty(ref mut term) => term.flush(),
44-
OutputLocation::Raw(ref mut stdout) => stdout.flush(),
45+
OutputLocation::Raw(ref mut stdout_or_file) => stdout_or_file.flush(),
4546
}
4647
}
4748
}
@@ -68,18 +69,43 @@ impl<T: Write> Output for OutputLocation<T> {
6869
}
6970
}
7071

72+
struct OutputMultiplexer {
73+
pub outputs: Vec<Box<dyn Output>>,
74+
}
75+
76+
impl Output for OutputMultiplexer {
77+
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
78+
for output in &mut self.outputs {
79+
output.write_pretty(word, color)?;
80+
}
81+
82+
Ok(())
83+
}
84+
85+
fn write_plain(&mut self, word: &str) -> io::Result<()> {
86+
for output in &mut self.outputs {
87+
output.write_plain(word)?;
88+
}
89+
90+
Ok(())
91+
}
92+
}
93+
7194
pub struct ConsoleTestDiscoveryState {
72-
pub log_out: Option<File>,
95+
log_out: OutputMultiplexer,
7396
pub tests: usize,
7497
pub benchmarks: usize,
7598
pub ignored: usize,
7699
}
77100

78101
impl ConsoleTestDiscoveryState {
79102
pub fn new(opts: &TestOpts) -> io::Result<ConsoleTestDiscoveryState> {
80-
let log_out = match opts.logfile {
81-
Some(ref path) => Some(File::create(path)?),
82-
None => None,
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 => (),
83109
};
84110

85111
Ok(ConsoleTestDiscoveryState { log_out, tests: 0, benchmarks: 0, ignored: 0 })
@@ -90,14 +116,9 @@ impl ConsoleTestDiscoveryState {
90116
S: AsRef<str>,
91117
F: FnOnce() -> S,
92118
{
93-
match self.log_out {
94-
None => Ok(()),
95-
Some(ref mut o) => {
96-
let msg = msg();
97-
let msg = msg.as_ref();
98-
o.write_all(msg.as_bytes())
99-
}
100-
}
119+
let msg = msg();
120+
let msg = msg.as_ref();
121+
self.log_out.write_plain(msg)
101122
}
102123
}
103124

0 commit comments

Comments
 (0)