@@ -48,6 +48,127 @@ fn basic() {
48
48
assert ! ( p. build_dir( ) . join( "doc/src/ex/ex.rs.html" ) . exists( ) ) ;
49
49
}
50
50
51
+ // This test ensures that even if there is no `[workspace]` in the top-level `Cargo.toml` file, the
52
+ // dependencies will get their examples scraped and that they appear in the generated documentation.
53
+ #[ cargo_test( nightly, reason = "rustdoc scrape examples flags are unstable" ) ]
54
+ fn without_workspace ( ) {
55
+ let p = project ( )
56
+ . file (
57
+ "Cargo.toml" ,
58
+ r#"
59
+ [package]
60
+ name = "foo"
61
+ version = "0.0.1"
62
+ edition = "2021"
63
+ authors = []
64
+
65
+ [dependencies]
66
+ a = { path = "crates/a" }
67
+ b = { path = "crates/b" }
68
+ "# ,
69
+ )
70
+ . file ( "src/lib.rs" , "pub use a::*;\n pub use b::*;" )
71
+ // Examples
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
+ . file (
81
+ "examples/two.rs" ,
82
+ r#"use foo::*;
83
+ fn main() {
84
+ let foo: Foo = Foo::new("yes".into());
85
+ let bar = Bar { amount: 1200. };
86
+ println!("{}", bar.pa());
87
+ }"# ,
88
+ )
89
+ // `a` crate
90
+ . file (
91
+ "crates/a/Cargo.toml" ,
92
+ r#"
93
+ [package]
94
+ name = "a"
95
+ version = "0.0.1"
96
+ authors = []
97
+ "# ,
98
+ )
99
+ . file (
100
+ "crates/a/src/lib.rs" ,
101
+ r#"
102
+ #[derive(Debug)]
103
+ pub struct Foo {
104
+ foo: String,
105
+ yes: bool,
106
+ }
107
+
108
+ impl Foo {
109
+ pub fn new(foo: String) -> Self {
110
+ Self { foo, yes: true }
111
+ }
112
+
113
+ pub fn maybe(&self) {
114
+ if self.yes {
115
+ println!("{}", self.foo)
116
+ }
117
+ }
118
+ }"# ,
119
+ )
120
+ // `b` crate
121
+ . file (
122
+ "crates/b/Cargo.toml" ,
123
+ r#"
124
+ [package]
125
+ name = "b"
126
+ version = "0.0.1"
127
+ authors = []
128
+ "# ,
129
+ )
130
+ . file (
131
+ "crates/b/src/lib.rs" ,
132
+ r#"
133
+ #[derive(Debug)]
134
+ pub struct Bar {
135
+ /// pressure in Pascal
136
+ pub amount: f32,
137
+ }
138
+
139
+ impl Bar {
140
+ pub fn pa(&self) -> f32 {
141
+ self.amount
142
+ }
143
+ }"# ,
144
+ )
145
+ . build ( ) ;
146
+
147
+ p. cargo ( "doc -Zunstable-options -Zrustdoc-scrape-examples --no-deps" )
148
+ . masquerade_as_nightly_cargo ( & [ "rustdoc-scrape-examples" ] )
149
+ . with_stderr_unordered (
150
+ "\
151
+ [CHECKING] a v0.0.1 ([CWD]/crates/a)
152
+ [CHECKING] b v0.0.1 ([CWD]/crates/b)
153
+ [CHECKING] foo v0.0.1 ([CWD])
154
+ [SCRAPING] foo v0.0.1 ([CWD])
155
+ [DOCUMENTING] foo v0.0.1 ([CWD])
156
+ [FINISHED] [..]
157
+ [GENERATED] [CWD]/target/doc/foo/index.html" ,
158
+ )
159
+ . run ( ) ;
160
+
161
+ let doc_html = p. read_file ( "target/doc/foo/struct.Foo.html" ) ;
162
+ assert ! ( doc_html. contains( "Examples found in repository" ) ) ;
163
+ // `Foo` is used in the two examples so since there are more than one example, we should have
164
+ // this sentence.
165
+ assert ! ( doc_html. contains( "More examples" ) ) ;
166
+ let doc_html = p. read_file ( "target/doc/foo/struct.Bar.html" ) ;
167
+ assert ! ( doc_html. contains( "Examples found in repository" ) ) ;
168
+ // There is only only usage of `Bar` so we shouldn't have this sentence in the docs.
169
+ assert ! ( !doc_html. contains( "More examples" ) ) ;
170
+ }
171
+
51
172
#[ cargo_test( nightly, reason = "rustdoc scrape examples flags are unstable" ) ]
52
173
fn avoid_build_script_cycle ( ) {
53
174
let p = project ( )
0 commit comments