Skip to content

Commit

Permalink
add runtime_http
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 26, 2023
1 parent e30d6d5 commit c82ca76
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
10 changes: 9 additions & 1 deletion examples/runner/examples/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_with::runner!(env, file, path);
test_with::runner!(env, file, path, net);

#[test_with::module]
mod env {
Expand Down Expand Up @@ -58,3 +58,11 @@ mod path {
assert!(true);
}
}

#[test_with::module]
mod net {
#[test_with::runtime_http(httpbin.org)]
fn test_works() {
assert!(true);
}
}
63 changes: 63 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,69 @@ fn check_http_condition(attr_str: String) -> (bool, String) {
(missing_links.is_empty(), ignore_msg)
}

/// Run test case when the example running and the http service exist.
///```rust
/// // write as example in exmaples/*rs
/// test_with::runner!(http);
/// #[test_with::module]
/// mod http {
/// #[test_with::runtime_http(httpbin.org)]
/// fn test_works() {
/// assert!(true);
/// }
/// }
#[cfg(not(feature = "runtime"))]
pub fn runtime_http(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
panic!("should be used with runtime feature")
}

#[cfg(all(feature = "runtime", feature = "http"))]
#[proc_macro_attribute]
#[proc_macro_error]
pub fn runtime_http(attr: TokenStream, stream: TokenStream) -> TokenStream {
let attr_str = attr.to_string().replace(' ', "");
let links: Vec<&str> = attr_str.split(',').collect();
let ItemFn {
attrs,
vis,
sig,
block,
} = parse_macro_input!(stream as ItemFn);
let syn::Signature { ident, .. } = sig.clone();
let check_ident = syn::Ident::new(
&format!("_check_{}", ident.to_string()),
proc_macro2::Span::call_site(),
);
quote::quote! {
fn #check_ident() -> Result<(), libtest_with::Failed> {

let mut missing_links = vec![];
let client = reqwest::blocking::Client::new();
#(
if client.head(&format!("http://{}", #links)).send().is_err() {
missing_links.push(format!("http://{}", #links));
}
)*
match missing_vars.len() {
0 => #ident(),
1 => return Err(
format!("{}because {} not response",
libtest_with::RUNTIME_IGNORE_PREFIX, missing_links[0]
).into()),
_ => return Err(
format!("{}because following links not response: \n{}\n",
libtest_with::RUNTIME_IGNORE_PREFIX, missing_links.join(", ")
).into()),
}
Ok(())
}

#(#attrs)*
#vis #sig #block
}
.into()
}

/// Run test case when the https service exist.
/// ```
/// #[cfg(test)]
Expand Down

0 comments on commit c82ca76

Please sign in to comment.