From 58c545ae384246639c3380e5d520c3097cfd2973 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Sun, 24 Mar 2024 17:44:41 +0100 Subject: [PATCH 1/6] test: pop new contract add test when fails --- src/commands/new/contract.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/commands/new/contract.rs b/src/commands/new/contract.rs index 4f08d5f4..fc68189a 100644 --- a/src/commands/new/contract.rs +++ b/src/commands/new/contract.rs @@ -61,9 +61,10 @@ impl NewContractCommand { mod tests { use super::*; use std::fs; + use anyhow::{Result, Error}; #[test] - fn test_new_contract_command_execute() -> anyhow::Result<()> { + fn test_new_contract_command_execute_success() -> Result<(), Error> { let command = NewContractCommand { name: "test_contract".to_string(), path: Some(PathBuf::new()) }; let result = command.execute(); @@ -75,4 +76,15 @@ mod tests { } Ok(()) } + + #[test] + fn test_new_contract_command_execute_fails_path_no_exist() -> Result<(), Error> { + let mut path = PathBuf::new(); + path.push("test_contract"); + let command = + NewContractCommand { name: "test_contract".to_string(), path: Some(path) }; + let result_error = command.execute(); + assert!(result_error.is_err()); + Ok(()) + } } From 056012f856f47fc526ab376009a26c9c6afe8d65 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Sun, 24 Mar 2024 18:31:32 +0100 Subject: [PATCH 2/6] test: add test for build contract --- src/engines/contract_engine.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index 0f20bbe2..d6f53ba2 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -203,6 +203,23 @@ mod tests { Ok(()) } + #[test] + fn test_contract_build() -> Result<(), Error> { + let temp_contract_dir = setup_test_environment()?; + + let build = build_smart_contract(&Some(temp_contract_dir.path().join("test_contract"))); + assert!(build.is_ok(), "Result should be Ok"); + + // Verify that the folder target has been created + assert!(temp_contract_dir.path().join("test_contract/target").exists()); + // Verify that all the artifacts has been generated + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.contract").exists()); + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.wasm").exists()); + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.json").exists()); + + Ok(()) + } + #[test] fn test_contract_test() -> Result<(), Error> { let temp_contract_dir = setup_test_environment()?; From bd1f74f1b7553d7886dc2db26d6e2391fdfee61a Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Sun, 24 Mar 2024 19:03:50 +0100 Subject: [PATCH 3/6] test: integration tests for build contract --- Cargo.lock | 100 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++ tests/build_contract.rs | 74 +++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 tests/build_contract.rs diff --git a/Cargo.lock b/Cargo.lock index d5bc54d8..512a3f78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,6 +418,21 @@ dependencies = [ "nom", ] +[[package]] +name = "assert_cmd" +version = "2.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed72493ac66d5804837f480ab3766c72bdfab91a65e565fc54fa9e42db0073a8" +dependencies = [ + "anstyle", + "bstr", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + [[package]] name = "ast_node" version = "0.9.5" @@ -1057,6 +1072,17 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "bstr" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706" +dependencies = [ + "memchr", + "regex-automata 0.4.6", + "serde", +] + [[package]] name = "bumpalo" version = "3.15.4" @@ -2190,6 +2216,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.8.1" @@ -2264,6 +2296,12 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "docify" version = "0.2.7" @@ -2673,6 +2711,15 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + [[package]] name = "fnv" version = "1.0.7" @@ -4649,6 +4696,12 @@ dependencies = [ "nom", ] +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -5365,6 +5418,7 @@ dependencies = [ "ansi_term", "anyhow", "askama", + "assert_cmd", "clap", "cliclack", "console", @@ -5376,6 +5430,7 @@ dependencies = [ "indexmap 2.2.5", "ink_env", "log", + "predicates", "reqwest", "serde", "serde_json", @@ -5414,6 +5469,36 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "predicates" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b87bfd4605926cdfefc1c3b5f8fe560e3feca9d5552cf68c466d3d8236c7e8" +dependencies = [ + "anstyle", + "difflib", + "float-cmp", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" + +[[package]] +name = "predicates-tree" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "prettier-please" version = "0.2.0" @@ -8499,6 +8584,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "termtree" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" + [[package]] name = "text_lines" version = "0.6.0" @@ -9359,6 +9450,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "waker-fn" version = "1.1.1" diff --git a/Cargo.toml b/Cargo.toml index 241cdb5c..211269c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,10 @@ tracing-subscriber = { version = "0.3", optional = true } zombienet-sdk = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", optional = true } zombienet-support = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", optional = true } +[dev-dependencies] +assert_cmd = "2.0.14" +predicates = "3.1.0" + [features] default = ["contract", "parachain"] contract = [ diff --git a/tests/build_contract.rs b/tests/build_contract.rs new file mode 100644 index 00000000..1b0c9ae6 --- /dev/null +++ b/tests/build_contract.rs @@ -0,0 +1,74 @@ +use assert_cmd::Command; +use predicates::prelude::*; +use anyhow::{Result, Error}; + +fn setup_test_environment() -> Result { + let temp_contract_dir = tempfile::tempdir().unwrap(); + // pop new contract test_contract + Command::cargo_bin("pop") + .unwrap() + .current_dir(&temp_contract_dir) + .args(&["new", "contract", "test_contract"]) + .assert() + .success(); + + Ok(temp_contract_dir) +} + +#[test] +fn test_contract_build() -> Result<(), Error> { + let temp_contract_dir = setup_test_environment()?; + + // pop new contract test_contract + Command::cargo_bin("pop") + .unwrap() + .current_dir(&temp_contract_dir.path().join("test_contract")) + .args(&["build", "contract"]) + .assert() + .success(); + + // Verify that the folder target has been created + assert!(temp_contract_dir.path().join("test_contract/target").exists()); + // Verify that all the artifacts has been generated + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.contract").exists()); + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.wasm").exists()); + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.json").exists()); + + Ok(()) +} + +#[test] +fn test_contract_build_specify_path() -> Result<(), Error> { + let temp_contract_dir = setup_test_environment()?; + + // pop new contract test_contract + Command::cargo_bin("pop") + .unwrap() + .current_dir(&temp_contract_dir.path()) + .args(&["build", "contract", "--path", "./test_contract"]) + .assert() + .success(); + + // Verify that the folder target has been created + assert!(temp_contract_dir.path().join("test_contract/target").exists()); + // Verify that all the artifacts has been generated + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.contract").exists()); + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.wasm").exists()); + assert!(temp_contract_dir.path().join("test_contract/target/ink/test_contract.json").exists()); + + Ok(()) +} + +#[test] +fn test_contract_build_fails_if_no_contract_exists() -> Result<(), Error> { + + // pop new contract test_contract + Command::cargo_bin("pop") + .unwrap() + .args(&["build", "contract",]) + .assert() + .failure() + .stderr(predicate::str::contains("Error: No 'ink' dependency found")); + + Ok(()) +} From cb517527f8b0cd77a1d77f21ab8d5d6f77ccb8d4 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Sun, 24 Mar 2024 19:05:58 +0100 Subject: [PATCH 4/6] docs: fix comments --- tests/build_contract.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/build_contract.rs b/tests/build_contract.rs index 1b0c9ae6..8e34dcca 100644 --- a/tests/build_contract.rs +++ b/tests/build_contract.rs @@ -19,7 +19,7 @@ fn setup_test_environment() -> Result { fn test_contract_build() -> Result<(), Error> { let temp_contract_dir = setup_test_environment()?; - // pop new contract test_contract + // pop build contract Command::cargo_bin("pop") .unwrap() .current_dir(&temp_contract_dir.path().join("test_contract")) @@ -41,7 +41,7 @@ fn test_contract_build() -> Result<(), Error> { fn test_contract_build_specify_path() -> Result<(), Error> { let temp_contract_dir = setup_test_environment()?; - // pop new contract test_contract + // pop build contract --path ./test_contract Command::cargo_bin("pop") .unwrap() .current_dir(&temp_contract_dir.path()) @@ -62,7 +62,7 @@ fn test_contract_build_specify_path() -> Result<(), Error> { #[test] fn test_contract_build_fails_if_no_contract_exists() -> Result<(), Error> { - // pop new contract test_contract + // pop build contract Command::cargo_bin("pop") .unwrap() .args(&["build", "contract",]) From 4957def21b6c151d410697046c0b85477a05f4d2 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Mon, 25 Mar 2024 09:55:22 +0100 Subject: [PATCH 5/6] chore: pr feedback --- src/commands/new/contract.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/new/contract.rs b/src/commands/new/contract.rs index fc68189a..d81778c3 100644 --- a/src/commands/new/contract.rs +++ b/src/commands/new/contract.rs @@ -64,7 +64,7 @@ mod tests { use anyhow::{Result, Error}; #[test] - fn test_new_contract_command_execute_success() -> Result<(), Error> { + fn test_new_contract_command_execute_success() -> Result<()> { let command = NewContractCommand { name: "test_contract".to_string(), path: Some(PathBuf::new()) }; let result = command.execute(); From c285be208e65ee7eb9c72ba801872966707c1965 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Mon, 25 Mar 2024 10:03:22 +0100 Subject: [PATCH 6/6] refactor: use tempdir for new contract tests --- src/commands/new/contract.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/commands/new/contract.rs b/src/commands/new/contract.rs index d81778c3..1039817a 100644 --- a/src/commands/new/contract.rs +++ b/src/commands/new/contract.rs @@ -60,29 +60,24 @@ impl NewContractCommand { #[cfg(test)] mod tests { use super::*; - use std::fs; - use anyhow::{Result, Error}; + use anyhow::Result; #[test] fn test_new_contract_command_execute_success() -> Result<()> { + let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir"); let command = - NewContractCommand { name: "test_contract".to_string(), path: Some(PathBuf::new()) }; + NewContractCommand { name: "test_contract".to_string(), path: Some(PathBuf::from(temp_contract_dir.path())) }; let result = command.execute(); assert!(result.is_ok()); - // Clean up - if let Err(err) = fs::remove_dir_all("test_contract") { - eprintln!("Failed to delete directory: {}", err); - } Ok(()) } #[test] - fn test_new_contract_command_execute_fails_path_no_exist() -> Result<(), Error> { - let mut path = PathBuf::new(); - path.push("test_contract"); + fn test_new_contract_command_execute_fails_path_no_exist() -> Result<()> { + let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir"); let command = - NewContractCommand { name: "test_contract".to_string(), path: Some(path) }; + NewContractCommand { name: "test_contract".to_string(), path: Some(temp_contract_dir.path().join("new_contract")) }; let result_error = command.execute(); assert!(result_error.is_err()); Ok(())