Skip to content

Commit

Permalink
Reimplement logfile output via console::Output trait.
Browse files Browse the repository at this point in the history
Make logfile output more similar to normal test output.
  • Loading branch information
aspotashev committed Apr 2, 2024
1 parent fc6c737 commit e9a4a61
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions library/test/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs::File;
use std::io;
use std::io::prelude::Write;
use std::time::Instant;
use std::vec;

use super::{
bench::fmt_bench_samples,
Expand Down Expand Up @@ -34,14 +35,14 @@ impl<T: Write> Write for OutputLocation<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match *self {
OutputLocation::Pretty(ref mut term) => term.write(buf),
OutputLocation::Raw(ref mut stdout) => stdout.write(buf),
OutputLocation::Raw(ref mut stdout_or_file) => stdout_or_file.write(buf),
}
}

fn flush(&mut self) -> io::Result<()> {
match *self {
OutputLocation::Pretty(ref mut term) => term.flush(),
OutputLocation::Raw(ref mut stdout) => stdout.flush(),
OutputLocation::Raw(ref mut stdout_or_file) => stdout_or_file.flush(),
}
}
}
Expand All @@ -68,18 +69,43 @@ impl<T: Write> Output for OutputLocation<T> {
}
}

struct OutputMultiplexer {
pub outputs: Vec<Box<dyn Output>>,
}

impl Output for OutputMultiplexer {
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
for output in &mut self.outputs {
output.write_pretty(word, color)?;
}

Ok(())
}

fn write_plain(&mut self, word: &str) -> io::Result<()> {
for output in &mut self.outputs {
output.write_plain(word)?;
}

Ok(())
}
}

pub struct ConsoleTestDiscoveryState {
pub log_out: Option<File>,
log_out: OutputMultiplexer,
pub tests: usize,
pub benchmarks: usize,
pub ignored: usize,
}

impl ConsoleTestDiscoveryState {
pub fn new(opts: &TestOpts) -> io::Result<ConsoleTestDiscoveryState> {
let log_out = match opts.logfile {
Some(ref path) => Some(File::create(path)?),
None => None,
let mut log_out = OutputMultiplexer { outputs: vec![] };
match opts.logfile {
Some(ref path) => {
log_out.outputs.push(Box::new(OutputLocation::Raw(File::create(path)?)))
}
None => (),
};

Ok(ConsoleTestDiscoveryState { log_out, tests: 0, benchmarks: 0, ignored: 0 })
Expand All @@ -90,14 +116,9 @@ impl ConsoleTestDiscoveryState {
S: AsRef<str>,
F: FnOnce() -> S,
{
match self.log_out {
None => Ok(()),
Some(ref mut o) => {
let msg = msg();
let msg = msg.as_ref();
o.write_all(msg.as_bytes())
}
}
let msg = msg();
let msg = msg.as_ref();
self.log_out.write_plain(msg)
}
}

Expand Down

0 comments on commit e9a4a61

Please sign in to comment.