Skip to content

Commit 0fce468

Browse files
Rollup merge of #86230 - GuillaumeGomez:nocapture, r=camelid
Add --nocapture option to rustdoc Fixes #26309. Fixes #45724. Once this PR is merged, I'll send a PR to cargo to also pass `--nocapture` to rustdoc. cc `@jyn514` r? `@camelid`
2 parents 83f0822 + d5e3294 commit 0fce468

11 files changed

+86
-1
lines changed

src/doc/rustdoc/src/command-line-arguments.md

+7
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,10 @@ This flag is **deprecated** and **has no effect**.
417417
Rustdoc only supports Rust source code and Markdown input formats. If the
418418
file ends in `.md` or `.markdown`, `rustdoc` treats it as a Markdown file.
419419
Otherwise, it assumes that the input file is Rust.
420+
421+
## `--nocapture`
422+
423+
When this flag is used with `--test`, the output (stdout and stderr) of your tests won't be
424+
captured by rustdoc. Instead, the output will be directed to your terminal,
425+
as if you had run the test executable manually. This is especially useful
426+
for debugging your tests!

src/librustdoc/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ crate struct Options {
156156
crate run_check: bool,
157157
/// Whether doctests should emit unused externs
158158
crate json_unused_externs: bool,
159+
/// Whether to skip capturing stdout and stderr of tests.
160+
crate nocapture: bool,
159161
}
160162

161163
impl fmt::Debug for Options {
@@ -199,6 +201,7 @@ impl fmt::Debug for Options {
199201
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
200202
.field("run_check", &self.run_check)
201203
.field("no_run", &self.no_run)
204+
.field("nocapture", &self.nocapture)
202205
.finish()
203206
}
204207
}
@@ -627,6 +630,7 @@ impl Options {
627630
let run_check = matches.opt_present("check");
628631
let generate_redirect_map = matches.opt_present("generate-redirect-map");
629632
let show_type_layout = matches.opt_present("show-type-layout");
633+
let nocapture = matches.opt_present("nocapture");
630634

631635
let (lint_opts, describe_lints, lint_cap, _) =
632636
get_cmd_lint_options(matches, error_format, &debugging_opts);
@@ -665,6 +669,7 @@ impl Options {
665669
test_builder,
666670
run_check,
667671
no_run,
672+
nocapture,
668673
render_options: RenderOptions {
669674
output,
670675
external_html,

src/librustdoc/doctest.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
107107

108108
let mut test_args = options.test_args.clone();
109109
let display_warnings = options.display_warnings;
110+
let nocapture = options.nocapture;
110111
let externs = options.externs.clone();
111112
let json_unused_externs = options.json_unused_externs;
112113

@@ -166,6 +167,9 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
166167
};
167168

168169
test_args.insert(0, "rustdoctest".to_string());
170+
if nocapture {
171+
test_args.push("--nocapture".to_string());
172+
}
169173

170174
test::test_main(&test_args, tests, Some(test::Options::new().display_output(display_warnings)));
171175

@@ -456,7 +460,16 @@ fn run_test(
456460
cmd.current_dir(run_directory);
457461
}
458462

459-
match cmd.output() {
463+
let result = if options.nocapture {
464+
cmd.status().map(|status| process::Output {
465+
status,
466+
stdout: Vec::new(),
467+
stderr: Vec::new(),
468+
})
469+
} else {
470+
cmd.output()
471+
};
472+
match result {
460473
Err(e) => return Err(TestFailure::ExecutionError(e)),
461474
Ok(out) => {
462475
if should_panic && out.status.success() {

src/librustdoc/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ fn opts() -> Vec<RustcOptGroup> {
604604
unstable("show-type-layout", |o| {
605605
o.optflagmulti("", "show-type-layout", "Include the memory layout of types in the docs")
606606
}),
607+
unstable("nocapture", |o| {
608+
o.optflag("", "nocapture", "Don't capture stdout and stderr of tests")
609+
}),
607610
]
608611
}
609612

src/librustdoc/markdown.rs

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ crate fn test(mut options: Options) -> Result<(), String> {
136136
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
137137

138138
options.test_args.insert(0, "rustdoctest".to_string());
139+
if options.nocapture {
140+
options.test_args.push("--nocapture".to_string());
141+
}
139142
test::test_main(
140143
&options.test_args,
141144
collector.tests,

src/test/rustdoc-ui/nocapture-fail.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
// compile-flags:--test -Zunstable-options --nocapture
3+
// normalize-stderr-test: "src/test/rustdoc-ui" -> "$$DIR"
4+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
5+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
7+
/// ```compile_fail
8+
/// fn foo() {
9+
/// Input: 123
10+
/// }
11+
/// ```
12+
pub struct Foo;
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: struct literal body without path
2+
--> $DIR/nocapture-fail.rs:8:10
3+
|
4+
LL | fn foo() {
5+
| __________^
6+
LL | | Input: 123
7+
LL | | }
8+
| |_^
9+
|
10+
help: you might have forgotten to add the struct literal inside the block
11+
|
12+
LL | fn foo() { SomeStruct {
13+
LL | Input: 123
14+
LL | } }
15+
|
16+
17+
error: aborting due to previous error
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/nocapture-fail.rs - Foo (line 7) - compile fail ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

src/test/rustdoc-ui/nocapture.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check-pass
2+
// compile-flags:--test -Zunstable-options --nocapture
3+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
4+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
5+
6+
/// ```
7+
/// println!("hello!");
8+
/// eprintln!("stderr");
9+
/// ```
10+
pub struct Foo;

src/test/rustdoc-ui/nocapture.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stderr

src/test/rustdoc-ui/nocapture.stdout

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
running 1 test
3+
hello!
4+
test $DIR/nocapture.rs - Foo (line 6) ... ok
5+
6+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
7+

0 commit comments

Comments
 (0)