Rollup merge of #93497 - willcrichton:rustdoc-scrape-test, r=GuillaumeGomez

Pass `--test` flag through rustdoc to rustc so `#[test]` functions can be scraped

As a part of stabilizing the scrape examples extension in Cargo, I uncovered a bug where examples cannot be scraped from tests. See this test: https://github.com/rust-lang/cargo/pull/10343/files#diff-27aa4f012ebfebaaee61498d91d2370de460628405d136b05e77efe61e044679R2496

The issue is that when rustdoc is run on a test file, because `--test` is not passed as a rustc option, then functions annotated with `#[test]` are ignored by the compiler. So this PR changes rustdoc so when `--test` is passed in conjunction with a `--scrape-example-<suffix>` flag, then the `test` field of `rustc_interface::Config` is true.

r? `@camelid`
This commit is contained in:
Matthias Krüger
2022-02-18 23:23:05 +01:00
committed by GitHub
8 changed files with 37 additions and 5 deletions

View File

@@ -34,6 +34,7 @@ use std::path::PathBuf;
crate struct ScrapeExamplesOptions {
output_path: PathBuf,
target_crates: Vec<String>,
crate scrape_tests: bool,
}
impl ScrapeExamplesOptions {
@@ -43,16 +44,22 @@ impl ScrapeExamplesOptions {
) -> Result<Option<Self>, i32> {
let output_path = matches.opt_str("scrape-examples-output-path");
let target_crates = matches.opt_strs("scrape-examples-target-crate");
match (output_path, !target_crates.is_empty()) {
(Some(output_path), true) => Ok(Some(ScrapeExamplesOptions {
let scrape_tests = matches.opt_present("scrape-tests");
match (output_path, !target_crates.is_empty(), scrape_tests) {
(Some(output_path), true, _) => Ok(Some(ScrapeExamplesOptions {
output_path: PathBuf::from(output_path),
target_crates,
scrape_tests,
})),
(Some(_), false) | (None, true) => {
(Some(_), false, _) | (None, true, _) => {
diag.err("must use --scrape-examples-output-path and --scrape-examples-target-crate together");
Err(1)
}
(None, false) => Ok(None),
(None, false, true) => {
diag.err("must use --scrape-examples-output-path and --scrape-examples-target-crate with --scrape-tests");
Err(1)
}
(None, false, false) => Ok(None),
}
}
}