From fb1f8de14bf1fc410fcab7905776125c851d872a Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 12 Jul 2024 10:21:55 +0200 Subject: [PATCH 1/2] support #ignore = reason for test_runtimes macro --- .../integration-tests/procedural/src/lib.rs | 36 ++++++++++++++++-- .../integration-tests/procedural/tests/lib.rs | 12 ++++++ runtime/integration-tests/src/lib.rs | 37 +++++++++++++++---- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/runtime/integration-tests/procedural/src/lib.rs b/runtime/integration-tests/procedural/src/lib.rs index da8252ffd9..6230bfc039 100644 --- a/runtime/integration-tests/procedural/src/lib.rs +++ b/runtime/integration-tests/procedural/src/lib.rs @@ -1,6 +1,6 @@ use proc_macro::TokenStream; use quote::quote; -use syn::{parse_macro_input, Expr, ItemFn}; +use syn::{parse_macro_input, punctuated::Punctuated, Expr, ItemFn, Token}; /// Test the function against different runtimes /// @@ -22,6 +22,21 @@ use syn::{parse_macro_input, Expr, ItemFn}; /// } /// ``` /// +/// You can also ignore them: +/// ```rust,ignore +/// use crate::generic::config::Runtime; +/// +/// #[test_runtimes([development, altair, centrifuge], ignore = "reason")] +/// fn foo { +/// // Your test here... +/// } +/// +/// #[test_runtimes(all, ignore = "reason")] +/// fn foo { +/// // Your test here... +/// } +/// ``` +/// /// You can test for fudge support adding the bound: /// ```rust,ignore /// use crate::generic::{config::Runtime, envs::fudge_env::FudgeSupport}; @@ -46,13 +61,26 @@ use syn::{parse_macro_input, Expr, ItemFn}; /// - The world `all`. #[proc_macro_attribute] pub fn test_runtimes(args: TokenStream, input: TokenStream) -> TokenStream { - let args = parse_macro_input!(args as Expr); - let func = parse_macro_input!(input as ItemFn); + let mut args: Punctuated = + parse_macro_input!(args with Punctuated::parse_terminated); + let runtimes = args + .get(0) + .expect("expect 'all' or a list of runtimes") + .clone(); + + let ignore = args.get(1).clone(); + + let func = parse_macro_input!(input as ItemFn); let func_name = &func.sig.ident; + let test_for_runtimes = match ignore { + Some(ignore) => quote!(crate::__test_for_runtimes!(#runtimes, #func_name, #ignore);), + None => quote!(crate::__test_for_runtimes!(#runtimes, #func_name);), + }; + quote! { - crate::__test_for_runtimes!(#args, #func_name); + #test_for_runtimes #func } .into() diff --git a/runtime/integration-tests/procedural/tests/lib.rs b/runtime/integration-tests/procedural/tests/lib.rs index ff5928ed13..84cb14b7b8 100644 --- a/runtime/integration-tests/procedural/tests/lib.rs +++ b/runtime/integration-tests/procedural/tests/lib.rs @@ -1,3 +1,9 @@ +// NOTE: to show the output during the compilation ensure this file is modified +// before compiling and the feature is enabled: +// Steps: +// 1. touch some file from this crate +// 2. `cargo test -p runtime-integration-tests-proc-macro -F debug-proc-macros` + #![allow(unused)] #![cfg(feature = "debug-proc-macros")] @@ -9,3 +15,9 @@ fn macro_runtimes() {} #[__dbg_test_runtimes([development, altair, centrifuge])] fn macro_runtimes_list() {} + +#[__dbg_test_runtimes(all, ignore = "reason")] +fn macro_runtimes_ignored() {} + +#[__dbg_test_runtimes([development, altair, centrifuge], ignore = "reason")] +fn macro_runtimes_list_ignored() {} diff --git a/runtime/integration-tests/src/lib.rs b/runtime/integration-tests/src/lib.rs index 459781a7e1..4df51a310c 100644 --- a/runtime/integration-tests/src/lib.rs +++ b/runtime/integration-tests/src/lib.rs @@ -37,7 +37,18 @@ mod impls; /// NOTE: Do not use it direclty, use `#[test_runtimes]` proc macro instead #[macro_export] macro_rules! __test_for_runtimes { - ( [ $($runtime_name:ident),* ], $test_name:ident ) => { + ( [ $($runtime_name:ident),* ], $test_name:ident $(, $ignore:meta)?) => { + // We need here an extra macro to unfold ignore as `?` inside a repetition of runtimes + macro_rules! __test_for_runtime { + ( $runtime_name_nested:ident) => { + #[tokio::test] + $(#[$ignore])? + async fn $runtime_name_nested() { + $test_name::<$runtime_name_nested::Runtime>() + } + }; + } + #[cfg(test)] mod $test_name { use super::*; @@ -52,14 +63,26 @@ macro_rules! __test_for_runtimes { use centrifuge_runtime as centrifuge; $( - #[tokio::test] - async fn $runtime_name() { - $test_name::<$runtime_name::Runtime>() - } + __test_for_runtime!($runtime_name); )* } }; - ( all , $test_name:ident ) => { - $crate::__test_for_runtimes!([development, altair, centrifuge], $test_name); + ( all, $test_name:ident $(, $ignore:meta)?) => { + $crate::__test_for_runtimes!([development, altair, centrifuge], $test_name $(, $ignore)?); }; } + +#[cfg(test)] +mod test_for_runtimes_macro_checks { + fn foo1() {} + fn foo2() {} + fn foo3() {} + fn foo4() {} + fn foo5() {} + + __test_for_runtimes!([development], foo1); + __test_for_runtimes!([development, altair, centrifuge], foo2); + __test_for_runtimes!(all, foo3); + __test_for_runtimes!([development], foo4, ignore = "ignored correctly"); + __test_for_runtimes!(all, foo5, ignore = "ignored correctly"); +} From 482faf5e559f160779a44b0ad784172896c2a562 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 12 Jul 2024 13:53:32 +0200 Subject: [PATCH 2/2] fix clippy --- runtime/integration-tests/procedural/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/integration-tests/procedural/src/lib.rs b/runtime/integration-tests/procedural/src/lib.rs index 6230bfc039..a8a4e8408e 100644 --- a/runtime/integration-tests/procedural/src/lib.rs +++ b/runtime/integration-tests/procedural/src/lib.rs @@ -69,7 +69,7 @@ pub fn test_runtimes(args: TokenStream, input: TokenStream) -> TokenStream { .expect("expect 'all' or a list of runtimes") .clone(); - let ignore = args.get(1).clone(); + let ignore = args.get(1); let func = parse_macro_input!(input as ItemFn); let func_name = &func.sig.ident;