Skip to content

Correctly display stdout and stderr in case a doctest is failing #140291

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 2 commits into from
Apr 27, 2025
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
17 changes: 16 additions & 1 deletion src/librustdoc/doctest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,22 @@ mod __doctest_mod {{
.output()
.expect(\"failed to run command\");
if !out.status.success() {{
eprint!(\"{{}}\", String::from_utf8_lossy(&out.stderr));
if let Some(code) = out.status.code() {{
eprintln!(\"Test executable failed (exit status: {{code}}).\");
}} else {{
eprintln!(\"Test executable failed (terminated by signal).\");
}}
if !out.stdout.is_empty() || !out.stderr.is_empty() {{
eprintln!();
}}
if !out.stdout.is_empty() {{
eprintln!(\"stdout:\");
eprintln!(\"{{}}\", String::from_utf8_lossy(&out.stdout));
}}
if !out.stderr.is_empty() {{
eprintln!(\"stderr:\");
eprintln!(\"{{}}\", String::from_utf8_lossy(&out.stderr));
}}
ExitCode::FAILURE
}} else {{
ExitCode::SUCCESS
Expand Down
4 changes: 4 additions & 0 deletions tests/rustdoc-ui/doctest/edition-2024-error-output.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ test $DIR/edition-2024-error-output.rs - (line 12) ... FAILED
failures:

---- $DIR/edition-2024-error-output.rs - (line 12) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at $TMP:6:1:
assertion `left == right` failed
Expand All @@ -13,6 +16,7 @@ assertion `left == right` failed
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace



failures:
$DIR/edition-2024-error-output.rs - (line 12)

Expand Down
26 changes: 26 additions & 0 deletions tests/rustdoc-ui/doctest/stdout-and-stderr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This test ensures that the output is correctly generated when the
// doctest fails. It checks when there is stderr and stdout, no stdout
// and no stderr/stdout.
//
// This is a regression test for <https://github.com/rust-lang/rust/issues/140289>.

//@ edition: 2024
//@ compile-flags:--test --test-args=--test-threads=1
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ normalize-stdout: "panicked at .+rs:" -> "panicked at $$TMP:"
//@ failure-status: 101
//@ rustc-env:RUST_BACKTRACE=0

//! ```
//! println!("######## from a DOC TEST ########");
//! assert_eq!("doc", "test");
//! ```
//!
//! ```
//! assert_eq!("doc", "test");
//! ```
//!
//! ```
//! std::process::exit(1);
//! ```
46 changes: 46 additions & 0 deletions tests/rustdoc-ui/doctest/stdout-and-stderr.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

running 3 tests
test $DIR/stdout-and-stderr.rs - (line 15) ... FAILED
test $DIR/stdout-and-stderr.rs - (line 20) ... FAILED
test $DIR/stdout-and-stderr.rs - (line 24) ... FAILED

failures:

---- $DIR/stdout-and-stderr.rs - (line 15) stdout ----
Test executable failed (exit status: 101).

stdout:
######## from a DOC TEST ########

stderr:

thread 'main' panicked at $TMP:7:1:
assertion `left == right` failed
left: "doc"
right: "test"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


---- $DIR/stdout-and-stderr.rs - (line 20) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at $TMP:15:1:
assertion `left == right` failed
left: "doc"
right: "test"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


---- $DIR/stdout-and-stderr.rs - (line 24) stdout ----
Test executable failed (exit status: 1).


failures:
$DIR/stdout-and-stderr.rs - (line 15)
$DIR/stdout-and-stderr.rs - (line 20)
$DIR/stdout-and-stderr.rs - (line 24)

test result: FAILED. 0 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

Loading