From 05caff88d2fbc7637af7a094899fb0aa810ca124 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Tue, 19 Mar 2024 16:42:30 +0900 Subject: [PATCH 01/11] add unit test for pop test contract --- src/engines/contract_engine.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index b4cf3d5a..f26a93e9 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -171,21 +171,37 @@ pub async fn dry_run_call( mod tests { use super::*; use std::fs; - use tempdir; + use std::path::PathBuf; + use tempdir::TempDir; + + fn setup_test_environment() -> Result> { + let temp_contract_dir = TempDir::new("test_folder")?; + let result: anyhow::Result<()> = create_smart_contract("test".to_string(), &Some(PathBuf::from(temp_contract_dir.path()))); + assert!(result.is_ok(), "Failed to create smart contract"); + + Ok(temp_contract_dir) + } #[test] fn test_create_smart_contract() -> Result<(), Box> { - let temp_dir = tempdir::TempDir::new("test_folder")?; - let result: anyhow::Result<()> = - create_smart_contract("test".to_string(), &Some(PathBuf::from(temp_dir.path()))); - assert!(result.is_ok()); + let temp_contract_dir = setup_test_environment()?; // Verify that the generated smart contract contains the expected content - let generated_file_content = fs::read_to_string(temp_dir.path().join("test/lib.rs"))?; + let generated_file_content = fs::read_to_string(temp_contract_dir.path().join("test/lib.rs"))?; assert!(generated_file_content.contains("#[ink::contract]")); assert!(generated_file_content.contains("mod test {")); Ok(()) } -} + + #[test] + fn test_test_smart_contract() -> Result<(), Box> { + let temp_contract_dir = setup_test_environment()?; + + let result = test_smart_contract(&Some(temp_contract_dir.path().join("test"))); + + assert!(result.is_ok(), "Result should be Ok"); + Ok(()) + } +} \ No newline at end of file From 6fb3a66d26003f1420f93796d5a29c76a6749d10 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 19:33:35 +0900 Subject: [PATCH 02/11] add anyhow:error --- src/engines/contract_engine.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index f26a93e9..6aeaa549 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -56,7 +56,7 @@ pub fn build_smart_contract(path: &Option) -> anyhow::Result<()> { Ok(()) } -pub fn test_smart_contract(path: &Option) -> anyhow::Result<()> { +pub fn (path: &Option) -> anyhow::Result<()> { cmd("cargo", vec!["test"]).dir(path.clone().unwrap_or("./".into())).run()?; Ok(()) @@ -174,7 +174,7 @@ mod tests { use std::path::PathBuf; use tempdir::TempDir; - fn setup_test_environment() -> Result> { + fn setup_test_environment() -> Result, anyhow::Error> { let temp_contract_dir = TempDir::new("test_folder")?; let result: anyhow::Result<()> = create_smart_contract("test".to_string(), &Some(PathBuf::from(temp_contract_dir.path()))); assert!(result.is_ok(), "Failed to create smart contract"); @@ -183,7 +183,7 @@ mod tests { } #[test] - fn test_create_smart_contract() -> Result<(), Box> { + fn test_create_smart_contract() -> Result, anyhow::Error> { let temp_contract_dir = setup_test_environment()?; // Verify that the generated smart contract contains the expected content @@ -196,7 +196,7 @@ mod tests { } #[test] - fn test_test_smart_contract() -> Result<(), Box> { + fn test_test_smart_contract() -> Result, anyhow::Error> { let temp_contract_dir = setup_test_environment()?; let result = test_smart_contract(&Some(temp_contract_dir.path().join("test"))); From 96b835c40ef3ca33d3eb183f9eae84e29eb0fbdf Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 19:36:10 +0900 Subject: [PATCH 03/11] rename tests --- src/engines/contract_engine.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 6aeaa549..9ed7c68a 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -183,7 +183,7 @@ mod tests { } #[test] - fn test_create_smart_contract() -> Result, anyhow::Error> { + fn test_contract_create() -> Result, anyhow::Error> { let temp_contract_dir = setup_test_environment()?; // Verify that the generated smart contract contains the expected content @@ -196,7 +196,7 @@ mod tests { } #[test] - fn test_test_smart_contract() -> Result, anyhow::Error> { + fn test_contract_test() -> Result, anyhow::Error> { let temp_contract_dir = setup_test_environment()?; let result = test_smart_contract(&Some(temp_contract_dir.path().join("test"))); From 28b032c4d13fc967bded81445d17193063bf7507 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 20:06:31 +0900 Subject: [PATCH 04/11] use anyhow bail --- src/engines/contract_engine.rs | 42 ++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 9ed7c68a..1db45b47 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -56,7 +56,7 @@ pub fn build_smart_contract(path: &Option) -> anyhow::Result<()> { Ok(()) } -pub fn (path: &Option) -> anyhow::Result<()> { +pub fn test_smart_contract(path: &Option) -> anyhow::Result<()> { cmd("cargo", vec!["test"]).dir(path.clone().unwrap_or("./".into())).run()?; Ok(()) @@ -172,36 +172,48 @@ mod tests { use super::*; use std::fs; use std::path::PathBuf; - use tempdir::TempDir; + use tempdir::TempDir; - fn setup_test_environment() -> Result, anyhow::Error> { - let temp_contract_dir = TempDir::new("test_folder")?; - let result: anyhow::Result<()> = create_smart_contract("test".to_string(), &Some(PathBuf::from(temp_contract_dir.path()))); - assert!(result.is_ok(), "Failed to create smart contract"); + fn setup_test_environment() -> anyhow::Result { + let temp_contract_dir = TempDir::new("test_folder")?; + let result: anyhow::Result<()> = create_smart_contract( + "test".to_string(), + &Some(PathBuf::from(temp_contract_dir.path())), + ); - Ok(temp_contract_dir) - } + if !result.is_ok() { + anyhow::bail!("Failed to create smart contract"); + } + + Ok(temp_contract_dir) + } #[test] - fn test_contract_create() -> Result, anyhow::Error> { + fn test_contract_create() -> anyhow::Result<(), anyhow::Error> { let temp_contract_dir = setup_test_environment()?; // Verify that the generated smart contract contains the expected content - let generated_file_content = fs::read_to_string(temp_contract_dir.path().join("test/lib.rs"))?; + let generated_file_content = + fs::read_to_string(temp_contract_dir.path().join("test/lib.rs"))?; assert!(generated_file_content.contains("#[ink::contract]")); assert!(generated_file_content.contains("mod test {")); + // Verify that the generated Cargo.toml file contains the expected content + let generated_file_content = + fs::read_to_string(temp_contract_dir.path().join("test/Cargo.toml"))?; + assert!(generated_file_content.contains("[package]")); + Ok(()) } #[test] - fn test_contract_test() -> Result, anyhow::Error> { - let temp_contract_dir = setup_test_environment()?; + fn test_contract_test() -> anyhow::Result<(), anyhow::Error> { + let temp_contract_dir = setup_test_environment()?; let result = test_smart_contract(&Some(temp_contract_dir.path().join("test"))); - assert!(result.is_ok(), "Result should be Ok"); + assert!(result.is_ok(), "Result should be Ok"); Ok(()) - } -} \ No newline at end of file + } +} From 2dc671144f1fcd1566da781e29a3a90b1a824456 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 20:36:33 +0900 Subject: [PATCH 05/11] use assert in tests --- src/engines/contract_engine.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 1db45b47..1eb9c1c6 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -181,9 +181,7 @@ mod tests { &Some(PathBuf::from(temp_contract_dir.path())), ); - if !result.is_ok() { - anyhow::bail!("Failed to create smart contract"); - } + assert!(result.is_ok(), "Result should be Ok"); Ok(temp_contract_dir) } From 040be08acb47b082a8f69fe66a6bb3bd783ddc5e Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 20:52:27 +0900 Subject: [PATCH 06/11] rename test to test_contract --- src/engines/contract_engine.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 1eb9c1c6..5d284100 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -172,12 +172,11 @@ mod tests { use super::*; use std::fs; use std::path::PathBuf; - use tempdir::TempDir; - fn setup_test_environment() -> anyhow::Result { - let temp_contract_dir = TempDir::new("test_folder")?; + fn setup_test_environment() -> anyhow::Result { + let temp_contract_dir = tempfile::tempdir()?; let result: anyhow::Result<()> = create_smart_contract( - "test".to_string(), + "test_contract".to_string(), &Some(PathBuf::from(temp_contract_dir.path())), ); @@ -192,14 +191,14 @@ mod tests { // Verify that the generated smart contract contains the expected content let generated_file_content = - fs::read_to_string(temp_contract_dir.path().join("test/lib.rs"))?; + fs::read_to_string(temp_contract_dir.path().join("test_contract/lib.rs"))?; assert!(generated_file_content.contains("#[ink::contract]")); - assert!(generated_file_content.contains("mod test {")); + assert!(generated_file_content.contains("mod test_contract {")); // Verify that the generated Cargo.toml file contains the expected content let generated_file_content = - fs::read_to_string(temp_contract_dir.path().join("test/Cargo.toml"))?; + fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml"))?; assert!(generated_file_content.contains("[package]")); Ok(()) @@ -209,7 +208,7 @@ mod tests { fn test_contract_test() -> anyhow::Result<(), anyhow::Error> { let temp_contract_dir = setup_test_environment()?; - let result = test_smart_contract(&Some(temp_contract_dir.path().join("test"))); + let result = test_smart_contract(&Some(temp_contract_dir.path().join("test_contract"))); assert!(result.is_ok(), "Result should be Ok"); Ok(()) From 7b24919b5460fb79453b16d3926dcada1ace6348 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 21:01:57 +0900 Subject: [PATCH 07/11] use expects --- src/engines/contract_engine.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 5d284100..11126148 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -174,7 +174,7 @@ mod tests { use std::path::PathBuf; fn setup_test_environment() -> anyhow::Result { - let temp_contract_dir = tempfile::tempdir()?; + let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir"); let result: anyhow::Result<()> = create_smart_contract( "test_contract".to_string(), &Some(PathBuf::from(temp_contract_dir.path())), @@ -191,15 +191,17 @@ mod tests { // Verify that the generated smart contract contains the expected content let generated_file_content = - fs::read_to_string(temp_contract_dir.path().join("test_contract/lib.rs"))?; + fs::read_to_string(temp_contract_dir.path().join("test_contract/lib.rs")) + .expect("Could not read file"); assert!(generated_file_content.contains("#[ink::contract]")); assert!(generated_file_content.contains("mod test_contract {")); // Verify that the generated Cargo.toml file contains the expected content let generated_file_content = - fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml"))?; - assert!(generated_file_content.contains("[package]")); + fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml")) + .expect("Could not read file"); + assert!(generated_file_content.contains("[package]")); Ok(()) } From ee13672cef2a4a4baf1a139de55b42bdf33a5632 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 21:54:45 +0900 Subject: [PATCH 08/11] make it one line --- src/engines/contract_engine.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 11126148..20230da2 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -170,8 +170,7 @@ pub async fn dry_run_call( #[cfg(test)] mod tests { use super::*; - use std::fs; - use std::path::PathBuf; + use std::{fs, path::PathBuf}; fn setup_test_environment() -> anyhow::Result { let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir"); From 2cad0cce2a2b2dfe2d73adeaa24014daaf9680cd Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 21:57:52 +0900 Subject: [PATCH 09/11] use anyhow --- src/engines/contract_engine.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 20230da2..7797705c 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -171,8 +171,9 @@ pub async fn dry_run_call( mod tests { use super::*; use std::{fs, path::PathBuf}; + use anyhow::{Result, Error}; - fn setup_test_environment() -> anyhow::Result { + fn setup_test_environment() -> Result { let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir"); let result: anyhow::Result<()> = create_smart_contract( "test_contract".to_string(), @@ -185,7 +186,7 @@ mod tests { } #[test] - fn test_contract_create() -> anyhow::Result<(), anyhow::Error> { + fn test_contract_create() -> Result<(), Error> { let temp_contract_dir = setup_test_environment()?; // Verify that the generated smart contract contains the expected content @@ -206,7 +207,7 @@ mod tests { } #[test] - fn test_contract_test() -> anyhow::Result<(), anyhow::Error> { + fn test_contract_test() -> Result<(), Error> { let temp_contract_dir = setup_test_environment()?; let result = test_smart_contract(&Some(temp_contract_dir.path().join("test_contract"))); From 84220d160868b7bc199b3ee8edb803b10779e424 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 22:01:04 +0900 Subject: [PATCH 10/11] remove test --- src/engines/contract_engine.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 7797705c..dfc6f548 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -201,8 +201,6 @@ mod tests { let generated_file_content = fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml")) .expect("Could not read file"); - assert!(generated_file_content.contains("[package]")); - Ok(()) } From b693b8e221a5d2876e2c1ab5861f42b936c1e172 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 21 Mar 2024 22:02:22 +0900 Subject: [PATCH 11/11] remove variable --- src/engines/contract_engine.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index dfc6f548..0f20bbe2 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -198,8 +198,7 @@ mod tests { assert!(generated_file_content.contains("mod test_contract {")); // Verify that the generated Cargo.toml file contains the expected content - let generated_file_content = - fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml")) + fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml")) .expect("Could not read file"); Ok(()) }