Skip to content

Test runner interacts badly with redirected child process #35136

Open
@BartMassey

Description

@BartMassey

(Moved here from rust-lang/cargo/2936 as it is believed to be a standard library bug.)

Consider the code at https://gitlab.com/BartMassey/ptyknot/tree/pipes-direct/misc/piperef-rs . The relevant portion is in piperef.rs:

        ...
       // Write "hello world" to stdout.                                       
        match write_mode {
            WriteMode::Macro => {println!("hello world");},
            WriteMode::C => {
                let buf = "hello world".as_bytes();
                let hello = std::ffi::CString::new(buf).unwrap();
                let bufptr = hello.as_ptr() as *const libc::c_void;
                check_cint!(libc::write(1, bufptr, buf.len()));
            }
        }
         ....

This code is run in a child process. When it is invoked by running it after cargo build, it works fine regardless of whether Macro or C mode is used. When it is invoked by cargo test, C mode works but Macro mode fails with an empty string. When it is invoked by cargo test -- --nocapture, both modes work again. Here's a transcript:

$ cargo build
   Compiling libc v0.2.14
   Compiling piperef v0.1.0 (file:///usr/local/src/ptyknot/misc/piperef-rs)
$ target/debug/piperef 
$ cargo test
   Compiling piperef v0.1.0 (file:///usr/local/src/ptyknot/misc/piperef-rs)
     Running target/debug/piperef-80efeb997239b2ac

running 2 tests
test write_macro_test ... FAILED$<2>
test write_c_test ... ok$<2>

failures:

---- write_macro_test stdout ----
    thread 'write_macro_test' panicked at 'assertion failed: `(left == right)` (left: `""`, right: `"hello world"`)', piperef.rs:60
note: Run with `RUST_BACKTRACE=1` for a backtrace.


failures:
    write_macro_test

test result: FAILED$<2>. 1 passed; 1 failed; 0 ignored; 0 measured

error: test failed
$ cargo test -- --nocapture
     Running target/debug/piperef-80efeb997239b2ac

running 2 tests
test write_macro_test ... ok
test write_c_test ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured

$ 

It looks like the test runner is fouling up the environment somehow, but I can't figure out how by looking at stuff.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryA-processArea: `std::process` and `std::env`C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions