Skip to content

Commit

Permalink
add runtime_root
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Sep 21, 2023
1 parent 2a98db4 commit 4a14014
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/runner/examples/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,11 @@ mod net {
assert!(true);
}
}

#[test_with::module]
mod user {
#[test_with::runtime_root()]
fn test_ignored_by_normal_user() {
panic!("should be ignored")
}
}
49 changes: 49 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,55 @@ fn check_root_condition(_attr_str: String) -> (bool, String) {
)
}

/// Run test case when runner is root
///```rust
/// // write as example in exmaples/*rs
/// test_with::runner!(user);
/// #[test_with::module]
/// mod user {
/// // Google DNS is online
/// #[test_with::runtime_root()]
/// fn test_ignored() {
/// panic!("should be ignored")
/// }
/// }
#[cfg(not(feature = "runtime"))]
pub fn runtime_root(_attr: TokenStream, _stream: TokenStream) -> TokenStream {
panic!("should be used with runtime feature")
}
#[cfg(all(feature = "runtime", feature = "user", not(target_os = "windows")))]
#[proc_macro_attribute]
#[proc_macro_error]
pub fn runtime_root(attr: TokenStream, stream: TokenStream) -> TokenStream {
let attr_str = attr.to_string().replace(' ', "");
let sockets: 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> {
if 0 = libtest_with::users::get_current_uid() {
#ident()
} else {
Err(format!("{}because this case should run with root", libtest_with::RUNTIME_IGNORE_PREFIX).into()),
}
Ok(())
}

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

/// Run test case when runner in group
///
/// ```
Expand Down

0 comments on commit 4a14014

Please sign in to comment.