Fix io capture in tests (#573)

This commit is contained in:
Firstyear 2021-08-24 14:23:53 +10:00 committed by GitHub
parent bc31d42f22
commit 09e83a98c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -64,9 +64,11 @@ impl fmt::Display for LogTag {
macro_rules! lqueue {
($audit:expr, $tag:expr, $($arg:tt)*) => ({
use crate::audit::{LogTag, AUDIT_LINE_SIZE};
/*
if cfg!(test) {
println!($($arg)*)
}
*/
if ($audit.level & $tag as u32) == $tag as u32 {
use std::fmt;
// We have to buffer the string to over-alloc it.

View file

@ -230,7 +230,7 @@ impl<P: Processor> Layer<Registry> for TreeLayer<P> {
let name = attrs.metadata().name();
let mut uuid = None;
let mut out = TreeIo::Stderr;
let mut out = TreeIo::Stdout;
attrs.record(
&mut |field: &Field, value: &dyn fmt::Debug| match field.name() {
@ -478,8 +478,26 @@ impl TreePreProcessed {
let buf = &formatted_logs[..];
match processed_logs.tree_io() {
TreeIo::Stdout => io::stdout().write_all(buf),
TreeIo::Stderr => io::stderr().write_all(buf),
TreeIo::Stdout => {
// BUG - we can't write to stdout/err directly because this breaks
// cargo test capturing of io.
// io::stdout().write_all(buf)
match std::str::from_utf8(buf) {
Ok(s) => print!("{}", s),
Err(e) => eprintln!("CRITICAL - UNABLE TO PRINT BUFFER -> {:?}", e),
}
Ok(())
}
TreeIo::Stderr => {
// io::stderr().write_all(buf)
match std::str::from_utf8(buf) {
Ok(s) => eprint!("{}", s),
Err(e) => eprintln!("CRITICAL - UNABLE TO PRINT BUFFER -> {:?}", e),
}
Ok(())
}
TreeIo::File(ref path) => OpenOptions::new()
.create(true)
.append(true)
@ -493,7 +511,7 @@ impl TreePreProcessed {
impl TreeProcessed {
fn tree_io(self) -> TreeIo {
match self {
TreeProcessed::Event(_) => TreeIo::Stderr,
TreeProcessed::Event(_) => TreeIo::Stdout,
TreeProcessed::Span(TreeSpanProcessed { out, .. }) => out,
}
}