Skip to content
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

Add --nocapture option to rustdoc #86230

Merged
merged 5 commits into from
Jul 19, 2021
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
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());
}
camelid marked this conversation as resolved.
Show resolved Hide resolved
test::test_main(
&options.test_args,
collector.tests,
Expand Down
12 changes: 12 additions & 0 deletions src/test/rustdoc-ui/nocapture-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// check-pass
// compile-flags:--test -Zunstable-options --nocapture
// normalize-stderr-test: "src/test/rustdoc-ui" -> "$$DIR"
// 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:8: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 7) - 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