Skip to content

Commit

Permalink
Remove outdated check on scrape units, add test for doc = false
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Nov 27, 2022
1 parent 125c6b4 commit 968caae
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
9 changes: 0 additions & 9 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,6 @@ pub fn create_bcx<'a, 'cfg>(
has_dev_units,
)?;

// The set of scraped targets should be a strict subset of the set of documented targets,
// except in the special case of examples targets.
if cfg!(debug_assertions) {
let valid_targets = units.iter().map(|u| &u.target).collect::<HashSet<_>>();
for unit in &all_units {
assert!(unit.target.is_example() || valid_targets.contains(&unit.target));
}
}

let valid_units = all_units
.into_iter()
.filter(|unit| {
Expand Down
63 changes: 62 additions & 1 deletion tests/testsuite/docscrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn configure_target() {
)
.build();

p.cargo("doc --lib --bins -Zunstable-options -Zrustdoc-scrape-examples")
p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples")
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
.run();

Expand Down Expand Up @@ -519,3 +519,64 @@ fn use_dev_deps_if_explicitly_enabled() {
)
.run();
}

#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")]
fn only_scrape_documented_targets() {
// package bar has doc = false and should not be eligible for documtation.
let run_with_config = |config: &str, should_scrape: bool| {
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
[lib]
{config}
[workspace]
members = ["foo"]
[dependencies]
foo = {{ path = "foo" }}
"#
),
)
.file("src/lib.rs", "pub fn bar() { foo::foo(); }")
.file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#,
)
.file("foo/src/lib.rs", "pub fn foo() {}")
.build();

p.cargo("doc --workspace -Zunstable-options -Zrustdoc-scrape-examples")
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
.run();

let doc_html = p.read_file("target/doc/foo/fn.foo.html");
let example_found = doc_html.contains("Examples found in repository");
if should_scrape {
assert!(example_found);
} else {
assert!(!example_found);
}
};

// By default, bar should be scraped.
run_with_config("", true);
// If bar isn't supposed to be documented, then it is not eligible
// for scraping.
run_with_config("doc = false", false);
// But if the user explicitly says bar should be scraped, then it should
// be scraped.
run_with_config("doc = false\ndoc-scrape-examples = true", true);
}

0 comments on commit 968caae

Please sign in to comment.