Skip to content

Commit

Permalink
Use add_spawn_hook for libtest's output capturing.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Nov 19, 2024
1 parent 24a0765 commit 2cc4b2e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 0 additions & 4 deletions std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,6 @@ impl Builder {
});
let their_packet = my_packet.clone();

let output_capture = crate::io::set_output_capture(None);
crate::io::set_output_capture(output_capture.clone());

// Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
// To prevent leaks we use a wrapper that drops its contents.
Expand Down Expand Up @@ -542,7 +539,6 @@ impl Builder {
imp::Thread::set_name(name);
}

crate::io::set_output_capture(output_capture);
for hook in hooks {
hook();
}
Expand Down
11 changes: 11 additions & 0 deletions test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#![feature(process_exitcode_internals)]
#![feature(panic_can_unwind)]
#![feature(test)]
#![feature(thread_spawn_hook)]
#![allow(internal_features)]
#![warn(rustdoc::unescaped_backticks)]

Expand Down Expand Up @@ -134,6 +135,16 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Opt
}
});
panic::set_hook(hook);
// Use a thread spawning hook to make new threads inherit output capturing.
std::thread::add_spawn_hook(|_| {
// Get and clone the output capture of the current thread.
let output_capture = io::set_output_capture(None);
io::set_output_capture(output_capture.clone());
// Set the output capture of the new thread.
Ok(|| {
io::set_output_capture(output_capture);
})
});
}
let res = console::run_tests_console(&opts, tests);
// Prevent Valgrind from reporting reachable blocks in users' unit tests.
Expand Down

0 comments on commit 2cc4b2e

Please sign in to comment.