From c3fefe4050d5bbfd0cf345923fa70b93eaa39278 Mon Sep 17 00:00:00 2001 From: Tom Parker-Shemilt Date: Sun, 31 Jul 2022 23:11:10 +0100 Subject: [PATCH 1/3] Feature to disable the async work --- serial_test/Cargo.toml | 7 +++++-- serial_test/src/lib.rs | 18 ++++++++++-------- serial_test/src/parallel_code_lock.rs | 13 +++++++++---- serial_test/src/parallel_file_lock.rs | 12 ++++++++++-- serial_test/src/serial_code_lock.rs | 2 ++ serial_test_derive/src/lib.rs | 6 ++++++ 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/serial_test/Cargo.toml b/serial_test/Cargo.toml index 23bd311..4c88a01 100644 --- a/serial_test/Cargo.toml +++ b/serial_test/Cargo.toml @@ -19,18 +19,21 @@ document-features = { version = "0.2", optional = true } log = { version = "0.4", optional = true } futures = { version = "^0.3", default_features = false, features = [ "executor", -] } +], optional = true} [dev-dependencies] itertools = "0.10" tokio = { version = "^1.17", features = ["macros", "rt"] } [features] -default = ["logging", "timeout"] +default = ["logging", "timeout", "async"] ## Switches on debug logging (and requires the `log` package) logging = ["log"] +## Enables async features +async = ["futures"] + ## The file_locks feature unlocks the `file_serial`/`file_parallel` macros file_locks = ["fslock"] diff --git a/serial_test/src/lib.rs b/serial_test/src/lib.rs index 28e9abc..8223f1e 100644 --- a/serial_test/src/lib.rs +++ b/serial_test/src/lib.rs @@ -65,14 +65,16 @@ mod serial_file_lock; #[cfg(feature = "timeout")] pub use code_lock::set_max_wait; -pub use parallel_code_lock::{ - local_async_parallel_core, local_async_parallel_core_with_return, local_parallel_core, - local_parallel_core_with_return, -}; -pub use serial_code_lock::{ - local_async_serial_core, local_async_serial_core_with_return, local_serial_core, - local_serial_core_with_return, -}; + +#[cfg(feature = "async")] +pub use parallel_code_lock::{local_async_parallel_core, local_async_parallel_core_with_return}; + +pub use parallel_code_lock::{local_parallel_core, local_parallel_core_with_return}; + +#[cfg(feature = "async")] +pub use serial_code_lock::{local_async_serial_core, local_async_serial_core_with_return}; + +pub use serial_code_lock::{local_serial_core, local_serial_core_with_return}; #[cfg(feature = "file_locks")] pub use serial_file_lock::{ diff --git a/serial_test/src/parallel_code_lock.rs b/serial_test/src/parallel_code_lock.rs index e854cec..603c222 100644 --- a/serial_test/src/parallel_code_lock.rs +++ b/serial_test/src/parallel_code_lock.rs @@ -1,6 +1,7 @@ #![allow(clippy::await_holding_lock)] use crate::code_lock::{check_new_key, LOCKS}; +#[cfg(feature = "async")] use futures::FutureExt; use std::{ops::Deref, panic}; @@ -39,6 +40,7 @@ pub fn local_parallel_core(name: &str, function: fn()) { } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_parallel_core_with_return( name: &str, fut: impl std::future::Future> + panic::UnwindSafe, @@ -58,6 +60,7 @@ pub async fn local_async_parallel_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_parallel_core( name: &str, fut: impl std::future::Future + panic::UnwindSafe, @@ -75,10 +78,10 @@ pub async fn local_async_parallel_core( #[cfg(test)] mod tests { - use crate::{ - code_lock::LOCKS, local_async_parallel_core, local_async_parallel_core_with_return, - local_parallel_core, local_parallel_core_with_return, - }; + #[cfg(feature = "async")] + use crate::{local_async_parallel_core, local_async_parallel_core_with_return}; + + use crate::{code_lock::LOCKS, local_parallel_core, local_parallel_core_with_return}; use std::{io::Error, ops::Deref, panic}; #[test] @@ -114,6 +117,7 @@ mod tests { } #[tokio::test] + #[cfg(feature = "async")] async fn unlock_on_assert_async_without_return() { async fn demo_assert() { assert!(false); @@ -135,6 +139,7 @@ mod tests { } #[tokio::test] + #[cfg(feature = "async")] async fn unlock_on_assert_async_with_return() { async fn demo_assert() -> Result<(), Error> { assert!(false); diff --git a/serial_test/src/parallel_file_lock.rs b/serial_test/src/parallel_file_lock.rs index 1c752bb..f7f73d8 100644 --- a/serial_test/src/parallel_file_lock.rs +++ b/serial_test/src/parallel_file_lock.rs @@ -1,5 +1,6 @@ use std::panic; +#[cfg(feature = "async")] use futures::FutureExt; use crate::file_lock::make_lock_for_name_and_path; @@ -34,6 +35,7 @@ pub fn fs_parallel_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn fs_async_parallel_core_with_return( name: &str, path: Option<&str>, @@ -51,6 +53,7 @@ pub async fn fs_async_parallel_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn fs_async_parallel_core( name: &str, path: Option<&str>, @@ -66,10 +69,12 @@ pub async fn fs_async_parallel_core( #[cfg(test)] mod tests { + #[cfg(feature = "async")] + use crate::{fs_async_parallel_core, fs_async_parallel_core_with_return}; + use crate::{ file_lock::{path_for_name, Lock}, - fs_async_parallel_core, fs_async_parallel_core_with_return, fs_parallel_core, - fs_parallel_core_with_return, + fs_parallel_core, fs_parallel_core_with_return, }; use std::{io::Error, panic}; @@ -110,6 +115,8 @@ mod tests { } #[tokio::test] + #[cfg(feature = "async")] + async fn unlock_on_assert_async_without_return() { let lock_path = path_for_name("unlock_on_assert_async_without_return"); async fn demo_assert() { @@ -134,6 +141,7 @@ mod tests { } #[tokio::test] + #[cfg(feature = "async")] async fn unlock_on_assert_async_with_return() { let lock_path = path_for_name("unlock_on_assert_async_with_return"); diff --git a/serial_test/src/serial_code_lock.rs b/serial_test/src/serial_code_lock.rs index 5c494bd..c48ba2c 100644 --- a/serial_test/src/serial_code_lock.rs +++ b/serial_test/src/serial_code_lock.rs @@ -27,6 +27,7 @@ pub fn local_serial_core(name: &str, function: fn()) { } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_serial_core_with_return( name: &str, fut: impl std::future::Future>, @@ -40,6 +41,7 @@ pub async fn local_async_serial_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_serial_core(name: &str, fut: impl std::future::Future) { check_new_key(name); diff --git a/serial_test_derive/src/lib.rs b/serial_test_derive/src/lib.rs index 56bb310..9fd6343 100644 --- a/serial_test_derive/src/lib.rs +++ b/serial_test_derive/src/lib.rs @@ -316,6 +316,9 @@ where { let ast: syn::ItemFn = syn::parse2(input).unwrap(); let asyncness = ast.sig.asyncness; + if asyncness.is_some() && cfg!(not(feature = "async")) { + panic!("async testing attempted with async feature disabled in serial_test!"); + } let name = ast.sig.ident; let return_type = match ast.sig.output { syn::ReturnType::Default => None, @@ -464,6 +467,7 @@ mod tests { } #[test] + #[cfg(feature = "async")] fn test_serial_async() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { @@ -479,6 +483,7 @@ mod tests { } #[test] + #[cfg(feature = "async")] fn test_serial_async_return() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { @@ -496,6 +501,7 @@ mod tests { // 1.54 needed for https://github.com/rust-lang/rust/commit/9daf546b77dbeab7754a80d7336cd8d00c6746e4 change in note message #[rustversion::since(1.54)] #[test] + #[cfg(feature = "async")] fn test_serial_async_before_wrapper() { let t = trybuild::TestCases::new(); t.compile_fail("tests/broken/test_serial_async_before_wrapper.rs"); From ea05666cd4ffb01aa18404e47979d7533ad988b6 Mon Sep 17 00:00:00 2001 From: Tom Parker-Shemilt Date: Sat, 6 Aug 2022 10:26:38 +0100 Subject: [PATCH 2/3] Split out all the fs async --- serial_test/src/lib.rs | 16 ++++++++-------- serial_test/src/serial_file_lock.rs | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/serial_test/src/lib.rs b/serial_test/src/lib.rs index 938a8ec..25a4967 100644 --- a/serial_test/src/lib.rs +++ b/serial_test/src/lib.rs @@ -74,17 +74,17 @@ pub use serial_code_lock::{local_async_serial_core, local_async_serial_core_with pub use serial_code_lock::{local_serial_core, local_serial_core_with_return}; +#[cfg(all(feature = "file_locks", feature = "async"))] +pub use serial_file_lock::{fs_async_serial_core, fs_async_serial_core_with_return}; + #[cfg(feature = "file_locks")] -pub use serial_file_lock::{ - fs_async_serial_core, fs_async_serial_core_with_return, fs_serial_core, - fs_serial_core_with_return, -}; +pub use serial_file_lock::{fs_serial_core, fs_serial_core_with_return}; + +#[cfg(all(feature = "file_locks", feature = "async"))] +pub use parallel_file_lock::{fs_async_parallel_core, fs_async_parallel_core_with_return}; #[cfg(feature = "file_locks")] -pub use parallel_file_lock::{ - fs_async_parallel_core, fs_async_parallel_core_with_return, fs_parallel_core, - fs_parallel_core_with_return, -}; +pub use parallel_file_lock::{fs_parallel_core, fs_parallel_core_with_return}; // Re-export #[serial/parallel]. pub use serial_test_derive::{parallel, serial}; diff --git a/serial_test/src/serial_file_lock.rs b/serial_test/src/serial_file_lock.rs index 8c5576e..dc224f8 100644 --- a/serial_test/src/serial_file_lock.rs +++ b/serial_test/src/serial_file_lock.rs @@ -25,6 +25,7 @@ pub fn fs_serial_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn fs_async_serial_core_with_return( name: &str, _max_wait: Option, @@ -39,6 +40,7 @@ pub async fn fs_async_serial_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn fs_async_serial_core( name: &str, _max_wait: Option, From 0fb75d622b156a34163e8085532f91ed2ef05bad Mon Sep 17 00:00:00 2001 From: Tom Parker-Shemilt Date: Sat, 6 Aug 2022 10:28:52 +0100 Subject: [PATCH 3/3] Minor spacing issue with cfg --- serial_test/src/parallel_file_lock.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/serial_test/src/parallel_file_lock.rs b/serial_test/src/parallel_file_lock.rs index 6e65933..fb4430e 100644 --- a/serial_test/src/parallel_file_lock.rs +++ b/serial_test/src/parallel_file_lock.rs @@ -126,7 +126,6 @@ mod tests { #[tokio::test] #[cfg(feature = "async")] - async fn unlock_on_assert_async_without_return() { let lock_path = path_for_name("unlock_on_assert_async_without_return"); async fn demo_assert() {