Skip to content

Test output capture does not capture Command output #92370

Open
@jonhoo

Description

@jonhoo

I tried this code:

#[test]
fn foo() {
    println!("hello");
    std::process::Command::new("echo").arg("world").status().unwrap();
}

I expected to see this happen: nothing should be printed to STDOUT for cargo test, while "hello" and "world" should be printed to STDOUT with cargo test -- --nocapture.

Instead, this happened: "world" is always printed to STDOUT (playground).

Meta

rustc --version --verbose:

rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-apple-darwin
release: 1.57.0
LLVM version: 13.0.0

The issue also occurs on nightly.

Notes

This happens because the output capture hook used by libtest only affects things that go through print_to:

fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)

It's already known that libtest output capturing has some... inconsistencies (e.g., #12309, #90785, #35136), but in this particular case I wonder if it's possible to do something about it since std controls the defaults for Command. Specifically, it might make sense to have Command default to a capturing .stdout and .stderr when those aren't overwritten in the builder.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions