From 5f7d24f9e1f4eccd0f5d048452d298ebecf70917 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Wed, 13 Nov 2024 11:57:21 +0200 Subject: [PATCH 01/12] gen snippets - remove empty lines --- .../contract/generate_snippets/snippet_sc_functions_gen.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs index b5789c9645..87b525e591 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs @@ -65,7 +65,6 @@ fn write_deploy_method_impl(file: &mut File, init_abi: &EndpointAbi, name: &Stri .init({}) .code(&self.contract_code) .returns(ReturnsNewAddress) - .run() .await; let new_address_bech32 = bech32::encode(&new_address); @@ -101,7 +100,6 @@ fn write_upgrade_endpoint_impl(file: &mut File, upgrade_abi: &EndpointAbi, name: .code(&self.contract_code) .code_metadata(CodeMetadata::UPGRADEABLE) .returns(ReturnsNewAddress) - .run() .await; @@ -214,7 +212,6 @@ fn write_contract_call(file: &mut File, endpoint_abi: &EndpointAbi, name: &Strin .typed(proxy::{}Proxy) .{}({}){} .returns(ReturnsResultUnmanaged) - .run() .await; @@ -237,7 +234,6 @@ fn write_contract_query(file: &mut File, endpoint_abi: &EndpointAbi, name: &Stri .typed(proxy::{}Proxy) .{}({}) .returns(ReturnsResultUnmanaged) - .run() .await; From bc81c9d978be5bf97ec0ef51ff7aace63c4b3d0e Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Wed, 13 Nov 2024 13:32:54 +0200 Subject: [PATCH 02/12] gen snippets - format config.rs --- .../generate_snippets/snippet_template_gen.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index 95ecbd64df..44ef570d98 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -174,8 +174,6 @@ const CONFIG_FILE: &str = \"config.toml\"; " ) .unwrap(); - - write_newline(file); } pub(crate) fn write_config_struct_declaration(file: &mut File) { @@ -186,19 +184,17 @@ pub(crate) fn write_config_struct_declaration(file: &mut File) { pub enum ChainType {{ Real, Simulator, - }} +}} /// Contract Interact configuration #[derive(Debug, Deserialize)] pub struct Config {{ pub gateway_uri: String, pub chain_type: ChainType, - }} +}} "# ) .unwrap(); - - write_newline(file); } pub(crate) fn write_config_struct_impl(file: &mut File) { @@ -217,7 +213,7 @@ pub(crate) fn write_config_struct_impl(file: &mut File) { Config {{ gateway_uri: "http://localhost:8085".to_owned(), chain_type: ChainType::Simulator, - }} + }} }} // Returns the gateway URI @@ -230,9 +226,9 @@ pub(crate) fn write_config_struct_impl(file: &mut File) { match self.chain_type {{ ChainType::Real => false, ChainType::Simulator => true, + }} }} - }} - }} +}} "# ) .unwrap(); From 8a490c014be726799c1f229e75cd642ef0933fae Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Wed, 13 Nov 2024 13:40:19 +0200 Subject: [PATCH 03/12] gen snippets - remove extra lines --- .../src/contract/generate_snippets/snippet_template_gen.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index 44ef570d98..7671596f7c 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -162,8 +162,6 @@ use std::io::Read; " ) .unwrap(); - - write_newline(file); } pub(crate) fn write_config_constants(file: &mut File) { @@ -228,10 +226,7 @@ pub(crate) fn write_config_struct_impl(file: &mut File) { ChainType::Simulator => true, }} }} -}} -"# +}}"# ) .unwrap(); - - write_newline(file); } From 653cd8a0b07f7fa84f2e432a60a050b1515e2a21 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Wed, 13 Nov 2024 17:17:30 +0100 Subject: [PATCH 04/12] generate lib and main files, support for custom tests folder --- .../generate_snippets/snippet_crate_gen.rs | 31 ++++++++++++++++--- .../generate_snippets/snippet_gen_main.rs | 7 ++--- .../generate_snippets/snippet_template_gen.rs | 15 +++------ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs index d92ec69aeb..529282104c 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs @@ -7,6 +7,7 @@ use std::{ use crate::version_history; static SNIPPETS_SOURCE_FILE_NAME: &str = "interactor_main.rs"; +static LIB_SOURCE_FILE_NAME: &str = "interact.rs"; static SC_CONFIG_PATH: &str = "../sc-config.toml"; static CONFIG_TOML_PATH: &str = "config.toml"; static CONFIG_SOURCE_FILE_NAME: &str = "config.rs"; @@ -77,6 +78,9 @@ publish = false name = "rust-interact" path = "src/{SNIPPETS_SOURCE_FILE_NAME}" +[lib] +path = "src/{LIB_SOURCE_FILE_NAME}" + [dependencies.{contract_crate_name}] path = ".." @@ -91,9 +95,8 @@ clap = {{ version = "4.4.7", features = ["derive"] }} serde = {{ version = "1.0", features = ["derive"] }} toml = "0.8.6" -# uncomment when using chain simulator -# [features] -# chain-simulator-tests = [] +[features] +chain-simulator-tests = [] "# ) .unwrap(); @@ -107,7 +110,7 @@ pub(crate) fn create_src_folder(snippets_folder_path: &str) { #[must_use] pub(crate) fn create_and_get_lib_file(snippets_folder_path: &str, overwrite: bool) -> File { - let lib_path = format!("{snippets_folder_path}/src/{SNIPPETS_SOURCE_FILE_NAME}"); + let lib_path = format!("{snippets_folder_path}/src/{LIB_SOURCE_FILE_NAME}"); if overwrite { File::create(&lib_path).unwrap() } else { @@ -125,6 +128,26 @@ pub(crate) fn create_and_get_lib_file(snippets_folder_path: &str, overwrite: boo } } +pub(crate) fn create_main_file(snippets_folder_path: &str, contract_crate_name: &str) { + let lib_path = format!("{snippets_folder_path}/src/{SNIPPETS_SOURCE_FILE_NAME}"); + + let mut file = File::create(&lib_path).unwrap(); + + writeln!( + &mut file, + r#" +use multiversx_sc_snippets::imports::*; +use rust_interact::{contract_crate_name}_cli; + +#[tokio::main] +async fn main() {{ + {contract_crate_name}_cli().await; +}} +"# + ) + .unwrap(); +} + pub(crate) fn create_sc_config_file(overwrite: bool) { // check if the file should be overwritten or if it already exists let mut file = if overwrite || !file_exists(SC_CONFIG_PATH) { diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs index c566dd49b6..d29c1d5f8d 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs @@ -7,9 +7,7 @@ use crate::cli::GenerateSnippetsArgs; use super::{ super::meta_config::MetaConfig, snippet_crate_gen::{ - create_and_get_lib_file, create_config_rust_file, create_config_toml_file, - create_sc_config_file, create_snippets_cargo_toml, create_snippets_folder, - create_snippets_gitignore, create_src_folder, + create_and_get_lib_file, create_config_rust_file, create_config_toml_file, create_main_file, create_sc_config_file, create_snippets_cargo_toml, create_snippets_folder, create_snippets_gitignore, create_src_folder }, snippet_sc_functions_gen::write_interact_struct_impl, snippet_template_gen::{ @@ -43,6 +41,7 @@ fn create_snippets_crate_and_get_lib_file( create_snippets_cargo_toml(snippets_folder_path, contract_crate_name, overwrite); create_src_folder(snippets_folder_path); create_sc_config_file(overwrite); + create_main_file(snippets_folder_path, contract_crate_name); create_and_get_lib_file(snippets_folder_path, overwrite) } @@ -55,7 +54,7 @@ fn create_config_and_get_file(snippets_folder_path: &str) -> File { fn write_snippets_to_file(file: &mut File, abi: &ContractAbi, crate_name: &str) { write_snippet_imports(file); write_snippet_constants(file); - write_snippet_main_function(file, abi); + write_snippet_main_function(file, abi, crate_name); write_state_struct_declaration(file); write_snippet_state_impl(file); write_interact_struct_declaration(file); diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index 7671596f7c..0c9b2e621b 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -9,8 +9,8 @@ pub(crate) fn write_snippet_imports(file: &mut File) { file, "#![allow(non_snake_case)] -mod proxy; mod config; +mod proxy; use config::Config; use multiversx_sc_snippets::imports::*; @@ -18,8 +18,7 @@ use serde::{{Deserialize, Serialize}}; use std::{{ io::{{Read, Write}}, path::Path, -}}; -" +}};" ) .unwrap(); @@ -28,15 +27,13 @@ use std::{{ pub(crate) fn write_snippet_constants(file: &mut File) { writeln!(file, "const STATE_FILE: &str = \"state.toml\";").unwrap(); - - write_newline(file); } -pub(crate) fn write_snippet_main_function(file: &mut File, abi: &ContractAbi) { +pub(crate) fn write_snippet_main_function(file: &mut File, abi: &ContractAbi, crate_name: &str) { writeln!( file, - "#[tokio::main] -async fn main() {{ + " +pub async fn {crate_name}_cli() {{ env_logger::init(); let mut args = std::env::args(); @@ -76,8 +73,6 @@ async fn main() {{ }}" ) .unwrap(); - - write_newline(file); } pub(crate) fn write_interact_struct_declaration(file: &mut File) { From a3d27cca40ada4d5fc4e0e5ed7bb1244173420d5 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Wed, 13 Nov 2024 17:40:56 +0100 Subject: [PATCH 05/12] fmt + clippy --- .../src/contract/generate_snippets/snippet_crate_gen.rs | 2 +- .../src/contract/generate_snippets/snippet_gen_main.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs index 529282104c..f7f0e923a1 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs @@ -131,7 +131,7 @@ pub(crate) fn create_and_get_lib_file(snippets_folder_path: &str, overwrite: boo pub(crate) fn create_main_file(snippets_folder_path: &str, contract_crate_name: &str) { let lib_path = format!("{snippets_folder_path}/src/{SNIPPETS_SOURCE_FILE_NAME}"); - let mut file = File::create(&lib_path).unwrap(); + let mut file = File::create(lib_path).unwrap(); writeln!( &mut file, diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs index d29c1d5f8d..ea3a711fae 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs @@ -7,7 +7,9 @@ use crate::cli::GenerateSnippetsArgs; use super::{ super::meta_config::MetaConfig, snippet_crate_gen::{ - create_and_get_lib_file, create_config_rust_file, create_config_toml_file, create_main_file, create_sc_config_file, create_snippets_cargo_toml, create_snippets_folder, create_snippets_gitignore, create_src_folder + create_and_get_lib_file, create_config_rust_file, create_config_toml_file, + create_main_file, create_sc_config_file, create_snippets_cargo_toml, + create_snippets_folder, create_snippets_gitignore, create_src_folder, }, snippet_sc_functions_gen::write_interact_struct_impl, snippet_template_gen::{ From bb5198ea74cd98d6215b5ff0c27f4ddff41e80b1 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Wed, 13 Nov 2024 18:03:21 +0100 Subject: [PATCH 06/12] public functions --- .../contract/generate_snippets/snippet_sc_functions_gen.rs | 4 ++-- .../src/contract/generate_snippets/snippet_template_gen.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs index 87b525e591..8d6d850aaa 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs @@ -12,7 +12,7 @@ pub(crate) fn write_interact_struct_impl(file: &mut File, abi: &ContractAbi, cra writeln!( file, r#"impl ContractInteract {{ - async fn new() -> Self {{ + pub async fn new() -> Self {{ let config = Config::new(); let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()).await; interactor.set_current_dir_from_workspace("{}"); @@ -130,7 +130,7 @@ fn write_endpoint_impl(file: &mut File, endpoint_abi: &EndpointAbi, name: &Strin } fn write_method_declaration(file: &mut File, endpoint_name: &str) { - writeln!(file, " async fn {endpoint_name}(&mut self) {{").unwrap(); + writeln!(file, " pub async fn {endpoint_name}(&mut self) {{").unwrap(); } fn write_payments_declaration(file: &mut File, accepted_tokens: &[String]) { diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index 0c9b2e621b..17469c9c1a 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -78,7 +78,7 @@ pub async fn {crate_name}_cli() {{ pub(crate) fn write_interact_struct_declaration(file: &mut File) { writeln!( file, - "struct ContractInteract {{ + "pub struct ContractInteract {{ interactor: Interactor, wallet_address: Address, contract_code: BytesValue, @@ -95,7 +95,7 @@ pub(crate) fn write_state_struct_declaration(file: &mut File) { file, " #[derive(Debug, Default, Serialize, Deserialize)] -struct State {{ +pub struct State {{ contract_address: Option }}" ) From c67c70ba509e04326b1ddd6e400cdc37e1a07ff8 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 14 Nov 2024 12:37:20 +0100 Subject: [PATCH 07/12] interactor syntax fix --- .../generate_snippets/snippet_sc_functions_gen.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs index 8d6d850aaa..a883d9c3f1 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs @@ -14,10 +14,16 @@ pub(crate) fn write_interact_struct_impl(file: &mut File, abi: &ContractAbi, cra r#"impl ContractInteract {{ pub async fn new() -> Self {{ let config = Config::new(); - let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()).await; - interactor.set_current_dir_from_workspace("{}"); + let mut interactor = Interactor::new(config.gateway_uri()) + .await + .use_chain_simulator(config.use_chain_simulator()); + interactor.set_current_dir_from_workspace("{}"); let wallet_address = interactor.register_wallet(test_wallets::alice()).await; + + // Useful in the chain simulator setting + // generate blocks until ESDTSystemSCAddress is enabled + interactor.generate_blocks_until_epoch(1).await.unwrap(); let contract_code = BytesValue::interpret_from( {}, From 46e9cdb01304c63fb8c6267e235e222b29c357cb Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 14 Nov 2024 13:59:06 +0100 Subject: [PATCH 08/12] tests folder and files gen --- .../generate_snippets/snippet_crate_gen.rs | 21 ++++++++++ .../generate_snippets/snippet_gen_main.rs | 22 ++++++++++- .../generate_snippets/snippet_template_gen.rs | 39 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs index f7f0e923a1..74ed9d4115 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs @@ -2,6 +2,7 @@ use colored::Colorize; use std::{ fs::{self, File, OpenOptions}, io::Write, + path::Path, }; use crate::version_history; @@ -15,6 +16,8 @@ static FULL_PROXY_ENTRY: &str = r#"[[proxy]] path = "interactor/src/proxy.rs" "#; static PROXY_PATH: &str = "interactor/src/proxy.rs"; +static INTERACTOR_CS_TEST_FILE_NAME: &str = "interact_cs_tests.rs"; +static INTERACTOR_TEST_FILE_NAME: &str = "interact_tests.rs"; pub(crate) fn create_snippets_folder(snippets_folder_path: &str) { // returns error if folder already exists, so we ignore the result @@ -148,6 +151,24 @@ async fn main() {{ .unwrap(); } +pub(crate) fn create_test_folder_and_get_files(snippets_folder_path: &str) -> (File, File) { + let folder_path = format!("{snippets_folder_path}/tests"); + + if !Path::new(&folder_path).exists() { + fs::create_dir_all(&folder_path).expect("Failed to create tests directory"); + } + + let interactor_file_path = format!("{folder_path}/{INTERACTOR_TEST_FILE_NAME}"); + let interactor_cs_file_path = format!("{folder_path}/{INTERACTOR_CS_TEST_FILE_NAME}"); + + let interactor_file = + File::create(&interactor_file_path).expect("Failed to create interact_tests.rs file"); + let interactor_cs_file = + File::create(&interactor_cs_file_path).expect("Failed to create interact_cs_tests.rs file"); + + (interactor_file, interactor_cs_file) +} + pub(crate) fn create_sc_config_file(overwrite: bool) { // check if the file should be overwritten or if it already exists let mut file = if overwrite || !file_exists(SC_CONFIG_PATH) { diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs index ea3a711fae..1c77340c64 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs @@ -10,11 +10,13 @@ use super::{ create_and_get_lib_file, create_config_rust_file, create_config_toml_file, create_main_file, create_sc_config_file, create_snippets_cargo_toml, create_snippets_folder, create_snippets_gitignore, create_src_folder, + create_test_folder_and_get_files, }, snippet_sc_functions_gen::write_interact_struct_impl, snippet_template_gen::{ - write_config_constants, write_config_imports, write_config_struct_declaration, - write_config_struct_impl, write_interact_struct_declaration, write_snippet_constants, + write_chain_sim_test_to_file, write_config_constants, write_config_imports, + write_config_struct_declaration, write_config_struct_impl, + write_interact_struct_declaration, write_interactor_test_to_file, write_snippet_constants, write_snippet_imports, write_snippet_main_function, write_snippet_state_impl, write_state_struct_declaration, }, @@ -29,6 +31,13 @@ impl MetaConfig { write_snippets_to_file(&mut file, &self.original_contract_abi, crate_name); let mut config_file = create_config_and_get_file(&self.snippets_dir); write_config_to_file(&mut config_file); + let (mut interactor_test_file, mut chain_sim_test_file) = + create_test_folder_and_get_files(&self.snippets_dir); + write_tests_to_files( + &mut interactor_test_file, + &mut chain_sim_test_file, + crate_name, + ); } } @@ -69,3 +78,12 @@ fn write_config_to_file(file: &mut File) { write_config_struct_declaration(file); write_config_struct_impl(file); } + +fn write_tests_to_files( + interactor_test_file: &mut File, + chain_sim_test_file: &mut File, + crate_name: &str, +) { + write_interactor_test_to_file(interactor_test_file, crate_name); + write_chain_sim_test_to_file(chain_sim_test_file, crate_name); +} diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index 17469c9c1a..a089b7c2fc 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -225,3 +225,42 @@ pub(crate) fn write_config_struct_impl(file: &mut File) { ) .unwrap(); } + +pub(crate) fn write_chain_sim_test_to_file(file: &mut File, crate_name: &str) { + writeln!( + file, + r#"use multiversx_sc_snippets::imports::*; +use rust_interact::ContractInteract; + +// Simple deploy test that runs using the chain simulator configuration. +// In order for this test to work, make sure that the `config.toml` file contains the chain simulator config (or choose it manually) +// The chain simulator should already be installed and running before attempting to run this test. +// The chain-simulator-tests feature should be present in Cargo.toml. +// Can be run with `cargo test --features chain-simulator-tests`. +#[tokio::test] +#[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] +async fn deploy_test_{crate_name}_cs() {{ + let mut interactor = ContractInteract::new().await; + + interactor.deploy().await; +}}"# + ).unwrap() +} + +pub(crate) fn write_interactor_test_to_file(file: &mut File, crate_name: &str) { + writeln!( + file, + r#"use multiversx_sc_snippets::imports::*; +use rust_interact::ContractInteract; + +// Simple deploy test that runs on the real blockchain configuration. +// In order for this test to work, make sure that the `config.toml` file contains the real blockchain config (or choose it manually) +// Can be run with `cargo test`. +#[tokio::test] +async fn deploy_test_{crate_name}() {{ + let mut interactor = ContractInteract::new().await; + + interactor.deploy().await; +}}"# + ).unwrap() +} From cda404f61e92e846978d33b6405e9967792996fd Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 14 Nov 2024 14:13:21 +0100 Subject: [PATCH 09/12] clippy --- .../src/contract/generate_snippets/snippet_crate_gen.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs index 74ed9d4115..8b9e840363 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs @@ -162,9 +162,9 @@ pub(crate) fn create_test_folder_and_get_files(snippets_folder_path: &str) -> (F let interactor_cs_file_path = format!("{folder_path}/{INTERACTOR_CS_TEST_FILE_NAME}"); let interactor_file = - File::create(&interactor_file_path).expect("Failed to create interact_tests.rs file"); + File::create(interactor_file_path).expect("Failed to create interact_tests.rs file"); let interactor_cs_file = - File::create(&interactor_cs_file_path).expect("Failed to create interact_cs_tests.rs file"); + File::create(interactor_cs_file_path).expect("Failed to create interact_cs_tests.rs file"); (interactor_file, interactor_cs_file) } From e65ce6a9244028c32d58b6e3e204f7ec12a571cf Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 14 Nov 2024 14:25:05 +0100 Subject: [PATCH 10/12] ignore real blockchain test, run on demand --- .../src/contract/generate_snippets/snippet_template_gen.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index a089b7c2fc..c2dc62a88b 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -257,6 +257,7 @@ use rust_interact::ContractInteract; // In order for this test to work, make sure that the `config.toml` file contains the real blockchain config (or choose it manually) // Can be run with `cargo test`. #[tokio::test] +#[ignore = "run on demand, relies on real blockchain state"] async fn deploy_test_{crate_name}() {{ let mut interactor = ContractInteract::new().await; From dee1053a5607b0a4978eeb7d47773241e75e16de Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 14 Nov 2024 16:38:30 +0100 Subject: [PATCH 11/12] feature for blockchain interactor tests --- .../src/contract/generate_snippets/snippet_crate_gen.rs | 1 + .../src/contract/generate_snippets/snippet_template_gen.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs index 8b9e840363..71dc38d3f4 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs @@ -100,6 +100,7 @@ toml = "0.8.6" [features] chain-simulator-tests = [] +blockchain-tests = [] "# ) .unwrap(); diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index c2dc62a88b..0a8d528e6b 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -257,7 +257,7 @@ use rust_interact::ContractInteract; // In order for this test to work, make sure that the `config.toml` file contains the real blockchain config (or choose it manually) // Can be run with `cargo test`. #[tokio::test] -#[ignore = "run on demand, relies on real blockchain state"] +#[cfg_attr(not(feature = "blockchain-tests"), ignore)] async fn deploy_test_{crate_name}() {{ let mut interactor = ContractInteract::new().await; From 4cbc0b703e4febf3e5e2bbbb40cb56e8116a1f0e Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Fri, 15 Nov 2024 09:37:57 +0100 Subject: [PATCH 12/12] comments --- .../src/contract/generate_snippets/snippet_template_gen.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index 0a8d528e6b..d3bd3e91c4 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -236,7 +236,7 @@ use rust_interact::ContractInteract; // In order for this test to work, make sure that the `config.toml` file contains the chain simulator config (or choose it manually) // The chain simulator should already be installed and running before attempting to run this test. // The chain-simulator-tests feature should be present in Cargo.toml. -// Can be run with `cargo test --features chain-simulator-tests`. +// Can be run with `sc-meta test -c`. #[tokio::test] #[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] async fn deploy_test_{crate_name}_cs() {{ @@ -255,7 +255,7 @@ use rust_interact::ContractInteract; // Simple deploy test that runs on the real blockchain configuration. // In order for this test to work, make sure that the `config.toml` file contains the real blockchain config (or choose it manually) -// Can be run with `cargo test`. +// Can be run with `sc-meta test`. #[tokio::test] #[cfg_attr(not(feature = "blockchain-tests"), ignore)] async fn deploy_test_{crate_name}() {{