Skip to content

Commit

Permalink
Rollup merge of rust-lang#86230 - GuillaumeGomez:nocapture, r=camelid
Browse files Browse the repository at this point in the history
Add --nocapture option to rustdoc

Fixes rust-lang#26309.
Fixes rust-lang#45724.

Once this PR is merged, I'll send a PR to cargo to also pass `--nocapture` to rustdoc.

cc `@jyn514`
r? `@camelid`
  • Loading branch information
GuillaumeGomez authored Jul 17, 2021
2 parents c7331d6 + f9f6083 commit 6ca154d
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/doc/rustdoc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,10 @@ This flag is **deprecated** and **has no effect**.
Rustdoc only supports Rust source code and Markdown input formats. If the
file ends in `.md` or `.markdown`, `rustdoc` treats it as a Markdown file.
Otherwise, it assumes that the input file is Rust.

## `--nocapture`

When this flag is used with `--test`, the output (stdout and stderr) of your tests won't be
captured by rustdoc. Instead, the output will be directed to your terminal,
as if you had run the test executable manually. This is especially useful
for debugging your tests!
5 changes: 5 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ crate struct Options {
crate run_check: bool,
/// Whether doctests should emit unused externs
crate json_unused_externs: bool,
/// Whether to skip capturing stdout and stderr of tests.
crate nocapture: bool,
}

impl fmt::Debug for Options {
Expand Down Expand Up @@ -199,6 +201,7 @@ impl fmt::Debug for Options {
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
.field("run_check", &self.run_check)
.field("no_run", &self.no_run)
.field("nocapture", &self.nocapture)
.finish()
}
}
Expand Down Expand Up @@ -627,6 +630,7 @@ impl Options {
let run_check = matches.opt_present("check");
let generate_redirect_map = matches.opt_present("generate-redirect-map");
let show_type_layout = matches.opt_present("show-type-layout");
let nocapture = matches.opt_present("nocapture");

let (lint_opts, describe_lints, lint_cap, _) =
get_cmd_lint_options(matches, error_format, &debugging_opts);
Expand Down Expand Up @@ -665,6 +669,7 @@ impl Options {
test_builder,
run_check,
no_run,
nocapture,
render_options: RenderOptions {
output,
external_html,
Expand Down
15 changes: 14 additions & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {

let mut test_args = options.test_args.clone();
let display_warnings = options.display_warnings;
let nocapture = options.nocapture;
let externs = options.externs.clone();
let json_unused_externs = options.json_unused_externs;

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

test_args.insert(0, "rustdoctest".to_string());
if nocapture {
test_args.push("--nocapture".to_string());
}

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

Expand Down Expand Up @@ -456,7 +460,16 @@ fn run_test(
cmd.current_dir(run_directory);
}

match cmd.output() {
let result = if options.nocapture {
cmd.status().map(|status| process::Output {
status,
stdout: Vec::new(),
stderr: Vec::new(),
})
} else {
cmd.output()
};
match result {
Err(e) => return Err(TestFailure::ExecutionError(e)),
Ok(out) => {
if should_panic && out.status.success() {
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ fn opts() -> Vec<RustcOptGroup> {
unstable("show-type-layout", |o| {
o.optflagmulti("", "show-type-layout", "Include the memory layout of types in the docs")
}),
unstable("nocapture", |o| {
o.optflag("", "nocapture", "Don't capture stdout and stderr of tests")
}),
]
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ crate fn test(mut options: Options) -> Result<(), String> {
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);

options.test_args.insert(0, "rustdoctest".to_string());
if options.nocapture {
options.test_args.push("--nocapture".to_string());
}
test::test_main(
&options.test_args,
collector.tests,
Expand Down
11 changes: 11 additions & 0 deletions src/test/rustdoc-ui/nocapture-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass
// compile-flags:--test -Zunstable-options --nocapture
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"

/// ```compile_fail
/// fn foo() {
/// Input: 123
/// }
/// ```
pub struct Foo;
18 changes: 18 additions & 0 deletions src/test/rustdoc-ui/nocapture-fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: struct literal body without path
--> $DIR/nocapture-fail.rs:7:10
|
LL | fn foo() {
| __________^
LL | | Input: 123
LL | | }
| |_^
|
help: you might have forgotten to add the struct literal inside the block
|
LL | fn foo() { SomeStruct {
LL | Input: 123
LL | } }
|

error: aborting due to previous error

6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/nocapture-fail.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

running 1 test
test $DIR/nocapture-fail.rs - Foo (line 6) - compile fail ... ok

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

10 changes: 10 additions & 0 deletions src/test/rustdoc-ui/nocapture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// check-pass
// compile-flags:--test -Zunstable-options --nocapture
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"

/// ```
/// println!("hello!");
/// eprintln!("stderr");
/// ```
pub struct Foo;
1 change: 1 addition & 0 deletions src/test/rustdoc-ui/nocapture.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stderr
7 changes: 7 additions & 0 deletions src/test/rustdoc-ui/nocapture.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

running 1 test
hello!
test $DIR/nocapture.rs - Foo (line 6) ... ok

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

0 comments on commit 6ca154d

Please sign in to comment.