@@ -48,6 +48,85 @@ fn basic() {
4848 assert ! ( p. build_dir( ) . join( "doc/src/ex/ex.rs.html" ) . exists( ) ) ;
4949}
5050
51+ // This test ensures that if there is no `[workspace]` in the top-level `Cargo.toml` file, the
52+ // dependencies will not get their examples scraped and that they appear in the generated
53+ // documentation.
54+ #[ cargo_test( nightly, reason = "-Zrustdoc-scrape-examples is unstable" ) ]
55+ fn scrape_examples_for_non_workspace_reexports ( ) {
56+ let p = project ( )
57+ . file (
58+ "Cargo.toml" ,
59+ r#"
60+ [package]
61+ name = "foo"
62+ version = "0.0.1"
63+ edition = "2021"
64+ authors = []
65+
66+ [dependencies]
67+ a = { path = "crates/a" }
68+ "# ,
69+ )
70+ . file ( "src/lib.rs" , "pub use a::*;" )
71+ // Example
72+ . file (
73+ "examples/one.rs" ,
74+ r#"use foo::*;
75+ fn main() {
76+ let foo = Foo::new("yes".into());
77+ foo.maybe();
78+ }"# ,
79+ )
80+ // `a` crate
81+ . file (
82+ "crates/a/Cargo.toml" ,
83+ r#"
84+ [package]
85+ name = "a"
86+ version = "0.0.1"
87+ authors = []
88+ "# ,
89+ )
90+ . file (
91+ "crates/a/src/lib.rs" ,
92+ r#"
93+ #[derive(Debug)]
94+ pub struct Foo {
95+ foo: String,
96+ yes: bool,
97+ }
98+
99+ impl Foo {
100+ pub fn new(foo: String) -> Self {
101+ Self { foo, yes: true }
102+ }
103+
104+ pub fn maybe(&self) {
105+ if self.yes {
106+ println!("{}", self.foo)
107+ }
108+ }
109+ }"# ,
110+ )
111+ . build ( ) ;
112+
113+ p. cargo ( "doc -Zunstable-options -Zrustdoc-scrape-examples --no-deps" )
114+ . masquerade_as_nightly_cargo ( & [ "rustdoc-scrape-examples" ] )
115+ . with_stderr_unordered (
116+ "\
117+ [CHECKING] a v0.0.1 ([CWD]/crates/a)
118+ [CHECKING] foo v0.0.1 ([CWD])
119+ [SCRAPING] foo v0.0.1 ([CWD])
120+ [DOCUMENTING] foo v0.0.1 ([CWD])
121+ [FINISHED] [..]
122+ [GENERATED] [CWD]/target/doc/foo/index.html" ,
123+ )
124+ . run ( ) ;
125+
126+ let doc_html = p. read_file ( "target/doc/foo/struct.Foo.html" ) ;
127+ assert ! ( !doc_html. contains( "Examples found in repository" ) ) ;
128+ }
129+
51130#[ cargo_test( nightly, reason = "rustdoc scrape examples flags are unstable" ) ]
52131fn avoid_build_script_cycle ( ) {
53132 let p = project ( )
0 commit comments