I'm working on a logging system in Rust where I want to capture the output of a macro that writes to
stdout
(viaprintln!
) and validate it in tests.To capture output, I'm using a
dup2
+tempfile
+libc::STDOUT_FILENO
strategy that redirectsstdout
to a temporary file. It works perfectly when invoked frommain.rs
, but fails when the exact same code is run inside a#[test]
function.
- I’m not using
io::set_output_capture
- Even with
--nocapture
,stdout
seems not to flow to the redirected output- My macro prints correctly, but the capture shows
""
Has anyone else run into this? Is the test harness doing some internal redirection or buffering of
stdout
before mydup2
has a chance to take effect?Here's a minimal example (with
main.rs
andtests/test-main.rs
) to reproduce.
Rust version: rustc 1.86.0 (05f9846f8 2025-03-31)
Appreciate any pointers. Thanks!