Skip to content

Commit

Permalink
export xtest inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
devrandom committed Dec 28, 2024
1 parent f880aad commit 1d28174
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ext-functional-test-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
println!("Hello, world!");
println!("{} tests were exported", lightning::get_xtests().len());
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions ext-test-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ proc-macro = true
syn = { version = "1.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"

[dev-dependencies]
inventory = "0.3"
58 changes: 52 additions & 6 deletions ext-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
}
},
Item::Fn(item_fn) => {
let cfg_attr = if attrs.is_empty() {
// export the test if not actually testing
quote! { #[cfg_attr(test, test)] }
let (cfg_attr, submit_attr) = if attrs.is_empty() {
(quote! { #[cfg_attr(test, test)] }, quote! { #[cfg(not(test))] })
} else {
// export the test if the feature is enabled
quote! { #[cfg_attr(test, test)] #[cfg(any(test, #attrs))] }
(
quote! { #[cfg_attr(test, test)] #[cfg(any(test, #attrs))] },
quote! { #[cfg(all(not(test), #attrs))] },
)
};

// Check that the function doesn't take args and returns nothing
if !item_fn.sig.inputs.is_empty() || !matches!(item_fn.sig.output, syn::ReturnType::Default) {
if !item_fn.sig.inputs.is_empty()
|| !matches!(item_fn.sig.output, syn::ReturnType::Default)
{
return syn::Error::new_spanned(
item_fn.sig,
"xtest functions must not take arguments and must return nothing",
Expand All @@ -74,9 +77,20 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
.into();
}

let fn_name = &item_fn.sig.ident;
let fn_name_str = fn_name.to_string();
quote! {
#cfg_attr
#item_fn

// We submit the test to the inventory only if we're not actually testing
#submit_attr
inventory::submit! {
crate::XTestItem {
test_fn: #fn_name,
test_name: #fn_name_str,
}
}
}
},
_ => {
Expand All @@ -91,3 +105,35 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {

TokenStream::from(expanded)
}

#[proc_macro]
pub fn xtest_inventory(_input: TokenStream) -> TokenStream {
let expanded = quote! {
pub struct XTestItem {
pub test_fn: fn(),
pub test_name: &'static str,
}

inventory::collect!(XTestItem);

pub fn get_xtests() -> Vec<&'static XTestItem> {
inventory::iter::<XTestItem>
.into_iter()
.collect()
}

#[macro_export]
macro_rules! xtest_inventory {
($test_fn:expr, $test_name:expr) => {
inventory::submit! {
XTestItem {
test_fn: $test_fn,
test_name: $test_name,
}
}
};
}
};

TokenStream::from(expanded)
}
3 changes: 2 additions & 1 deletion lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
# Internal test utilities exposed to other repo crates
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "ext-test-macro", "lightning-types/_test_utils"]
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "ext-test-macro", "lightning-types/_test_utils", "inventory"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
Expand Down Expand Up @@ -48,6 +48,7 @@ backtrace = { version = "0.3", optional = true }
libm = { version = "0.2", default-features = false }
delegate = "0.12.0"
ext-test-macro = { path = "../ext-test-macro", optional = true }
inventory = { version = "0.3", optional = true }

[dev-dependencies]
regex = "1.5.6"
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@ mod prelude {
extern crate backtrace;

mod sync;

#[cfg(feature = "_test_utils")]
ext_test_macro::xtest_inventory!();

0 comments on commit 1d28174

Please sign in to comment.