Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BufferRedirect is broken in unit tests #5

Open
soenkehahn opened this issue Mar 6, 2019 · 9 comments
Open

BufferRedirect is broken in unit tests #5

soenkehahn opened this issue Mar 6, 2019 · 9 comments

Comments

@soenkehahn
Copy link

soenkehahn commented Mar 6, 2019

This example from the readme fails when put into a unit test:

use std::io::Read;
use gag::BufferRedirect;

let mut buf = BufferRedirect::stdout().unwrap();
println!("Hello world!");

let mut output = String::new();
buf.read_to_string(&mut output).unwrap();

assert_eq!(&output[..], "Hello world!\n");

output is empty when assert_eq is executed.

rustc --version is rustc 1.33.0 (2aa4c46cf 2019-02-28).

@Stebalien
Copy link
Owner

What operating system? This works for me on Linux.

@soenkehahn
Copy link
Author

Yeah, on linux.

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.10
DISTRIB_CODENAME=cosmic
DISTRIB_DESCRIPTION="Ubuntu 18.10"

After some experimenting I figured out that it works if put into a main function. It doesn't work when put into a unit test (with #[test]). My main use-case for gag is to capture stdout and stderr during tests, to write tests that ensure that some function writes something to those streams. I'll edit the issue description.

@soenkehahn soenkehahn changed the title BufferRedirect is broken BufferRedirect is broken in unit tests Mar 6, 2019
@Stebalien
Copy link
Owner

Ah. So, rust uses a super-special set_print function to override where println writes data when testing (but not in doc tests?). Unfortunately, there's not much I can do about that.

@Stebalien
Copy link
Owner

(at least I think that's what's happening here)

@Stebalien
Copy link
Owner

Actually... I'm not sure.

@assapir
Copy link

assapir commented Dec 13, 2020

Any news regarding this? :(

@Stebalien
Copy link
Owner

Stebalien commented Dec 14, 2020 via email

@assapir
Copy link

assapir commented Dec 14, 2020

As far as I could figure out this is because by default, cargo test redirect the output so it won't show up. Running with -- --nocapture will actually solve the problem. Then the problem is that it run tests in parallel.

@soenkehahn
Copy link
Author

You can run tests in sequence with --test-threads=1 for what it's worth.

Personally I don't like that rusts unit tests mess with stdout like that. It has a lot of downsides, besides breaking gag-rs... :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants