Skip to content

Make tests capture the error printed by a Result return #102794

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

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ use crate::sys_common::memchr;

#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
pub use self::buffered::WriterPanicked;
pub(crate) use self::stdio::attempt_print_to_stderr;
#[unstable(feature = "internal_output_capture", issue = "none")]
#[doc(no_inline, hidden)]
pub use self::stdio::set_output_capture;
Expand Down
28 changes: 22 additions & 6 deletions library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,18 @@ fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
where
T: Write,
{
if OUTPUT_CAPTURE_USED.load(Ordering::Relaxed)
if print_to_buffer_if_capture_used(args) {
// Successfully wrote to capture buffer.
return;
}

if let Err(e) = global_s().write_fmt(args) {
panic!("failed printing to {label}: {e}");
}
}

fn print_to_buffer_if_capture_used(args: fmt::Arguments<'_>) -> bool {
OUTPUT_CAPTURE_USED.load(Ordering::Relaxed)
&& OUTPUT_CAPTURE.try_with(|s| {
// Note that we completely remove a local sink to write to in case
// our printing recursively panics/prints, so the recursive
Expand All @@ -1009,14 +1020,19 @@ where
s.set(Some(w));
})
}) == Ok(Some(()))
{
// Successfully wrote to capture buffer.
}

/// Used by impl Termination for Result to print error after `main` or a test
/// has returned. Should avoid panicking, although we can't help it if one of
/// the Display impls inside args decides to.
pub(crate) fn attempt_print_to_stderr(args: fmt::Arguments<'_>) {
if print_to_buffer_if_capture_used(args) {
return;
}

if let Err(e) = global_s().write_fmt(args) {
panic!("failed printing to {label}: {e}");
}
// Ignore error if the write fails, for example because stderr is already
// closed. There is not much point panicking at this point.
let _ = stderr().write_fmt(args);
}

#[unstable(
Expand Down
4 changes: 1 addition & 3 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2200,9 +2200,7 @@ impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
match self {
Ok(val) => val.report(),
Err(err) => {
// Ignore error if the write fails, for example because stderr is
// already closed. There is not much point panicking at this point.
let _ = writeln!(io::stderr(), "Error: {err:?}");
io::attempt_print_to_stderr(format_args_nl!("Error: {err:?}"));
ExitCode::FAILURE
}
}
Expand Down