use crate::{
config::Backend,
ir,
};
use derive_more::From;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
const DEFAULT_CONTRACTS_NODE: &str = "substrate-contracts-node";
#[derive(From)]
pub struct InkE2ETest {
test: ir::InkE2ETest,
}
impl InkE2ETest {
pub fn generate_code(&self) -> TokenStream2 {
#[cfg(clippy)]
if true {
return quote! {}
}
let item_fn = &self.test.item_fn.item_fn;
let fn_name = &item_fn.sig.ident;
let block = &item_fn.block;
let fn_return_type = &item_fn.sig.output;
let vis = &item_fn.vis;
let attrs = &item_fn.attrs;
let ret = match fn_return_type {
syn::ReturnType::Default => quote! {},
syn::ReturnType::Type(rarrow, ret_type) => quote! { #rarrow #ret_type },
};
let environment = self
.test
.config
.environment()
.unwrap_or_else(|| syn::parse_quote! { ::ink::env::DefaultEnvironment });
let additional_contracts = self.test.config.additional_contracts();
let exec_build_contracts = if additional_contracts.is_empty() {
quote! {
::ink_e2e::build_root_and_contract_dependencies()
}
} else {
quote! {
::ink_e2e::build_root_and_additional_contracts([ #( #additional_contracts ),* ])
}
};
let client_building = match self.test.config.backend() {
Backend::Full => build_full_client(&environment, exec_build_contracts),
#[cfg(any(test, feature = "drink"))]
Backend::RuntimeOnly => {
build_runtime_client(exec_build_contracts, self.test.config.runtime())
}
};
quote! {
#( #attrs )*
#[test]
#vis fn #fn_name () #ret {
use ::ink_e2e::log_info;
::ink_e2e::LOG_PREFIX.with(|log_prefix| {
let str = format!("test: {}", stringify!(#fn_name));
*log_prefix.borrow_mut() = String::from(str);
});
log_info("setting up e2e test");
::ink_e2e::INIT.call_once(|| {
::ink_e2e::tracing_subscriber::fmt::init();
});
log_info("creating new client");
let run = async {
#client_building
let __ret = {
#block
};
__ret
};
{
return ::ink_e2e::tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap_or_else(|err| panic!("Failed building the Runtime: {err}"))
.block_on(run);
}
}
}
}
}
fn build_full_client(environment: &syn::Path, contracts: TokenStream2) -> TokenStream2 {
let contracts_node: &'static str =
option_env!("CONTRACTS_NODE").unwrap_or(DEFAULT_CONTRACTS_NODE);
if which::which(contracts_node).is_err() {
if contracts_node == DEFAULT_CONTRACTS_NODE {
panic!(
"The '{DEFAULT_CONTRACTS_NODE}' executable was not found. Install '{DEFAULT_CONTRACTS_NODE}' on the PATH, \
or specify the `CONTRACTS_NODE` environment variable.",
)
} else {
panic!("The contracts node executable '{contracts_node}' was not found.")
}
}
quote! {
let node_proc = ::ink_e2e::TestNodeProcess::<::ink_e2e::PolkadotConfig>
::build(#contracts_node)
.spawn()
.await
.unwrap_or_else(|err|
::core::panic!("Error spawning substrate-contracts-node: {err:?}")
);
let contracts = #contracts;
let mut client = ::ink_e2e::Client::<
::ink_e2e::PolkadotConfig,
#environment
>::new(node_proc.client(), contracts).await;
}
}
#[cfg(any(test, feature = "drink"))]
fn build_runtime_client(
contracts: TokenStream2,
runtime: Option<syn::Path>,
) -> TokenStream2 {
let runtime =
runtime.unwrap_or_else(|| syn::parse_quote! { ::ink_e2e::MinimalRuntime });
quote! {
let contracts = #contracts;
let mut client = ::ink_e2e::DrinkClient::<_, _, #runtime>::new(contracts);
}
}