rust-cli / anstyle

ANSI text styling
https://docs.rs/anstyle
Other
120 stars 19 forks source link

`anstream::println!()` shows output during tests #88

Closed robertwahler closed 1 year ago

robertwahler commented 1 year ago

When running anstream::println!("should this show?"); in a unit test, I would have thought that no output would show on stdout unless running the test via cargo test -- --nocapture but it shows regardless of the no capture flag. I'm pretty new to rust so I may be missing something obvious. Thanks!

epage commented 1 year ago

The standard library's println uses a semi-public-but-unstable API for the test crate to capture output

See https://github.com/rust-lang/rust/blob/master/library/std/src/io/stdio.rs#L979

To support this, we'd need to special case print/println/eprint/eprintln to call the original if #[cfg(test)]. This would require making some of the logic shuffled around to allow the logic to wrap around other macros and not just streams. The conditional behavior for the error handling should be preserved in the non-test case. As broken pipes for tests is likely to be a problem.