From 9f0fe18dfc58a45a8edacdc774fa2fdc09e3df12 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Tue, 30 Nov 2021 17:46:50 +0100 Subject: [PATCH] fix async_test for non-wasm32 envs --- .../src/verification/machine.rs | 2 +- crates/matrix-sdk-test-macros/src/lib.rs | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/verification/machine.rs b/crates/matrix-sdk-crypto/src/verification/machine.rs index 90c303ef5..2560ce677 100644 --- a/crates/matrix-sdk-crypto/src/verification/machine.rs +++ b/crates/matrix-sdk-crypto/src/verification/machine.rs @@ -582,7 +582,7 @@ mod test { } #[async_test] - fn create() { + async fn create() { let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id()); let identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(alice_id()))); let store = MemoryStore::new(); diff --git a/crates/matrix-sdk-test-macros/src/lib.rs b/crates/matrix-sdk-test-macros/src/lib.rs index 25d2730d9..82e090a28 100644 --- a/crates/matrix-sdk-test-macros/src/lib.rs +++ b/crates/matrix-sdk-test-macros/src/lib.rs @@ -5,11 +5,11 @@ use quote::{quote, format_ident}; use syn; /// Attribute to use `wasm_bindgen_test` for wasm32 targets and `tokio::test` -/// for everything else +/// for everything else with async-support and custom result-tyupes #[proc_macro_attribute] pub fn async_test(_attr: TokenStream, item: TokenStream) -> TokenStream { - let fun = parse_macro_input!(item as syn::ItemFn); + // on the regular return-case, we can just use cfg_attr and quit early if fun.sig.output == syn::ReturnType::Default { let attrs = r#" #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] @@ -22,6 +22,13 @@ pub fn async_test(_attr: TokenStream, item: TokenStream) -> TokenStream { return out } + // on the more complicated case, where we have some `->`-Result-Arrow + // `wasm_bindgen_test` doesn't yet support + // - see https://github.com/rustwasm/wasm-bindgen/issues/2565 - + // we split the attribution into to functions: one with the original + // to be attributed for non-wasm, and then a second outer-wrapper function + // that calls the first in wasm32 cases. + let attrs = r#" #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] "#; @@ -52,7 +59,8 @@ pub fn async_test(_attr: TokenStream, item: TokenStream) -> TokenStream { out.extend(inner); let attrs = r#" - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg(target_arch = "wasm32")] + #[wasm_bindgen_test::wasm_bindgen_test] "#; let outer_attrs: TokenStream = attrs.parse().expect("Static works."); let of : TokenStream = outer.into_token_stream().into(); @@ -61,10 +69,3 @@ pub fn async_test(_attr: TokenStream, item: TokenStream) -> TokenStream { out } - -// Attribute to use `wasm_bindgen_test` for wasm32 targets and `tokio::test` -// for everything else -// #[cfg(not(target_arch = "wasm32"))] -// #[proc_macro_attribute] -// pub fn async_test(_attr: TokenStream, item: TokenStream) -> TokenStream { -// }