1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#![deny(missing_docs, unsafe_code)]
#![allow(unused_imports, unused_variables)]
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{AttributeArgs, Error, ItemFn, Lit, Meta, NestedMeta, ReturnType};
#[proc_macro_attribute]
pub fn test_fork(args: TokenStream, item: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as AttributeArgs);
let mut crate_name = quote::quote! { two_rusty_forks };
let mut timeout = quote::quote! { 0 };
for arg in args {
if let NestedMeta::Meta(meta) = arg {
if let Meta::NameValue(name_value) = meta {
if let Some(ident) = name_value.path.get_ident() {
match ident.to_string().as_str() {
"timeout_ms" => {
if let Lit::Int(int) = name_value.lit {
timeout = int.to_token_stream();
}
}
"crate" => {
if let Lit::Str(str) = name_value.lit {
crate_name = str.to_token_stream();
}
}
_ => (),
}
}
}
}
}
let item = syn::parse_macro_input!(item as ItemFn);
let fn_attrs = &item.attrs;
let fn_sig = &item.sig;
let fn_body = &item.block;
let fn_name = &fn_sig.ident;
let mut test = quote::quote! { #[test] };
for attr in fn_attrs {
if let Some(ident) = attr.path.get_ident() {
if ident == "test" {
test = quote::quote! {};
}
}
}
if let Some(asyncness) = fn_sig.asyncness {
return Error::new(
asyncness.span,
"put `#[test_fork]` last to let other macros process first",
)
.to_compile_error()
.into();
}
let body_fn = if let ReturnType::Type(_, ret_ty) = &fn_sig.output {
quote::quote! {
fn body_fn() {
fn body_fn() -> #ret_ty #fn_body
body_fn().unwrap();
}
}
} else {
quote::quote! {
fn body_fn() #fn_body
}
};
(quote::quote! {
#test
#(#fn_attrs)*
fn #fn_name() {
#body_fn
let body: fn () = body_fn;
fn supervise_fn(
child: &mut ::#crate_name::ChildWrapper,
file: &mut ::std::fs::File
) {
::#crate_name::fork_test::supervise_child(child, file, #timeout)
}
let supervise:
fn (&mut ::#crate_name::ChildWrapper, &mut ::std::fs::File) =
supervise_fn;
::#crate_name::fork(
::#crate_name::rusty_fork_test_name!(#fn_name),
::#crate_name::rusty_fork_id!(),
::#crate_name::fork_test::no_configure_child,
supervise,
body
).expect("forking test failed")
}
})
.into()
}
#[cfg(test)]
mod test {
use std::io::Result;
use two_rusty_forks::test_fork;
#[test_fork]
fn trivials() {}
#[test_fork]
#[should_panic]
fn panicking_child() {
panic!("just testing a panic, nothing to see here");
}
#[test_fork]
#[should_panic]
fn aborting_child() {
::std::process::abort();
}
#[test_fork]
fn trivial_result() -> Result<()> {
Ok(())
}
#[test_fork]
#[should_panic]
fn panicking_child_result() -> Result<()> {
panic!("just testing a panic, nothing to see here");
}
#[test_fork]
#[should_panic]
fn aborting_child_result() -> Result<()> {
::std::process::abort();
}
#[test_fork(timeout_ms = 1000)]
fn timeout_passes() {}
#[test_fork(timeout_ms = 1000)]
#[should_panic]
fn timeout_fails() {
println!("hello from child");
::std::thread::sleep(::std::time::Duration::from_millis(10000));
println!("goodbye from child");
}
#[tokio::test]
#[test_fork]
async fn my_test() {
assert!(true);
}
}