Skip to content

Commit

Permalink
Add regression test for scraped examples in crates without [workspace]
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 30, 2023
1 parent ce8dc50 commit 3c1aa55
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions tests/testsuite/docscrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,127 @@ fn basic() {
assert!(p.build_dir().join("doc/src/ex/ex.rs.html").exists());
}

// This test ensures that even if there is no `[workspace]` in the top-level `Cargo.toml` file, the
// dependencies will get their examples scraped and that they appear in the generated documentation.
#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")]
fn without_workspace() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2021"
authors = []
[dependencies]
a = { path = "crates/a" }
b = { path = "crates/b" }
"#,
)
.file("src/lib.rs", "pub use a::*;\npub use b::*;")
// Examples
.file(
"examples/one.rs",
r#"use foo::*;
fn main() {
let foo = Foo::new("yes".into());
foo.maybe();
}"#,
)
.file(
"examples/two.rs",
r#"use foo::*;
fn main() {
let foo: Foo = Foo::new("yes".into());
let bar = Bar { amount: 1200. };
println!("{}", bar.pa());
}"#,
)
// `a` crate
.file(
"crates/a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
authors = []
"#,
)
.file(
"crates/a/src/lib.rs",
r#"
#[derive(Debug)]
pub struct Foo {
foo: String,
yes: bool,
}
impl Foo {
pub fn new(foo: String) -> Self {
Self { foo, yes: true }
}
pub fn maybe(&self) {
if self.yes {
println!("{}", self.foo)
}
}
}"#,
)
// `b` crate
.file(
"crates/b/Cargo.toml",
r#"
[package]
name = "b"
version = "0.0.1"
authors = []
"#,
)
.file(
"crates/b/src/lib.rs",
r#"
#[derive(Debug)]
pub struct Bar {
/// pressure in Pascal
pub amount: f32,
}
impl Bar {
pub fn pa(&self) -> f32 {
self.amount
}
}"#,
)
.build();

p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples --no-deps")
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
.with_stderr_unordered(
"\
[CHECKING] a v0.0.1 ([CWD]/crates/a)
[CHECKING] b v0.0.1 ([CWD]/crates/b)
[CHECKING] foo v0.0.1 ([CWD])
[SCRAPING] foo v0.0.1 ([CWD])
[DOCUMENTING] foo v0.0.1 ([CWD])
[FINISHED] [..]
[GENERATED] [CWD]/target/doc/foo/index.html",
)
.run();

let doc_html = p.read_file("target/doc/foo/struct.Foo.html");
assert!(doc_html.contains("Examples found in repository"));
// `Foo` is used in the two examples so since there are more than one example, we should have
// this sentence.
assert!(doc_html.contains("More examples"));
let doc_html = p.read_file("target/doc/foo/struct.Bar.html");
assert!(doc_html.contains("Examples found in repository"));
// There is only only usage of `Bar` so we shouldn't have this sentence in the docs.
assert!(!doc_html.contains("More examples"));
}

#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")]
fn avoid_build_script_cycle() {
let p = project()
Expand Down

0 comments on commit 3c1aa55

Please sign in to comment.