test_with_derive/lib.rs
1use proc_macro::TokenStream;
2use syn::{parse_macro_input, ItemFn, ItemMod};
3
4#[cfg(feature = "runtime")]
5use syn::ReturnType;
6
7use crate::utils::{fn_macro, is_module, lock_macro, mod_macro};
8
9mod env;
10#[cfg(feature = "executable")]
11mod executable;
12mod file;
13#[cfg(feature = "http")]
14mod http;
15#[cfg(feature = "icmp")]
16mod icmp;
17#[cfg(feature = "resource")]
18mod resource;
19#[cfg(feature = "runtime")]
20mod runtime;
21#[cfg(all(feature = "runtime", feature = "ip"))]
22mod runtime_ip;
23
24mod socket;
25#[cfg(feature = "timezone")]
26mod timezone;
27#[cfg(feature = "user")]
28mod user;
29mod utils;
30
31/// Run test case when the environment variable is set.
32/// ```
33/// #[cfg(test)]
34/// mod tests {
35///
36/// // PWD environment variable exists
37/// #[test_with::env(PWD)]
38/// #[test]
39/// fn test_works() {
40/// assert!(true);
41/// }
42///
43/// // NOTHING environment variable does not exist
44/// #[test_with::env(NOTHING)]
45/// #[test]
46/// fn test_ignored() {
47/// panic!("should be ignored")
48/// }
49///
50/// // NOT_SAYING environment variable does not exist
51/// #[test_with::env(PWD, NOT_SAYING)]
52/// #[test]
53/// fn test_ignored_too() {
54/// panic!("should be ignored")
55/// }
56/// }
57/// ```
58/// or run all test cases for test module when the environment variable is set.
59/// ```
60/// #[test_with::env(PWD)]
61/// #[cfg(test)]
62/// mod tests {
63///
64/// #[test]
65/// fn test_works() {
66/// assert!(true);
67/// }
68/// }
69/// ```
70#[proc_macro_attribute]
71pub fn env(attr: TokenStream, stream: TokenStream) -> TokenStream {
72 if is_module(&stream) {
73 mod_macro(
74 attr,
75 parse_macro_input!(stream as ItemMod),
76 env::check_env_condition,
77 )
78 } else {
79 fn_macro(
80 attr,
81 parse_macro_input!(stream as ItemFn),
82 env::check_env_condition,
83 )
84 }
85}
86
87/// Run test case when the example running and the environment variable is set.
88///```rust
89/// // write as example in examples/*rs
90/// test_with::runner!(env);
91/// #[test_with::module]
92/// mod env {
93/// #[test_with::runtime_env(PWD)]
94/// fn test_works() {
95/// assert!(true);
96/// }
97/// }
98///```
99#[cfg(not(feature = "runtime"))]
100#[proc_macro_attribute]
101pub fn runtime_env(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
102 panic!("should be used with runtime feature")
103}
104#[cfg(feature = "runtime")]
105#[proc_macro_attribute]
106pub fn runtime_env(attr: TokenStream, stream: TokenStream) -> TokenStream {
107 env::runtime_env(attr, stream)
108}
109
110/// Ignore test case when the environment variable is set.
111/// ```
112/// #[cfg(test)]
113/// mod tests {
114///
115/// // The test will be ignored in GITHUB_ACTION
116/// #[test_with::no_env(GITHUB_ACTIONS)]
117/// #[test]
118/// fn test_ignore_in_github_action() {
119/// assert!(false);
120/// }
121/// }
122#[proc_macro_attribute]
123pub fn no_env(attr: TokenStream, stream: TokenStream) -> TokenStream {
124 if is_module(&stream) {
125 mod_macro(
126 attr,
127 parse_macro_input!(stream as ItemMod),
128 env::check_no_env_condition,
129 )
130 } else {
131 fn_macro(
132 attr,
133 parse_macro_input!(stream as ItemFn),
134 env::check_no_env_condition,
135 )
136 }
137}
138
139/// Ignore test case when the example running and the environment variable is set.
140///```rust
141/// // write as example in examples/*rs
142/// test_with::runner!(env);
143/// #[test_with::module]
144/// mod env {
145/// #[test_with::runtime_no_env(NOT_EXIST)]
146/// fn test_works() {
147/// assert!(true);
148/// }
149/// }
150///```
151#[cfg(not(feature = "runtime"))]
152#[proc_macro_attribute]
153pub fn runtime_no_env(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
154 panic!("should be used with runtime feature")
155}
156#[cfg(feature = "runtime")]
157#[proc_macro_attribute]
158pub fn runtime_no_env(attr: TokenStream, stream: TokenStream) -> TokenStream {
159 env::runtime_no_env(attr, stream)
160}
161
162/// Run test case when the file exist.
163/// ```
164/// #[cfg(test)]
165/// mod tests {
166///
167/// // hostname exists
168/// #[test_with::file(/etc/hostname)]
169/// #[test]
170/// fn test_works() {
171/// assert!(true);
172/// }
173///
174/// // nothing file does not exist
175/// #[test_with::file(/etc/nothing)]
176/// #[test]
177/// fn test_ignored() {
178/// panic!("should be ignored")
179/// }
180///
181/// // hostname and hosts exist
182/// #[test_with::file(/etc/hostname, /etc/hosts)]
183/// #[test]
184/// fn test_works_too() {
185/// assert!(true);
186/// }
187/// }
188/// ```
189#[proc_macro_attribute]
190pub fn file(attr: TokenStream, stream: TokenStream) -> TokenStream {
191 if is_module(&stream) {
192 mod_macro(
193 attr,
194 parse_macro_input!(stream as ItemMod),
195 file::check_file_condition,
196 )
197 } else {
198 fn_macro(
199 attr,
200 parse_macro_input!(stream as ItemFn),
201 file::check_file_condition,
202 )
203 }
204}
205
206/// Run test case when the example running and the file exist.
207///```rust
208/// // write as example in examples/*rs
209/// test_with::runner!(file);
210/// #[test_with::module]
211/// mod file {
212/// #[test_with::runtime_file(/etc/hostname)]
213/// fn test_works() {
214/// assert!(true);
215/// }
216/// }
217///```
218#[cfg(not(feature = "runtime"))]
219#[proc_macro_attribute]
220pub fn runtime_file(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
221 panic!("should be used with runtime feature")
222}
223#[cfg(feature = "runtime")]
224#[proc_macro_attribute]
225pub fn runtime_file(attr: TokenStream, stream: TokenStream) -> TokenStream {
226 file::runtime_file(attr, stream)
227}
228
229/// Run test case when the path(file or folder) exist.
230/// ```
231/// #[cfg(test)]
232/// mod tests {
233///
234/// // etc exists
235/// #[test_with::path(/etc)]
236/// #[test]
237/// fn test_works() {
238/// assert!(true);
239/// }
240///
241/// // nothing does not exist
242/// #[test_with::path(/nothing)]
243/// #[test]
244/// fn test_ignored() {
245/// panic!("should be ignored")
246/// }
247///
248/// // etc and tmp exist
249/// #[test_with::path(/etc, /tmp)]
250/// #[test]
251/// fn test_works_too() {
252/// assert!(true);
253/// }
254/// }
255/// ```
256#[proc_macro_attribute]
257pub fn path(attr: TokenStream, stream: TokenStream) -> TokenStream {
258 if is_module(&stream) {
259 mod_macro(
260 attr,
261 parse_macro_input!(stream as ItemMod),
262 file::check_path_condition,
263 )
264 } else {
265 fn_macro(
266 attr,
267 parse_macro_input!(stream as ItemFn),
268 file::check_path_condition,
269 )
270 }
271}
272
273/// Run test case when the example running and the path(file or folder) exist.
274///```rust
275/// // write as example in examples/*rs
276/// test_with::runner!(path);
277/// #[test_with::module]
278/// mod path {
279/// #[test_with::runtime_path(/etc)]
280/// fn test_works() {
281/// assert!(true);
282/// }
283/// }
284///```
285#[cfg(not(feature = "runtime"))]
286#[proc_macro_attribute]
287pub fn runtime_path(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
288 panic!("should be used with runtime feature")
289}
290#[cfg(feature = "runtime")]
291#[proc_macro_attribute]
292pub fn runtime_path(attr: TokenStream, stream: TokenStream) -> TokenStream {
293 file::runtime_path(attr, stream)
294}
295
296/// Run test case when the http service exist.
297/// ```
298/// #[cfg(test)]
299/// mod tests {
300///
301/// // http service exists
302/// #[test_with::http(httpbin.org)]
303/// #[test]
304/// fn test_works() {
305/// assert!(true);
306/// }
307///
308/// // There is no not.exist.com
309/// #[test_with::http(not.exist.com)]
310/// #[test]
311/// fn test_ignored() {
312/// panic!("should be ignored")
313/// }
314/// }
315/// ```
316#[proc_macro_attribute]
317#[cfg(feature = "http")]
318pub fn http(attr: TokenStream, stream: TokenStream) -> TokenStream {
319 if is_module(&stream) {
320 mod_macro(
321 attr,
322 parse_macro_input!(stream as ItemMod),
323 http::check_http_condition,
324 )
325 } else {
326 fn_macro(
327 attr,
328 parse_macro_input!(stream as ItemFn),
329 http::check_http_condition,
330 )
331 }
332}
333
334/// Run test case when the example running and the http service exist.
335///```rust
336/// // write as example in examples/*rs
337/// test_with::runner!(http);
338/// #[test_with::module]
339/// mod http {
340/// #[test_with::runtime_http(httpbin.org)]
341/// fn test_works() {
342/// assert!(true);
343/// }
344/// }
345#[cfg(not(feature = "runtime"))]
346#[proc_macro_attribute]
347pub fn runtime_http(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
348 panic!("should be used with runtime feature")
349}
350
351#[cfg(all(feature = "runtime", feature = "http"))]
352#[proc_macro_attribute]
353pub fn runtime_http(attr: TokenStream, stream: TokenStream) -> TokenStream {
354 http::runtime_http(attr, stream)
355}
356
357/// Run test case when the https service exist.
358/// ```
359/// #[cfg(test)]
360/// mod tests {
361///
362/// // https server exists
363/// #[test_with::https(www.rust-lang.org)]
364/// #[test]
365/// fn test_works() {
366/// assert!(true);
367/// }
368///
369/// // There is no not.exist.com
370/// #[test_with::https(not.exist.com)]
371/// #[test]
372/// fn test_ignored() {
373/// panic!("should be ignored")
374/// }
375/// }
376/// ```
377#[proc_macro_attribute]
378#[cfg(feature = "http")]
379pub fn https(attr: TokenStream, stream: TokenStream) -> TokenStream {
380 if is_module(&stream) {
381 mod_macro(
382 attr,
383 parse_macro_input!(stream as ItemMod),
384 http::check_https_condition,
385 )
386 } else {
387 fn_macro(
388 attr,
389 parse_macro_input!(stream as ItemFn),
390 http::check_https_condition,
391 )
392 }
393}
394
395/// Run test case when the example running and the http service exist.
396///```rust
397/// // write as example in examples/*rs
398/// test_with::runner!(http);
399/// #[test_with::module]
400/// mod http {
401/// #[test_with::runtime_https(httpbin.org)]
402/// fn test_works() {
403/// assert!(true);
404/// }
405/// }
406#[cfg(all(not(feature = "runtime"), feature = "http"))]
407#[proc_macro_attribute]
408pub fn runtime_https(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
409 panic!("should be used with runtime feature")
410}
411
412#[cfg(all(feature = "runtime", feature = "http"))]
413#[proc_macro_attribute]
414pub fn runtime_https(attr: TokenStream, stream: TokenStream) -> TokenStream {
415 http::runtime_https(attr, stream)
416}
417
418/// Run test case when the server online.
419/// Please make sure the role of test case runner have capability to open socket
420///
421/// ```
422/// #[cfg(test)]
423/// mod tests {
424///
425/// // localhost is online
426/// #[test_with::icmp(127.0.0.1)]
427/// #[test]
428/// fn test_works() {
429/// assert!(true);
430/// }
431///
432/// // 193.194.195.196 is offline
433/// #[test_with::icmp(193.194.195.196)]
434/// #[test]
435/// fn test_ignored() {
436/// panic!("should be ignored")
437/// }
438/// }
439/// ```
440#[proc_macro_attribute]
441#[cfg(feature = "icmp")]
442pub fn icmp(attr: TokenStream, stream: TokenStream) -> TokenStream {
443 if is_module(&stream) {
444 mod_macro(
445 attr,
446 parse_macro_input!(stream as ItemMod),
447 icmp::check_icmp_condition,
448 )
449 } else {
450 fn_macro(
451 attr,
452 parse_macro_input!(stream as ItemFn),
453 icmp::check_icmp_condition,
454 )
455 }
456}
457
458/// Run test case when the example running and the server online.
459/// Please make sure the role of test case runner have capability to open socket
460///```rust
461/// // write as example in examples/*rs
462/// test_with::runner!(icmp);
463/// #[test_with::module]
464/// mod icmp {
465/// // 193.194.195.196 is offline
466/// #[test_with::runtime_icmp(193.194.195.196)]
467/// fn test_ignored_with_non_existing_host() {
468/// panic!("should be ignored with non existing host")
469/// }
470/// }
471#[cfg(all(not(feature = "runtime"), feature = "icmp"))]
472#[proc_macro_attribute]
473pub fn runtime_icmp(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
474 panic!("should be used with runtime feature")
475}
476
477#[cfg(all(feature = "runtime", feature = "icmp"))]
478#[proc_macro_attribute]
479pub fn runtime_icmp(attr: TokenStream, stream: TokenStream) -> TokenStream {
480 icmp::runtime_icmp(attr, stream)
481}
482
483/// Run test case when the example running and one of the interface ip is in the CIDR.
484///```rust
485/// // write as example in examples/*rs
486/// test_with::runner!(ip);
487/// #[test_with::module]
488/// mod ip {
489/// // Only works when one of the interface ip is in 192.168.1.0/24
490/// #[test_with::runtime_ip_in(192.168.1.0/24)]
491/// fn test_works() {
492/// assert!(true);
493/// }
494/// }
495#[cfg(all(not(feature = "runtime"), feature = "ip"))]
496#[proc_macro_attribute]
497pub fn runtime_ip_in(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
498 panic!("should be used with runtime feature")
499}
500
501#[cfg(all(feature = "runtime", feature = "ip"))]
502#[proc_macro_attribute]
503pub fn runtime_ip_in(attr: TokenStream, stream: TokenStream) -> TokenStream {
504 runtime_ip::runtime_ip_in(attr, stream)
505}
506
507/// Run test case when the example running and the public ip can be got.
508/// The default check site is `https://ip.me`, and can be overridden with `ip_check_site=<url>`.
509///```rust
510/// // write as example in examples/*rs
511/// test_with::runner!(ip);
512/// #[test_with::module]
513/// mod ip {
514/// // Only works when the public ip can be got from https://ip.me
515/// #[test_with::runtime_public_ip]
516/// fn test_works() {
517/// assert!(true);
518/// }
519///
520/// // Or override the check site (url has to be a string literal)
521/// #[test_with::runtime_public_ip(ip_check_site = "https://ipinfo.io/ip")]
522/// fn test_works_with_other_site() {
523/// assert!(true);
524/// }
525/// }
526#[cfg(all(not(feature = "runtime"), feature = "ip"))]
527#[proc_macro_attribute]
528pub fn runtime_public_ip(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
529 panic!("should be used with runtime feature")
530}
531
532#[cfg(all(feature = "runtime", feature = "ip"))]
533#[proc_macro_attribute]
534pub fn runtime_public_ip(attr: TokenStream, stream: TokenStream) -> TokenStream {
535 runtime_ip::runtime_public_ip(attr, stream)
536}
537
538/// Run test case when the example running and the public ip is in the CIDR.
539/// The default check site is `https://ip.me`, and can be overridden with `ip_check_site=<url>`.
540///```rust
541/// // write as example in examples/*rs
542/// test_with::runner!(ip);
543/// #[test_with::module]
544/// mod ip {
545/// // Only works when the public ip is in 1.2.3.0/24
546/// #[test_with::runtime_public_ip_in(1.2.3.0/24)]
547/// fn test_works() {
548/// assert!(true);
549/// }
550///
551/// // Or override the check site (url has to be a string literal)
552/// #[test_with::runtime_public_ip_in(1.2.3.0/24, ip_check_site = "https://ipinfo.io/ip")]
553/// fn test_works_with_other_site() {
554/// assert!(true);
555/// }
556/// }
557#[cfg(all(not(feature = "runtime"), feature = "ip"))]
558#[proc_macro_attribute]
559pub fn runtime_public_ip_in(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
560 panic!("should be used with runtime feature")
561}
562
563#[cfg(all(feature = "runtime", feature = "ip"))]
564#[proc_macro_attribute]
565pub fn runtime_public_ip_in(attr: TokenStream, stream: TokenStream) -> TokenStream {
566 runtime_ip::runtime_public_ip_in(attr, stream)
567}
568
569/// Run test case when the example running and the public ip is exactly the expected ip.
570/// The default check site is `https://ip.me`, and can be overridden with `ip_check_site=<url>`.
571///```rust
572/// // write as example in examples/*rs
573/// test_with::runner!(ip);
574/// #[test_with::module]
575/// mod ip {
576/// // Only works when the public ip is 1.2.3.4
577/// #[test_with::runtime_public_ip_is(1.2.3.4)]
578/// fn test_works() {
579/// assert!(true);
580/// }
581///
582/// // Or override the check site (url has to be a string literal)
583/// #[test_with::runtime_public_ip_is(1.2.3.4, ip_check_site = "https://ipinfo.io/ip")]
584/// fn test_works_with_other_site() {
585/// assert!(true);
586/// }
587/// }
588#[cfg(all(not(feature = "runtime"), feature = "ip"))]
589#[proc_macro_attribute]
590pub fn runtime_public_ip_is(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
591 panic!("should be used with runtime feature")
592}
593
594#[cfg(all(feature = "runtime", feature = "ip"))]
595#[proc_macro_attribute]
596pub fn runtime_public_ip_is(attr: TokenStream, stream: TokenStream) -> TokenStream {
597 runtime_ip::runtime_public_ip_is(attr, stream)
598}
599
600/// Run test case when socket connected
601///
602/// ```
603/// #[cfg(test)]
604/// mod tests {
605///
606/// // Google DNS is online
607/// #[test_with::tcp(8.8.8.8:53)]
608/// #[test]
609/// fn test_works() {
610/// assert!(true);
611/// }
612///
613/// // 193.194.195.196 is offline
614/// #[test_with::tcp(193.194.195.196)]
615/// #[test]
616/// fn test_ignored() {
617/// panic!("should be ignored")
618/// }
619/// }
620/// ```
621#[proc_macro_attribute]
622pub fn tcp(attr: TokenStream, stream: TokenStream) -> TokenStream {
623 if is_module(&stream) {
624 mod_macro(
625 attr,
626 parse_macro_input!(stream as ItemMod),
627 socket::check_tcp_condition,
628 )
629 } else {
630 fn_macro(
631 attr,
632 parse_macro_input!(stream as ItemFn),
633 socket::check_tcp_condition,
634 )
635 }
636}
637
638/// Run test case when the example running and socket connected
639///```rust
640/// // write as example in examples/*rs
641/// test_with::runner!(tcp);
642/// #[test_with::module]
643/// mod tcp {
644/// // Google DNS is online
645/// #[test_with::runtime_tcp(8.8.8.8:53)]
646/// fn test_works_with_DNS_server() {
647/// assert!(true);
648/// }
649/// }
650#[cfg(not(feature = "runtime"))]
651#[proc_macro_attribute]
652pub fn runtime_tcp(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
653 panic!("should be used with runtime feature")
654}
655
656#[cfg(feature = "runtime")]
657#[proc_macro_attribute]
658pub fn runtime_tcp(attr: TokenStream, stream: TokenStream) -> TokenStream {
659 socket::runtime_tcp(attr, stream)
660}
661
662/// Run test case when runner is root
663///
664/// ```
665/// #[cfg(test)]
666/// mod tests {
667///
668/// // Only works with root account
669/// #[test_with::root()]
670/// #[test]
671/// fn test_ignored() {
672/// panic!("should be ignored")
673/// }
674/// }
675/// ```
676#[proc_macro_attribute]
677#[cfg(feature = "user")]
678pub fn root(attr: TokenStream, stream: TokenStream) -> TokenStream {
679 if is_module(&stream) {
680 mod_macro(
681 attr,
682 parse_macro_input!(stream as ItemMod),
683 user::check_root_condition,
684 )
685 } else {
686 fn_macro(
687 attr,
688 parse_macro_input!(stream as ItemFn),
689 user::check_root_condition,
690 )
691 }
692}
693
694/// Run test case when runner is root
695///```rust
696/// // write as example in examples/*rs
697/// test_with::runner!(user);
698/// #[test_with::module]
699/// mod user {
700/// // Google DNS is online
701/// #[test_with::runtime_root()]
702/// fn test_ignored() {
703/// panic!("should be ignored")
704/// }
705/// }
706#[cfg(not(feature = "runtime"))]
707#[proc_macro_attribute]
708pub fn runtime_root(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
709 panic!("should be used with runtime feature")
710}
711#[cfg(all(feature = "runtime", feature = "user"))]
712#[proc_macro_attribute]
713pub fn runtime_root(attr: TokenStream, stream: TokenStream) -> TokenStream {
714 user::runtime_root(attr, stream)
715}
716
717/// Run test case when runner in group
718///
719/// ```
720/// #[cfg(test)]
721/// mod tests {
722///
723/// // Only works with group avengers
724/// #[test_with::group(avengers)]
725/// #[test]
726/// fn test_ignored() {
727/// panic!("should be ignored")
728/// }
729/// }
730/// ```
731#[proc_macro_attribute]
732#[cfg(feature = "user")]
733pub fn group(attr: TokenStream, stream: TokenStream) -> TokenStream {
734 if is_module(&stream) {
735 mod_macro(
736 attr,
737 parse_macro_input!(stream as ItemMod),
738 user::check_group_condition,
739 )
740 } else {
741 fn_macro(
742 attr,
743 parse_macro_input!(stream as ItemFn),
744 user::check_group_condition,
745 )
746 }
747}
748
749/// Run test case when runner in group
750///```rust
751/// // write as example in examples/*rs
752/// test_with::runner!(user);
753/// #[test_with::module]
754/// mod user {
755/// // Only works with group avengers
756/// #[test_with::runtime_group(avengers)]
757/// fn test_ignored() {
758/// panic!("should be ignored")
759/// }
760/// }
761#[cfg(not(feature = "runtime"))]
762#[proc_macro_attribute]
763pub fn runtime_group(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
764 panic!("should be used with runtime feature")
765}
766#[cfg(all(feature = "runtime", feature = "user"))]
767#[proc_macro_attribute]
768pub fn runtime_group(attr: TokenStream, stream: TokenStream) -> TokenStream {
769 user::runtime_group(attr, stream)
770}
771
772/// Run test case when runner is specific user
773///
774/// ```
775/// #[cfg(test)]
776/// mod tests {
777///
778/// // Only works with user
779/// #[test_with::user(spider)]
780/// #[test]
781/// fn test_ignored() {
782/// panic!("should be ignored")
783/// }
784/// }
785/// ```
786#[proc_macro_attribute]
787#[cfg(all(feature = "user", not(target_os = "windows")))]
788pub fn user(attr: TokenStream, stream: TokenStream) -> TokenStream {
789 if is_module(&stream) {
790 mod_macro(
791 attr,
792 parse_macro_input!(stream as ItemMod),
793 user::check_user_condition,
794 )
795 } else {
796 fn_macro(
797 attr,
798 parse_macro_input!(stream as ItemFn),
799 user::check_user_condition,
800 )
801 }
802}
803
804/// Run test case when runner is specific user
805///```rust
806/// // write as example in examples/*rs
807/// test_with::runner!(user);
808/// #[test_with::module]
809/// mod user {
810/// // Only works with user
811/// #[test_with::runtime_user(spider)]
812/// fn test_ignored() {
813/// panic!("should be ignored")
814/// }
815/// }
816#[cfg(not(feature = "runtime"))]
817#[proc_macro_attribute]
818pub fn runtime_user(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
819 panic!("should be used with runtime feature")
820}
821#[cfg(all(feature = "runtime", feature = "user"))]
822#[proc_macro_attribute]
823pub fn runtime_user(attr: TokenStream, stream: TokenStream) -> TokenStream {
824 user::runtime_user(attr, stream)
825}
826
827/// Run test case when memory size enough
828///
829/// ```
830/// #[cfg(test)]
831/// mod tests {
832///
833/// // Only works with enough memory size
834/// #[test_with::mem(100GB)]
835/// #[test]
836/// fn test_ignored() {
837/// panic!("should be ignored")
838/// }
839/// }
840/// ```
841#[proc_macro_attribute]
842#[cfg(feature = "resource")]
843pub fn mem(attr: TokenStream, stream: TokenStream) -> TokenStream {
844 if is_module(&stream) {
845 mod_macro(
846 attr,
847 parse_macro_input!(stream as ItemMod),
848 resource::check_mem_condition,
849 )
850 } else {
851 fn_macro(
852 attr,
853 parse_macro_input!(stream as ItemFn),
854 resource::check_mem_condition,
855 )
856 }
857}
858
859/// Run test case when the example running and memory size enough
860///```rust
861/// // write as example in examples/*rs
862/// test_with::runner!(resource);
863/// #[test_with::module]
864/// mod resource {
865/// // Only works with enough memory size
866/// #[test_with::runtime_mem(100GB)]
867/// fn test_ignored_mem_not_enough() {
868/// panic!("should be ignored")
869/// }
870/// }
871#[cfg(not(feature = "runtime"))]
872#[proc_macro_attribute]
873pub fn runtime_mem(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
874 panic!("should be used with runtime feature")
875}
876#[cfg(all(feature = "runtime", feature = "resource"))]
877#[proc_macro_attribute]
878pub fn runtime_mem(attr: TokenStream, stream: TokenStream) -> TokenStream {
879 resource::runtime_mem(attr, stream)
880}
881
882/// Run test case when the example running and free memory size enough
883///```rust
884/// // write as example in examples/*rs
885/// test_with::runner!(resource);
886/// #[test_with::module]
887/// mod resource {
888/// // Only works with enough free memory size
889/// #[test_with::runtime_free_mem(100GB)]
890/// fn test_ignored_free_mem_not_enough() {
891/// panic!("should be ignored")
892/// }
893/// }
894#[cfg(not(feature = "runtime"))]
895#[proc_macro_attribute]
896pub fn runtime_free_mem(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
897 panic!("should be used with runtime feature")
898}
899#[cfg(all(feature = "runtime", feature = "resource"))]
900#[proc_macro_attribute]
901pub fn runtime_free_mem(attr: TokenStream, stream: TokenStream) -> TokenStream {
902 resource::runtime_free_mem(attr, stream)
903}
904
905/// Run test case when the example running and available memory size enough
906///```rust
907/// // write as example in examples/*rs
908/// test_with::runner!(resource);
909/// #[test_with::module]
910/// mod resource {
911/// // Only works with enough available memory size
912/// #[test_with::runtime_available_mem(100GB)]
913/// fn test_ignored_available_mem_not_enough() {
914/// panic!("should be ignored")
915/// }
916/// }
917#[cfg(not(feature = "runtime"))]
918#[proc_macro_attribute]
919pub fn runtime_available_mem(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
920 panic!("should be used with runtime feature")
921}
922#[cfg(all(feature = "runtime", feature = "resource"))]
923#[proc_macro_attribute]
924pub fn runtime_available_mem(attr: TokenStream, stream: TokenStream) -> TokenStream {
925 resource::runtime_available_mem(attr, stream)
926}
927
928/// Run test case when swap size enough
929///
930/// ```
931/// #[cfg(test)]
932/// mod tests {
933///
934/// // Only works with enough swap size
935/// #[test_with::swap(100GB)]
936/// #[test]
937/// fn test_ignored() {
938/// panic!("should be ignored")
939/// }
940/// }
941/// ```
942#[proc_macro_attribute]
943#[cfg(feature = "resource")]
944pub fn swap(attr: TokenStream, stream: TokenStream) -> TokenStream {
945 if is_module(&stream) {
946 mod_macro(
947 attr,
948 parse_macro_input!(stream as ItemMod),
949 resource::check_swap_condition,
950 )
951 } else {
952 fn_macro(
953 attr,
954 parse_macro_input!(stream as ItemFn),
955 resource::check_swap_condition,
956 )
957 }
958}
959
960/// Run test case when the example running and swap enough
961///```rust
962/// // write as example in examples/*rs
963/// test_with::runner!(resource);
964/// #[test_with::module]
965/// mod resource {
966/// // Only works with enough swap size
967/// #[test_with::runtime_swap(100GB)]
968/// fn test_ignored_swap_not_enough() {
969/// panic!("should be ignored")
970/// }
971/// }
972#[cfg(not(feature = "runtime"))]
973#[proc_macro_attribute]
974pub fn runtime_swap(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
975 panic!("should be used with runtime feature")
976}
977#[cfg(all(feature = "runtime", feature = "resource"))]
978#[proc_macro_attribute]
979pub fn runtime_swap(attr: TokenStream, stream: TokenStream) -> TokenStream {
980 let swap_limitation_str = attr.to_string().replace(' ', "");
981 if byte_unit::Byte::parse_str(&swap_limitation_str, true).is_err() {
982 return syn::Error::new(
983 proc_macro2::Span::call_site(),
984 "swap size description is not correct",
985 )
986 .to_compile_error()
987 .into();
988 }
989
990 let ItemFn {
991 attrs,
992 vis,
993 sig,
994 block,
995 ..
996 } = parse_macro_input!(stream as ItemFn);
997 let syn::Signature { ident, .. } = sig.clone();
998 let check_ident = syn::Ident::new(&format!("_check_{ident}"), proc_macro2::Span::call_site());
999
1000 let check_fn = match (&sig.asyncness, &sig.output) {
1001 (Some(_), ReturnType::Default) => quote::quote! {
1002 async fn #check_ident() -> Result<test_with::Completion, test_with::Failed> {
1003 let Ok(sys) = test_with::sysinfo::System::new_with_specifics(
1004 test_with::sysinfo::RefreshKind::nothing().with_memory(test_with::sysinfo::MemoryRefreshKind::nothing().with_swap()),
1005 ) else {
1006 panic!("Fail to fetch sys info")
1007 };
1008 let swap_size = match test_with::byte_unit::Byte::parse_str(format!("{} B", sys.total_swap()), false) {
1009 Ok(b) => b,
1010 Err(_) => panic!("system swap size can not get"),
1011 };
1012 let swap_size_limitation = test_with::byte_unit::Byte::parse_str(#swap_limitation_str, true).expect("swap limitation should correct");
1013 if swap_size >= swap_size_limitation {
1014 #ident().await;
1015 Ok(test_with::Completion::Completed)
1016 } else {
1017 Ok(test_with::Completion::ignored_with(format!("because the swap less than {}", #swap_limitation_str)))
1018 }
1019 }
1020 },
1021 (Some(_), ReturnType::Type(_, _)) => quote::quote! {
1022 async fn #check_ident() -> Result<test_with::Completion, test_with::Failed> {
1023 let Ok(sys) = test_with::sysinfo::System::new_with_specifics(
1024 test_with::sysinfo::RefreshKind::nothing().with_memory(test_with::sysinfo::MemoryRefreshKind::nothing().with_swap()),
1025 ) else {
1026 panic!("Fail to fetch sys info")
1027 };
1028 let swap_size = match test_with::byte_unit::Byte::parse_str(format!("{} B", sys.total_swap()), false) {
1029 Ok(b) => b,
1030 Err(_) => panic!("system swap size can not get"),
1031 };
1032 let swap_size_limitation = test_with::byte_unit::Byte::parse_str(#swap_limitation_str, true).expect("swap limitation should correct");
1033 if swap_size >= swap_size_limitation {
1034 if let Err(e) = #ident().await {
1035 Err(format!("{e:?}").into())
1036 } else {
1037 Ok(test_with::Completion::Completed)
1038 }
1039 } else {
1040 Ok(test_with::Completion::ignored_with(format!("because the swap less than {}", #swap_limitation_str)))
1041 }
1042 }
1043 },
1044 (None, _) => quote::quote! {
1045 fn #check_ident() -> Result<test_with::Completion, test_with::Failed> {
1046 let Ok(sys) = test_with::sysinfo::System::new_with_specifics(
1047 test_with::sysinfo::RefreshKind::nothing().with_memory(test_with::sysinfo::MemoryRefreshKind::nothing().with_swap()),
1048 ) else {
1049 panic!("Fail to fetch sys info")
1050 };
1051 let swap_size = match test_with::byte_unit::Byte::parse_str(format!("{} B", sys.total_swap()), false) {
1052 Ok(b) => b,
1053 Err(_) => panic!("system swap size can not get"),
1054 };
1055 let swap_size_limitation = test_with::byte_unit::Byte::parse_str(#swap_limitation_str, true).expect("swap limitation should correct");
1056 if swap_size >= swap_size_limitation {
1057 #ident();
1058 Ok(test_with::Completion::Completed)
1059 } else {
1060 Ok(test_with::Completion::ignored_with(format!("because the swap less than {}", #swap_limitation_str)))
1061 }
1062 }
1063 },
1064 };
1065
1066 quote::quote! {
1067 #check_fn
1068 #(#attrs)*
1069 #vis #sig #block
1070 }
1071 .into()
1072}
1073
1074/// Run test case when the example running and free swap enough
1075///```rust
1076/// // write as example in examples/*rs
1077/// test_with::runner!(resource);
1078/// #[test_with::module]
1079/// mod resource {
1080/// // Only works with enough free swap size
1081/// #[test_with::runtime_free_swap(100GB)]
1082/// fn test_ignored_free_swap_not_enough() {
1083/// panic!("should be ignored")
1084/// }
1085/// }
1086#[cfg(not(feature = "runtime"))]
1087#[proc_macro_attribute]
1088pub fn runtime_free_swap(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1089 panic!("should be used with runtime feature")
1090}
1091#[cfg(all(feature = "runtime", feature = "resource"))]
1092#[proc_macro_attribute]
1093pub fn runtime_free_swap(attr: TokenStream, stream: TokenStream) -> TokenStream {
1094 resource::runtime_free_swap(attr, stream)
1095}
1096
1097/// Run test case when cpu core enough
1098///
1099/// ```
1100/// #[cfg(test)]
1101/// mod tests {
1102///
1103/// // Only works with enough cpu core
1104/// #[test_with::cpu_core(64)]
1105/// #[test]
1106/// fn test_ignored() {
1107/// panic!("should be ignored")
1108/// }
1109/// }
1110/// ```
1111#[proc_macro_attribute]
1112#[cfg(feature = "resource")]
1113pub fn cpu_core(attr: TokenStream, stream: TokenStream) -> TokenStream {
1114 if is_module(&stream) {
1115 mod_macro(
1116 attr,
1117 parse_macro_input!(stream as ItemMod),
1118 resource::check_cpu_core_condition,
1119 )
1120 } else {
1121 fn_macro(
1122 attr,
1123 parse_macro_input!(stream as ItemFn),
1124 resource::check_cpu_core_condition,
1125 )
1126 }
1127}
1128
1129/// Run test case when cpu core enough
1130///```rust
1131/// // write as example in examples/*rs
1132/// test_with::runner!(resource);
1133/// #[test_with::module]
1134/// mod resource {
1135/// // Only works with enough cpu core
1136/// #[test_with::runtime_cpu_core(32)]
1137/// fn test_ignored_core_not_enough() {
1138/// panic!("should be ignored")
1139/// }
1140/// }
1141#[cfg(not(feature = "runtime"))]
1142#[proc_macro_attribute]
1143pub fn runtime_cpu_core(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1144 panic!("should be used with runtime feature")
1145}
1146#[cfg(all(feature = "runtime", feature = "resource"))]
1147#[proc_macro_attribute]
1148pub fn runtime_cpu_core(attr: TokenStream, stream: TokenStream) -> TokenStream {
1149 resource::runtime_cpu_core(attr, stream)
1150}
1151
1152/// Run test case when physical cpu core enough
1153///
1154/// ```
1155/// #[cfg(test)]
1156/// mod tests {
1157///
1158/// // Only works with enough cpu core
1159/// #[test_with::phy_core(64)]
1160/// #[test]
1161/// fn test_ignored() {
1162/// panic!("should be ignored")
1163/// }
1164/// }
1165/// ```
1166#[proc_macro_attribute]
1167#[cfg(feature = "resource")]
1168pub fn phy_core(attr: TokenStream, stream: TokenStream) -> TokenStream {
1169 if is_module(&stream) {
1170 mod_macro(
1171 attr,
1172 parse_macro_input!(stream as ItemMod),
1173 resource::check_cpu_core_condition,
1174 )
1175 } else {
1176 fn_macro(
1177 attr,
1178 parse_macro_input!(stream as ItemFn),
1179 resource::check_phy_core_condition,
1180 )
1181 }
1182}
1183
1184/// Run test case when physical core enough
1185///```rust
1186/// // write as example in examples/*rs
1187/// test_with::runner!(resource);
1188/// #[test_with::module]
1189/// mod resource {
1190/// // Only works with enough physical cpu core
1191/// #[test_with::runtime_phy_cpu_core(32)]
1192/// fn test_ignored_phy_core_not_enough() {
1193/// panic!("should be ignored")
1194/// }
1195/// }
1196#[cfg(not(feature = "runtime"))]
1197#[proc_macro_attribute]
1198pub fn runtime_phy_cpu_core(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1199 panic!("should be used with runtime feature")
1200}
1201#[cfg(all(feature = "runtime", feature = "resource"))]
1202#[proc_macro_attribute]
1203pub fn runtime_phy_cpu_core(attr: TokenStream, stream: TokenStream) -> TokenStream {
1204 resource::runtime_phy_cpu_core(attr, stream)
1205}
1206
1207/// Run test case when the executables exist.
1208/// ```
1209/// #[cfg(test)]
1210/// mod tests {
1211/// // `pwd` executable command exists
1212/// #[test_with::executable(pwd)]
1213/// #[test]
1214/// fn test_executable() {
1215/// assert!(true);
1216/// }
1217///
1218/// // `/bin/sh` executable exists
1219/// #[test_with::executable(/bin/sh)]
1220/// #[test]
1221/// fn test_executable_with_path() {
1222/// assert!(true);
1223/// }
1224///
1225/// // `non` does not exist
1226/// #[test_with::executable(non)]
1227/// #[test]
1228/// fn test_non_existing_executable() {
1229/// panic!("should be ignored")
1230/// }
1231///
1232/// // `pwd` and `ls` exist
1233/// #[test_with::executable(pwd, ls)]
1234/// #[test]
1235/// fn test_executables_too() {
1236/// assert!(true);
1237/// }
1238///
1239/// // `non` or `ls` exist
1240/// #[test_with::executable(non || ls)]
1241/// #[test]
1242/// fn test_one_of_executables_exist() {
1243/// assert!(true);
1244/// }
1245/// }
1246/// ```
1247#[proc_macro_attribute]
1248#[cfg(feature = "executable")]
1249pub fn executable(attr: TokenStream, stream: TokenStream) -> TokenStream {
1250 if is_module(&stream) {
1251 mod_macro(
1252 attr,
1253 parse_macro_input!(stream as ItemMod),
1254 executable::check_executable_condition,
1255 )
1256 } else {
1257 fn_macro(
1258 attr,
1259 parse_macro_input!(stream as ItemFn),
1260 executable::check_executable_condition,
1261 )
1262 }
1263}
1264
1265/// Run test case when the executable existing
1266///```rust
1267/// // write as example in examples/*rs
1268/// test_with::runner!(exe);
1269/// #[test_with::module]
1270/// mod exe {
1271/// // `/bin/sh` executable exists
1272/// #[test_with::runtime_executable(/bin/sh)]
1273/// fn test_executable_with_path() {
1274/// assert!(true);
1275/// }
1276/// }
1277#[cfg(not(feature = "runtime"))]
1278#[proc_macro_attribute]
1279pub fn runtime_executable(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1280 panic!("should be used with runtime feature")
1281}
1282#[cfg(all(feature = "runtime", feature = "executable"))]
1283#[proc_macro_attribute]
1284pub fn runtime_executable(attr: TokenStream, stream: TokenStream) -> TokenStream {
1285 executable::runtime_executable(attr, stream)
1286}
1287
1288/// Ignore test case when function return some reason
1289/// The function should be `fn() -> Option<String>`
1290/// ```
1291/// test_with::runner!(custom_mod);
1292///
1293/// fn something_happened() -> Option<String> {
1294/// Some("because something happened".to_string())
1295/// }
1296///
1297/// #[test_with::module]
1298/// mod custom_mod {
1299/// #[test_with::runtime_ignore_if(something_happened)]
1300/// fn test_ignored() {
1301/// assert!(false);
1302/// }
1303/// }
1304/// ```
1305#[cfg(not(feature = "runtime"))]
1306#[proc_macro_attribute]
1307pub fn runtime_ignore_if(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1308 panic!("should be used with runtime feature")
1309}
1310#[cfg(feature = "runtime")]
1311#[proc_macro_attribute]
1312pub fn runtime_ignore_if(attr: TokenStream, stream: TokenStream) -> TokenStream {
1313 let ignore_function = syn::Ident::new(
1314 &attr.to_string().replace(' ', ""),
1315 proc_macro2::Span::call_site(),
1316 );
1317 let ItemFn {
1318 attrs,
1319 vis,
1320 sig,
1321 block,
1322 ..
1323 } = parse_macro_input!(stream as ItemFn);
1324 let syn::Signature { ident, .. } = sig.clone();
1325 let check_ident = syn::Ident::new(&format!("_check_{ident}"), proc_macro2::Span::call_site());
1326
1327 let check_fn = match (&sig.asyncness, &sig.output) {
1328 (Some(_), ReturnType::Default) => quote::quote! {
1329 async fn #check_ident() -> Result<test_with::Completion, test_with::Failed> {
1330 if let Some(msg) = #ignore_function() {
1331 Ok(test_with::Completion::ignored_with(msg))
1332 } else {
1333 #ident().await;
1334 Ok(test_with::Completion::Completed)
1335 }
1336 }
1337 },
1338 (Some(_), ReturnType::Type(_, _)) => quote::quote! {
1339 async fn #check_ident() -> Result<test_with::Completion, test_with::Failed> {
1340 if let Some(msg) = #ignore_function() {
1341 Ok(test_with::Completion::ignored_with(msg))
1342 } else {
1343 if let Err(e) = #ident().await {
1344 Err(format!("{e:?}").into())
1345 } else {
1346 Ok(test_with::Completion::Completed)
1347 }
1348 }
1349 }
1350 },
1351 (None, _) => quote::quote! {
1352 fn #check_ident() -> Result<test_with::Completion, test_with::Failed> {
1353 if let Some(msg) = #ignore_function() {
1354 Ok(test_with::Completion::ignored_with(msg))
1355 } else {
1356 #ident();
1357 Ok(test_with::Completion::Completed)
1358 }
1359 }
1360 },
1361 };
1362
1363 quote::quote! {
1364 #check_fn
1365 #(#attrs)*
1366 #vis #sig #block
1367 }
1368 .into()
1369}
1370
1371/// Run test case one by one when the lock is acquired
1372/// It will automatically implement a file lock for the test case to prevent it run in the same
1373/// time. Also, you can pass the second parameter to specific the waiting seconds, default will be
1374/// 60 seconds.
1375/// ```
1376/// #[cfg(test)]
1377/// mod tests {
1378///
1379/// // `LOCK` is file based lock to prevent test1 an test2 run at the same time
1380/// #[test_with::lock(LOCK)]
1381/// #[test]
1382/// fn test_1() {
1383/// assert!(true);
1384/// }
1385///
1386/// // `LOCK` is file based lock to prevent test1 an test2 run at the same time
1387/// #[test_with::lock(LOCK)]
1388/// #[test]
1389/// fn test_2() {
1390/// assert!(true);
1391/// }
1392///
1393/// // `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
1394/// // waiting time.
1395/// #[test_with::lock(ANOTHER_LOCK, 3)]
1396/// fn test_3() {
1397/// assert!(true);
1398/// }
1399///
1400/// // `ANOTHER_LOCK` is file based lock to prevent test3 an test4 run at the same time with 3 sec
1401/// // waiting time.
1402/// #[test_with::lock(ANOTHER_LOCK, 3)]
1403/// fn test_4() {
1404/// assert!(true);
1405/// }
1406///
1407/// }
1408#[proc_macro_attribute]
1409pub fn lock(attr: TokenStream, stream: TokenStream) -> TokenStream {
1410 if is_module(&stream) {
1411 return syn::Error::new(
1412 proc_macro2::Span::call_site(),
1413 "#[test_with::lock] only works with fn",
1414 )
1415 .to_compile_error()
1416 .into();
1417 } else {
1418 lock_macro(attr, parse_macro_input!(stream as ItemFn))
1419 }
1420}
1421
1422/// Run test case when the timezone is expected.
1423/// ```
1424/// #[cfg(test)]
1425/// mod tests {
1426///
1427/// // 0 means UTC
1428/// #[test_with::timezone(0)]
1429/// #[test]
1430/// fn test_works() {
1431/// assert!(true);
1432/// }
1433///
1434/// // UTC is GMT+0
1435/// #[test_with::timezone(UTC)]
1436/// #[test]
1437/// fn test_works_too() {
1438/// assert!(true);
1439/// }
1440///
1441/// // +8 means GMT+8
1442/// #[test_with::timezone(+8)]
1443/// #[test]
1444/// fn test_ignored() {
1445/// panic!("should be ignored")
1446/// }
1447///
1448/// // HKT GMT+8
1449/// #[test_with::timezone(HKT)]
1450/// #[test]
1451/// fn test_ignored_too() {
1452/// panic!("should be ignored")
1453/// }
1454/// }
1455/// ```
1456#[cfg(feature = "timezone")]
1457#[proc_macro_attribute]
1458pub fn timezone(attr: TokenStream, stream: TokenStream) -> TokenStream {
1459 if is_module(&stream) {
1460 mod_macro(
1461 attr,
1462 parse_macro_input!(stream as ItemMod),
1463 timezone::check_tz_condition,
1464 )
1465 } else {
1466 fn_macro(
1467 attr,
1468 parse_macro_input!(stream as ItemFn),
1469 timezone::check_tz_condition,
1470 )
1471 }
1472}
1473
1474/// Run test case when the example running within specific timezones.
1475///```rust
1476/// // write as example in examples/*rs
1477/// test_with::runner!(timezone);
1478/// #[test_with::module]
1479/// mod timezone {
1480/// // 0 means UTC timezone
1481/// #[test_with::runtime_timezone(0)]
1482/// fn test_works() {
1483/// assert!(true);
1484/// }
1485/// }
1486#[cfg(not(feature = "runtime"))]
1487#[proc_macro_attribute]
1488pub fn runtime_timezone(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1489 return syn::Error::new(
1490 proc_macro2::Span::call_site(),
1491 "should be used with runtime feature",
1492 )
1493 .to_compile_error()
1494 .into();
1495}
1496
1497#[cfg(all(feature = "runtime", feature = "timezone"))]
1498#[proc_macro_attribute]
1499pub fn runtime_timezone(attr: TokenStream, stream: TokenStream) -> TokenStream {
1500 timezone::runtime_timezone(attr, stream)
1501}
1502
1503/// Provide a test runner and test on each module
1504///```rust
1505/// // example/run-test.rs
1506///
1507/// test_with::runner!(module1, module2);
1508/// #[test_with::module]
1509/// mod module1 {
1510/// #[test_with::runtime_env(PWD)]
1511/// fn test_works() {
1512/// assert!(true);
1513/// }
1514/// }
1515///
1516/// #[test_with::module]
1517/// mod module2 {
1518/// #[test_with::runtime_env(PWD)]
1519/// fn test_works() {
1520/// assert!(true);
1521/// }
1522/// }
1523///```
1524#[cfg(not(feature = "runtime"))]
1525#[proc_macro]
1526pub fn runner(_input: TokenStream) -> TokenStream {
1527 return syn::Error::new(
1528 proc_macro2::Span::call_site(),
1529 "should be used with runtime feature",
1530 )
1531 .to_compile_error()
1532 .into();
1533}
1534#[cfg(feature = "runtime")]
1535#[proc_macro]
1536pub fn runner(input: TokenStream) -> TokenStream {
1537 runtime::runner(input)
1538}
1539
1540#[cfg(not(feature = "runtime"))]
1541#[proc_macro]
1542pub fn tokio_runner(_input: TokenStream) -> TokenStream {
1543 return syn::Error::new(
1544 proc_macro2::Span::call_site(),
1545 "should be used with `runtime` feature",
1546 )
1547 .to_compile_error()
1548 .into();
1549}
1550#[cfg(feature = "runtime")]
1551#[proc_macro]
1552pub fn tokio_runner(input: TokenStream) -> TokenStream {
1553 runtime::tokio_runner(input)
1554}
1555
1556/// Help each function with `#[test_with::runtime_*]` in the module can register to run
1557/// Also you can set up a mock instance for all of the test in the module
1558///
1559/// ```rust
1560/// // example/run-test.rs
1561///
1562/// test_with::runner!(module1, module2);
1563/// #[test_with::module]
1564/// mod module1 {
1565/// #[test_with::runtime_env(PWD)]
1566/// fn test_works() {
1567/// assert!(true);
1568/// }
1569/// }
1570///
1571/// #[test_with::module]
1572/// mod module2 {
1573/// #[test_with::runtime_env(PWD)]
1574/// fn test_works() {
1575/// assert!(true);
1576/// }
1577/// }
1578/// ```
1579/// You can set up mock with a public struct named `TestEnv` inside the module, or a public type
1580/// named `TestEnv` inside the module. And the type or struct should have a Default trait for
1581/// initialize the mock instance.
1582/// ```rust
1583/// use std::ops::Drop;
1584/// use std::process::{Child, Command};
1585///
1586/// test_with::runner!(net);
1587///
1588/// #[test_with::module]
1589/// mod net {
1590/// pub struct TestEnv {
1591/// p: Child,
1592/// }
1593///
1594/// impl Default for TestEnv {
1595/// fn default() -> TestEnv {
1596/// let p = Command::new("python")
1597/// .args(["-m", "http.server"])
1598/// .spawn()
1599/// .expect("failed to execute child");
1600/// let mut count = 0;
1601/// while count < 3 {
1602/// if test_with::reqwest::blocking::get("http://127.0.0.1:8000").is_ok() {
1603/// break;
1604/// }
1605/// std::thread::sleep(std::time::Duration::from_secs(1));
1606/// count += 1;
1607/// }
1608/// TestEnv { p }
1609/// }
1610/// }
1611///
1612/// impl Drop for TestEnv {
1613/// fn drop(&mut self) {
1614/// self.p.kill().expect("fail to kill python http.server");
1615/// }
1616/// }
1617///
1618/// #[test_with::runtime_http(127.0.0.1:8000)]
1619/// fn test_with_environment() {
1620/// assert!(true);
1621/// }
1622/// }
1623///
1624/// ```
1625/// or you can write mock struct in other place and just pass by type.
1626/// ```rust
1627/// use std::ops::Drop;
1628/// use std::process::{Child, Command};
1629///
1630/// test_with::runner!(net);
1631///
1632/// pub struct Moc {
1633/// p: Child,
1634/// }
1635///
1636/// impl Default for Moc {
1637/// fn default() -> Moc {
1638/// let p = Command::new("python")
1639/// .args(["-m", "http.server"])
1640/// .spawn()
1641/// .expect("failed to execute child");
1642/// let mut count = 0;
1643/// while count < 3 {
1644/// if test_with::reqwest::blocking::get("http://127.0.0.1:8000").is_ok() {
1645/// break;
1646/// }
1647/// std::thread::sleep(std::time::Duration::from_secs(1));
1648/// count += 1;
1649/// }
1650/// Moc { p }
1651/// }
1652/// }
1653///
1654/// impl Drop for Moc {
1655/// fn drop(&mut self) {
1656/// self.p.kill().expect("fail to kill python http.server");
1657/// }
1658/// }
1659///
1660/// #[test_with::module]
1661/// mod net {
1662/// pub type TestEnv = super::Moc;
1663///
1664/// #[test_with::runtime_http(127.0.0.1:8000)]
1665/// fn test_with_environment() {
1666/// assert!(true);
1667/// }
1668/// }
1669/// ```
1670#[cfg(not(feature = "runtime"))]
1671#[proc_macro_attribute]
1672pub fn module(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
1673 panic!("should be used with runtime feature")
1674}
1675#[cfg(feature = "runtime")]
1676#[proc_macro_attribute]
1677pub fn module(attr: TokenStream, stream: TokenStream) -> TokenStream {
1678 runtime::module(attr, stream)
1679}