Skip to content

Commit d684e28

Browse files
committed
Validate custom executor is passed if needed
1 parent 894d1a3 commit d684e28

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

macros/src/attributes/tests/parse.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ impl Input {
6666
validate_argument_type(test, expected_arg_type)?;
6767
}
6868

69+
// Validate a custom executor is provided if needed and at least one test/init is async
70+
if cfg!(feature = "external-executor") && macro_args.executor.is_none() {
71+
if tests.iter().any(|test| test.asyncness)
72+
|| init.as_ref().map_or(false, |i| i.asyncness)
73+
{
74+
return Err(parse::Error::new(
75+
proc_macro2::Span::call_site(),
76+
"async test/init func requires that an executor is provided via `#[embedded_test::tests(executor = ...)] because the feature `external-executor` is enabled",
77+
));
78+
}
79+
}
80+
6981
Ok(Self {
7082
module_name: module.ident.to_string(),
7183
macro_args,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
```cargo
3+
[dependencies]
4+
embassy-executor = { version = "0.7", features = ["arch-riscv32"] }
5+
embedded-test = { path = "../../..", features = ["embassy", "external-executor"] }
6+
7+
[lib]
8+
harness = false
9+
```
10+
*/
11+
12+
#![no_std]
13+
#![no_main]
14+
15+
#[cfg(test)]
16+
#[embedded_test::tests]
17+
mod tests {
18+
struct Context;
19+
20+
#[init]
21+
async fn async_init() -> Context {
22+
Context
23+
}
24+
25+
#[test]
26+
fn takes_state(_state: Context) {
27+
assert!(true)
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: async test/init func requires that an executor is provided via `#[embedded_test::tests(executor = ...)] because the feature `external-executor` is enabled
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
```cargo
3+
[dependencies]
4+
embassy-executor = { version = "0.7", features = ["arch-riscv32"] }
5+
embedded-test = { path = "../../..", features = ["embassy", "external-executor"] }
6+
7+
[lib]
8+
harness = false
9+
```
10+
*/
11+
12+
#![no_std]
13+
#![no_main]
14+
15+
#[cfg(test)]
16+
#[embedded_test::tests]
17+
mod tests {
18+
struct Context;
19+
20+
#[init]
21+
fn sync_init() -> Context {
22+
Context
23+
}
24+
25+
#[test]
26+
async fn takes_state(_state: Context) {
27+
assert!(true)
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: async test/init func requires that an executor is provided via `#[embedded_test::tests(executor = ...)] because the feature `external-executor` is enabled

0 commit comments

Comments
 (0)