Skip to content

Commit 968caae

Browse files
committed
Remove outdated check on scrape units, add test for doc = false
1 parent 125c6b4 commit 968caae

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

src/cargo/ops/cargo_compile/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,6 @@ pub fn create_bcx<'a, 'cfg>(
387387
has_dev_units,
388388
)?;
389389

390-
// The set of scraped targets should be a strict subset of the set of documented targets,
391-
// except in the special case of examples targets.
392-
if cfg!(debug_assertions) {
393-
let valid_targets = units.iter().map(|u| &u.target).collect::<HashSet<_>>();
394-
for unit in &all_units {
395-
assert!(unit.target.is_example() || valid_targets.contains(&unit.target));
396-
}
397-
}
398-
399390
let valid_units = all_units
400391
.into_iter()
401392
.filter(|unit| {

tests/testsuite/docscrape.rs

+62-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn configure_target() {
196196
)
197197
.build();
198198

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

@@ -519,3 +519,64 @@ fn use_dev_deps_if_explicitly_enabled() {
519519
)
520520
.run();
521521
}
522+
523+
#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")]
524+
fn only_scrape_documented_targets() {
525+
// package bar has doc = false and should not be eligible for documtation.
526+
let run_with_config = |config: &str, should_scrape: bool| {
527+
let p = project()
528+
.file(
529+
"Cargo.toml",
530+
&format!(
531+
r#"
532+
[package]
533+
name = "bar"
534+
version = "0.0.1"
535+
authors = []
536+
537+
[lib]
538+
{config}
539+
540+
[workspace]
541+
members = ["foo"]
542+
543+
[dependencies]
544+
foo = {{ path = "foo" }}
545+
"#
546+
),
547+
)
548+
.file("src/lib.rs", "pub fn bar() { foo::foo(); }")
549+
.file(
550+
"foo/Cargo.toml",
551+
r#"
552+
[package]
553+
name = "foo"
554+
version = "0.0.1"
555+
authors = []
556+
"#,
557+
)
558+
.file("foo/src/lib.rs", "pub fn foo() {}")
559+
.build();
560+
561+
p.cargo("doc --workspace -Zunstable-options -Zrustdoc-scrape-examples")
562+
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
563+
.run();
564+
565+
let doc_html = p.read_file("target/doc/foo/fn.foo.html");
566+
let example_found = doc_html.contains("Examples found in repository");
567+
if should_scrape {
568+
assert!(example_found);
569+
} else {
570+
assert!(!example_found);
571+
}
572+
};
573+
574+
// By default, bar should be scraped.
575+
run_with_config("", true);
576+
// If bar isn't supposed to be documented, then it is not eligible
577+
// for scraping.
578+
run_with_config("doc = false", false);
579+
// But if the user explicitly says bar should be scraped, then it should
580+
// be scraped.
581+
run_with_config("doc = false\ndoc-scrape-examples = true", true);
582+
}

0 commit comments

Comments
 (0)