Skip to content

Commit 67d23bd

Browse files
author
Andrew J Westlake
committed
Consolidated async-std and tokio test_main proc macros
1 parent 5d6f6a7 commit 67d23bd

File tree

7 files changed

+14
-48
lines changed

7 files changed

+14
-48
lines changed

pyo3-asyncio-macros/src/lib.rs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -153,40 +153,6 @@ pub fn async_std_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
153153
result.into()
154154
}
155155

156-
#[cfg(not(test))]
157-
#[proc_macro]
158-
pub fn async_std_test_main(args: TokenStream) -> TokenStream {
159-
let suite_name = syn::parse_macro_input!(args as syn::LitStr);
160-
161-
let result = quote! {
162-
#[derive(Clone)]
163-
pub(crate) struct Test {
164-
pub name: String,
165-
pub test_fn: &'static (dyn Fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = pyo3::PyResult<()>> + Send>> + Send + Sync),
166-
}
167-
168-
impl pyo3_asyncio::testing::TestTrait for Test {
169-
fn name(&self) -> &str {
170-
self.name.as_str()
171-
}
172-
173-
fn task(self) -> std::pin::Pin<Box<dyn std::future::Future<Output = pyo3::PyResult<()>> + Send>> {
174-
(self.test_fn)()
175-
}
176-
}
177-
178-
inventory::collect!(Test);
179-
180-
fn main() {
181-
pyo3_asyncio::async_std::testing::test_main(
182-
#suite_name,
183-
inventory::iter::<Test>().map(|test| test.clone()).collect()
184-
);
185-
}
186-
};
187-
result.into()
188-
}
189-
190156
#[cfg(not(test))] // NOTE: exporting main breaks tests, we should file an issue.
191157
#[proc_macro_attribute]
192158
pub fn tokio_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -256,12 +222,12 @@ impl Parse for Item {
256222
}
257223
}
258224

259-
struct TokioTestMainArgs {
225+
struct TestMainArgs {
260226
attrs: Vec<Attribute>,
261227
suite_name: String,
262228
}
263229

264-
impl Parse for TokioTestMainArgs {
230+
impl Parse for TestMainArgs {
265231
fn parse(input: ParseStream<'_>) -> Result<Self> {
266232
let mut args: syn::punctuated::Punctuated<Item, syn::Token![,]> =
267233
input.parse_terminated(Item::parse)?;
@@ -283,9 +249,8 @@ impl Parse for TokioTestMainArgs {
283249

284250
#[cfg(not(test))]
285251
#[proc_macro]
286-
pub fn tokio_test_main(args: TokenStream) -> TokenStream {
287-
let TokioTestMainArgs { attrs, suite_name } =
288-
syn::parse_macro_input!(args as TokioTestMainArgs);
252+
pub fn test_main(args: TokenStream) -> TokenStream {
253+
let TestMainArgs { attrs, suite_name } = syn::parse_macro_input!(args as TestMainArgs);
289254

290255
let result = quote! {
291256
#[derive(Clone)]

pytests/test_async_std_asyncio.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@ async fn test_into_future() -> PyResult<()> {
6464
common::test_into_future().await
6565
}
6666

67-
pyo3_asyncio::async_std::test_main!("PyO3 Asyncio Test Suite for Async-Std Runtime");
67+
pyo3_asyncio::testing::test_main!(
68+
#[pyo3_asyncio::async_std::main],
69+
"PyO3 Asyncio Test Suite for Async-Std Runtime"
70+
);

pytests/test_tokio_current_thread_asyncio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod common;
22
mod tokio_asyncio;
33

44
// TODO: Fix current thread init
5-
pyo3_asyncio::tokio::test_main!(
5+
pyo3_asyncio::testing::test_main!(
66
#[pyo3_asyncio::tokio::main(flavor = "current_thread")],
77
"PyO3 Asyncio Tokio Current Thread Test Suite"
88
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod common;
22
mod tokio_asyncio;
33

4-
pyo3_asyncio::tokio::test_main!(
4+
pyo3_asyncio::testing::test_main!(
55
#[pyo3_asyncio::tokio::main],
66
"PyO3 Asyncio Tokio Multi Thread Test Suite"
77
);

src/async_std.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ pub use pyo3_asyncio_macros::async_std_main as main;
1212
#[cfg(feature = "attributes")]
1313
pub use pyo3_asyncio_macros::async_std_test as test;
1414

15-
#[cfg(feature = "attributes")]
16-
pub use pyo3_asyncio_macros::async_std_test_main as test_main;
17-
1815
struct AsyncStdJoinError;
1916

2017
impl JoinError for AsyncStdJoinError {

src/testing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ use clap::{App, Arg};
1717
use futures::stream::{self, StreamExt};
1818
use pyo3::prelude::*;
1919

20+
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span>
21+
#[cfg(feature = "attributes")]
22+
pub use pyo3_asyncio_macros::test_main;
23+
2024
/// Args that should be provided to the test program
2125
///
2226
/// These args are meant to mirror the default test harness's args.

src/tokio.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ pub use pyo3_asyncio_macros::tokio_main as main;
1919
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span> Sets up the tokio runtime and runs an async fn as main
2020
#[cfg(feature = "attributes")]
2121
pub use pyo3_asyncio_macros::tokio_test as test;
22-
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span>
23-
#[cfg(feature = "attributes")]
24-
pub use pyo3_asyncio_macros::tokio_test_main as test_main;
2522

2623
static TOKIO_RUNTIME: OnceCell<Runtime> = OnceCell::new();
2724

0 commit comments

Comments
 (0)