Skip to content

Commit d39d537

Browse files
AdamGSwiktor-k
andauthored
Introduce a #[hidden] attribute to hide the generated macro from docs (#333)
* . * Add hidden attribute to hide public macros from docs * Update rstest_reuse/src/lib.rs Co-authored-by: Wiktor Kwapisiewicz <wiktor@metacode.biz> * Add docs and CR notes * Add docs and tests --------- Co-authored-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent 454df84 commit d39d537

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

rstest_reuse/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ pub use rstest_reuse;
180180

181181
And not just `use rstest_reuse` like in the standard cases.
182182

183+
## `#[hidden]` Attribute
184+
185+
Adds a `#[doc(hidden)]` attribute to the generated macro, hiding it from documentation.
186+
187+
When using `#[export]`, the exported macro would otherwise appear in your public docs with a random suffix (used to prevent name collisions). Using `#[hidden]` keeps your documentation clean, which is especially useful with tools like `cargo-semver-checks`.
188+
183189
## Disclaimer
184190

185191
This crate is in a development stage. I don't know if I'll include it in `rstest` or change some syntax in the future.

rstest_reuse/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ fn get_export(attributes: &[Attribute]) -> Option<&Attribute> {
286286
.find(|&attr| attr.path().is_ident(&format_ident!("export")))
287287
}
288288

289+
fn get_hidden(attributes: &[Attribute]) -> Option<&Attribute> {
290+
attributes
291+
.iter()
292+
.find(|&attr| attr.path().is_ident(&format_ident!("hidden")))
293+
}
294+
289295
/// Define a template where the name is given from the function name. This attribute register all
290296
/// attributes. The function signature don't really mater but to make it clear is better that you
291297
/// use a signature like if you're wrinting a standard `rstest`.
@@ -294,6 +300,9 @@ fn get_export(attributes: &[Attribute]) -> Option<&Attribute> {
294300
/// should annotate it with `#[export]` attribute. This attribute add `#[macro_export]` attribute to
295301
/// the template macro and make possible to use it from another crate.
296302
///
303+
/// If you care about maintaining clean external docs, or use tools like `cargo-semver-check`, you
304+
/// can use the `#[hidden]` attribute to hide the generated macro from external rust docs.
305+
///
297306
/// When define a template you can also set the arguments attributes like `#[case]`, `#[values]`
298307
/// and `#[with]`: when you apply it attributes will be copied to the matched by name arguments.
299308
///
@@ -333,8 +342,19 @@ pub fn template(_args: proc_macro::TokenStream, input: proc_macro::TokenStream)
333342
let macro_name = template.sig.ident.clone();
334343
let macro_name_rand = format_ident!("{}_{}", macro_name, rand::random::<u64>());
335344

345+
let doc = if get_hidden(&attributes).is_some() {
346+
quote! {
347+
#[doc(hidden)]
348+
}
349+
} else {
350+
let docstr = format!("Apply {macro_name} template to given body");
351+
quote! {
352+
#[doc = #docstr]
353+
}
354+
};
355+
336356
let tokens = quote! {
337-
/// Apply #macro_name template to given body
357+
#doc
338358
#macro_attribute
339359
macro_rules! #macro_name_rand {
340360
( $test:item ) => {

rstest_reuse/tests/acceptance.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ fn rstest_reuse_not_in_crate_root() {
190190
TestResults::new().ok("test::case_1").assert(output);
191191
}
192192

193+
#[test]
194+
fn hidden_attribute() {
195+
let (output, _) = run_test("export_hidden_template.rs");
196+
197+
TestResults::new()
198+
.ok("public_test::case_1")
199+
.ok("private_test::case_1")
200+
.ok("foo::test::case_1")
201+
.assert(output);
202+
}
203+
204+
#[test]
205+
fn hidden_deny_docs() {
206+
let (output, _) = run_test("hidden_deny_docs.rs");
207+
208+
TestResults::new()
209+
.ok("public_it_works::case_1")
210+
.ok("public_it_works::case_2")
211+
.ok("private_it_works::case_1")
212+
.ok("private_it_works::case_2")
213+
.assert(output);
214+
}
215+
193216
lazy_static! {
194217
static ref ROOT_DIR: TempDir = TempDir::new(std::env::temp_dir().join("rstest_reuse"), false);
195218
static ref ROOT_PROJECT: Project = Project::new(ROOT_DIR.as_ref());
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use rstest::rstest;
2+
use rstest_reuse::apply;
3+
use rstest_reuse::template;
4+
5+
mod foo {
6+
use rstest::rstest;
7+
use rstest_reuse::apply;
8+
use rstest_reuse::template;
9+
10+
#[template]
11+
#[export]
12+
#[hidden]
13+
#[rstest]
14+
#[case("bar")]
15+
fn public_template(#[case] s: &str) {}
16+
17+
#[apply(public_template)]
18+
fn test(#[case] s: &str) {
19+
assert_eq!(s, "bar");
20+
}
21+
}
22+
23+
use foo::public_template;
24+
25+
#[apply(public_template)]
26+
fn public_test(#[case] s: &str) {
27+
assert_eq!(s, "bar");
28+
}
29+
30+
#[template]
31+
#[hidden]
32+
#[rstest]
33+
#[case("bar")]
34+
fn private_template(#[case] s: &str) {}
35+
36+
#[apply(private_template)]
37+
fn private_test(#[case] s: &str) {
38+
assert_eq!(s, "bar");
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![deny(missing_docs)]
2+
3+
//! We should provide also a docs for the crate itself if we set `#![deny(missing_docs)]`.
4+
5+
use rstest::rstest;
6+
use rstest_reuse::{self, *};
7+
8+
#[template]
9+
#[export]
10+
#[hidden]
11+
#[rstest(a, b, case(2, 2), case(4/2, 2))]
12+
fn public_two_simple_cases(a: u32, b: u32) {}
13+
14+
#[apply(public_two_simple_cases)]
15+
fn public_it_works(a: u32, b: u32) {
16+
assert!(a == b);
17+
}
18+
19+
#[template]
20+
#[hidden]
21+
#[rstest(a, b, case(2, 2), case(4/2, 2))]
22+
fn private_two_simple_cases(a: u32, b: u32) {}
23+
24+
#[apply(private_two_simple_cases)]
25+
fn private_it_works(a: u32, b: u32) {
26+
assert!(a == b);
27+
}

0 commit comments

Comments
 (0)