Skip to content

Commit

Permalink
IT: Support #ignore = reason for test_runtimes macro (#1908)
Browse files Browse the repository at this point in the history
* support #ignore = reason for test_runtimes macro

* fix clippy
  • Loading branch information
lemunozm authored Jul 12, 2024
1 parent 2c1be11 commit 2169f6f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
36 changes: 32 additions & 4 deletions runtime/integration-tests/procedural/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand All @@ -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<T: Runtime> {
/// // Your test here...
/// }
///
/// #[test_runtimes(all, ignore = "reason")]
/// fn foo<T: Runtime> {
/// // Your test here...
/// }
/// ```
///
/// You can test for fudge support adding the bound:
/// ```rust,ignore
/// use crate::generic::{config::Runtime, envs::fudge_env::FudgeSupport};
Expand All @@ -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<Expr, Token![,]> =
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);

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()
Expand Down
12 changes: 12 additions & 0 deletions runtime/integration-tests/procedural/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -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")]

Expand All @@ -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() {}
37 changes: 30 additions & 7 deletions runtime/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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<T: crate::config::Runtime>() {}
fn foo2<T: crate::config::Runtime>() {}
fn foo3<T: crate::config::Runtime>() {}
fn foo4<T: crate::config::Runtime>() {}
fn foo5<T: crate::config::Runtime>() {}

__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");
}

0 comments on commit 2169f6f

Please sign in to comment.