Skip to content

Commit

Permalink
Auto merge of rust-lang#83857 - ABouttefeux:master, r=jyn514
Browse files Browse the repository at this point in the history
added --no-run option for rustdoc

resolve rust-lang#59053

add `--no-run` option for `rustdoc` for compiling doc test but not running them.
Intended for use with `--persist-doctests`.
  • Loading branch information
bors committed May 1, 2021
2 parents 603a42e + 03c710b commit 5f304a5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ crate struct Options {
/// For example, using ignore-foo to ignore running the doctest on any target that
/// contains "foo" as a substring
crate enable_per_target_ignores: bool,
/// Do not run doctests, compile them if should_test is active.
crate no_run: bool,

/// The path to a rustc-like binary to build tests with. If not set, we
/// default to loading from `$sysroot/bin/rustc`.
Expand Down Expand Up @@ -197,6 +199,7 @@ impl fmt::Debug for Options {
.field("runtool_args", &self.runtool_args)
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
.field("run_check", &self.run_check)
.field("no_run", &self.no_run)
.finish()
}
}
Expand Down Expand Up @@ -466,6 +469,12 @@ impl Options {
test_args.iter().flat_map(|s| s.split_whitespace()).map(|s| s.to_string()).collect();

let should_test = matches.opt_present("test");
let no_run = matches.opt_present("no-run");

if !should_test && no_run {
diag.err("the `--test` flag must be passed to enable `--no-run`");
return Err(1);
}

let output =
matches.opt_str("o").map(|s| PathBuf::from(&s)).unwrap_or_else(|| PathBuf::from("doc"));
Expand Down Expand Up @@ -666,6 +675,7 @@ impl Options {
enable_per_target_ignores,
test_builder,
run_check,
no_run,
render_options: RenderOptions {
output,
external_html,
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,13 +940,14 @@ impl Tester for Collector {
let report_unused_externs = |uext| {
unused_externs.lock().unwrap().push(uext);
};
let no_run = config.no_run || options.no_run;
let res = run_test(
&test,
&cratename,
line,
options,
config.should_panic,
config.no_run,
no_run,
config.test_harness,
runtool,
runtool_args,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ fn opts() -> Vec<RustcOptGroup> {
"[unversioned-shared-resources,toolchain-shared-resources,invocation-specific]",
)
}),
unstable("no-run", |o| o.optflag("", "no-run", "Compile doctests without running them")),
]
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/no-run-flag-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// test the behavior of the --no-run flag without the --test flag

// compile-flags:-Z unstable-options --no-run --test-args=--test-threads=1
// error-pattern: the `--test` flag must be passed

pub fn f() {}
2 changes: 2 additions & 0 deletions src/test/rustdoc-ui/no-run-flag-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: the `--test` flag must be passed to enable `--no-run`

38 changes: 38 additions & 0 deletions src/test/rustdoc-ui/no-run-flag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// test the behavior of the --no-run flag

// check-pass
// compile-flags:-Z unstable-options --test --no-run --test-args=--test-threads=1
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"

/// ```
/// let a = true;
/// ```
/// ```should_panic
/// panic!()
/// ```
/// ```ignore (incomplete-code)
/// fn foo() {
/// ```
/// ```no_run
/// loop {
/// println!("Hello, world");
/// }
/// ```
/// fails to compile
/// ```compile_fail
/// let x = 5;
/// x += 2; // shouldn't compile!
/// ```
/// Ok the test does not run
/// ```
/// panic!()
/// ```
/// Ok the test does not run
/// ```should_panic
/// loop {
/// println!("Hello, world");
/// panic!()
/// }
/// ```
pub fn f() {}
12 changes: 12 additions & 0 deletions src/test/rustdoc-ui/no-run-flag.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

running 7 tests
test $DIR/no-run-flag.rs - f (line 11) ... ok
test $DIR/no-run-flag.rs - f (line 14) ... ignored
test $DIR/no-run-flag.rs - f (line 17) ... ok
test $DIR/no-run-flag.rs - f (line 23) ... ok
test $DIR/no-run-flag.rs - f (line 28) ... ok
test $DIR/no-run-flag.rs - f (line 32) ... ok
test $DIR/no-run-flag.rs - f (line 8) ... ok

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

0 comments on commit 5f304a5

Please sign in to comment.