diff --git a/serial_test/Cargo.toml b/serial_test/Cargo.toml index 10104c6..1350e91 100644 --- a/serial_test/Cargo.toml +++ b/serial_test/Cargo.toml @@ -19,7 +19,7 @@ document-features = { version = "0.2", optional = true } log = { version = "0.4", optional = true } futures = { version = "^0.3", default_features = false, features = [ "executor", -] } +], optional = true} dashmap = { version = "5"} [dev-dependencies] @@ -27,11 +27,14 @@ itertools = "0.10" tokio = { version = "^1.17", features = ["macros", "rt"] } [features] -default = ["logging"] +default = ["logging", "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 9c8c9d7..25a4967 100644 --- a/serial_test/src/lib.rs +++ b/serial_test/src/lib.rs @@ -64,26 +64,27 @@ mod parallel_file_lock; #[cfg(feature = "file_locks")] mod serial_file_lock; -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(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/parallel_code_lock.rs b/serial_test/src/parallel_code_lock.rs index c4cbb73..0d4da78 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::{panic, time::Duration}; @@ -40,6 +41,7 @@ pub fn local_parallel_core(name: &str, max_wait: Option, function: fn( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_parallel_core_with_return( name: &str, max_wait: Option, @@ -60,6 +62,7 @@ pub async fn local_async_parallel_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_parallel_core( name: &str, max_wait: Option, @@ -78,10 +81,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, panic}; #[test] @@ -122,6 +125,7 @@ mod tests { } #[tokio::test] + #[cfg(feature = "async")] async fn unlock_on_assert_async_without_return() { async fn demo_assert() { assert!(false); @@ -146,6 +150,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 dfc3abe..fb4430e 100644 --- a/serial_test/src/parallel_file_lock.rs +++ b/serial_test/src/parallel_file_lock.rs @@ -1,5 +1,6 @@ use std::{panic, time::Duration}; +#[cfg(feature = "async")] use futures::FutureExt; use crate::file_lock::make_lock_for_name_and_path; @@ -40,6 +41,7 @@ pub fn fs_parallel_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn fs_async_parallel_core_with_return( name: &str, _max_wait: Option, @@ -58,6 +60,7 @@ pub async fn fs_async_parallel_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn fs_async_parallel_core( name: &str, _max_wait: Option, @@ -74,10 +77,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}; @@ -120,6 +125,7 @@ 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() { @@ -145,6 +151,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 8071198..dad9abe 100644 --- a/serial_test/src/serial_code_lock.rs +++ b/serial_test/src/serial_code_lock.rs @@ -28,6 +28,7 @@ pub fn local_serial_core(name: &str, max_wait: Option, function: fn()) } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_serial_core_with_return( name: &str, max_wait: Option, @@ -42,6 +43,7 @@ pub async fn local_async_serial_core_with_return( } #[doc(hidden)] +#[cfg(feature = "async")] pub async fn local_async_serial_core( name: &str, max_wait: Option, 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, diff --git a/serial_test_derive/src/lib.rs b/serial_test_derive/src/lib.rs index b1c6d81..e696f3e 100644 --- a/serial_test_derive/src/lib.rs +++ b/serial_test_derive/src/lib.rs @@ -380,6 +380,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, @@ -577,6 +580,7 @@ mod tests { } #[test] + #[cfg(feature = "async")] fn test_serial_async() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { @@ -592,6 +596,7 @@ mod tests { } #[test] + #[cfg(feature = "async")] fn test_serial_async_return() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { @@ -609,6 +614,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");