From 074670e6dfa70ff0b11c38ddda986dbd34b680d6 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Tue, 26 Mar 2024 20:36:26 +0100 Subject: [PATCH] chore: cargo fmt --- src/commands/new/contract.rs | 12 +- src/commands/up/parachain.rs | 5 +- src/engines/contract_engine.rs | 19 +++- src/engines/parachain_engine.rs | 9 +- src/git.rs | 2 +- src/parachains/mod.rs | 2 +- src/parachains/zombienet.rs | 187 ++++++++++++++++---------------- tests/build_contract.rs | 95 +++++++++------- tests/up_parachain.rs | 104 +++++++++--------- 9 files changed, 237 insertions(+), 198 deletions(-) diff --git a/src/commands/new/contract.rs b/src/commands/new/contract.rs index 1039817a..909c1ea8 100644 --- a/src/commands/new/contract.rs +++ b/src/commands/new/contract.rs @@ -65,8 +65,10 @@ mod tests { #[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::from(temp_contract_dir.path())) }; + let command = NewContractCommand { + name: "test_contract".to_string(), + path: Some(PathBuf::from(temp_contract_dir.path())), + }; let result = command.execute(); assert!(result.is_ok()); @@ -76,8 +78,10 @@ mod tests { #[test] 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(temp_contract_dir.path().join("new_contract")) }; + let command = 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(()) diff --git a/src/commands/up/parachain.rs b/src/commands/up/parachain.rs index b45a586c..aa1c124d 100644 --- a/src/commands/up/parachain.rs +++ b/src/commands/up/parachain.rs @@ -1,4 +1,7 @@ -use crate::{style::{style, Theme}, parachains::zombienet::Zombienet}; +use crate::{ + parachains::zombienet::Zombienet, + style::{style, Theme}, +}; use clap::Args; use cliclack::{clear_screen, confirm, intro, log, outro, outro_cancel, set_theme}; use console::{Emoji, Style}; diff --git a/src/engines/contract_engine.rs b/src/engines/contract_engine.rs index d6f53ba2..09721ec6 100644 --- a/src/engines/contract_engine.rs +++ b/src/engines/contract_engine.rs @@ -170,8 +170,8 @@ pub async fn dry_run_call( #[cfg(test)] mod tests { use super::*; + use anyhow::{Error, Result}; use std::{fs, path::PathBuf}; - use anyhow::{Result, Error}; fn setup_test_environment() -> Result { let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir"); @@ -199,7 +199,7 @@ mod tests { // Verify that the generated Cargo.toml file contains the expected content fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml")) - .expect("Could not read file"); + .expect("Could not read file"); Ok(()) } @@ -213,9 +213,18 @@ mod tests { // 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()); + 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(()) } diff --git a/src/engines/parachain_engine.rs b/src/engines/parachain_engine.rs index 5fcbc2ab..cf55776d 100644 --- a/src/engines/parachain_engine.rs +++ b/src/engines/parachain_engine.rs @@ -104,8 +104,10 @@ mod tests { let generated_file_content = fs::read_to_string(temp_dir.path().join("node/src/chain_spec.rs")) .expect("Failed to read file"); - assert!(generated_file_content.contains("properties.insert(\"tokenSymbol\".into(), \"DOT\".into());")); - assert!(generated_file_content.contains("properties.insert(\"tokenDecimals\".into(), 18.into());")); + assert!(generated_file_content + .contains("properties.insert(\"tokenSymbol\".into(), \"DOT\".into());")); + assert!(generated_file_content + .contains("properties.insert(\"tokenDecimals\".into(), 18.into());")); assert!(generated_file_content.contains("1000000")); // Verify network.toml contains expected content @@ -124,7 +126,8 @@ mod tests { #[test] fn test_parachain_build_after_instantiating_template() -> Result<()> { - let temp_dir = setup_template_and_instantiate().expect("Failed to setup template and instantiate"); + let temp_dir = + setup_template_and_instantiate().expect("Failed to setup template and instantiate"); let build = build_parachain(&Some(temp_dir.path().to_path_buf())); assert!(build.is_ok(), "Result should be Ok"); Ok(()) diff --git a/src/git.rs b/src/git.rs index 935039af..0156bccd 100644 --- a/src/git.rs +++ b/src/git.rs @@ -70,4 +70,4 @@ impl GitHub { pub(crate) fn release(repo: &Url, tag: &str, artifact: &str) -> String { format!("{}/releases/download/{tag}/{artifact}", repo.as_str()) } -} \ No newline at end of file +} diff --git a/src/parachains/mod.rs b/src/parachains/mod.rs index c77a88b7..5babc92a 100644 --- a/src/parachains/mod.rs +++ b/src/parachains/mod.rs @@ -1 +1 @@ -pub(crate) mod zombienet; \ No newline at end of file +pub(crate) mod zombienet; diff --git a/src/parachains/zombienet.rs b/src/parachains/zombienet.rs index 0a9c47f9..8b69344f 100644 --- a/src/parachains/zombienet.rs +++ b/src/parachains/zombienet.rs @@ -366,7 +366,6 @@ impl Zombienet { .strip_prefix("polkadot-") .map_or_else(|| release_tag.clone(), |v| v.to_string())) } - } pub struct Binary { @@ -527,16 +526,16 @@ mod tests { async fn test_new_success() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); - let zombienet = Zombienet::new( - cache.clone(), - "./tests/zombienet.toml", - Some(&"v1.7.0".to_string()), - Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) - ) - .await?; + let zombienet = Zombienet::new( + cache.clone(), + "./tests/zombienet.toml", + Some(&"v1.7.0".to_string()), + Some(&"v1.7.0".to_string()), + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), + ) + .await?; // Check has the binary for Polkadot assert_eq!(zombienet.relay_chain.name, "polkadot-v1.7.0"); @@ -547,14 +546,14 @@ mod tests { // Check has the binary for the System Chain assert_eq!(zombienet.parachains.len(), 2); - let system_chain = &zombienet.parachains[0]; + let system_chain = &zombienet.parachains[0]; assert_eq!(system_chain.name, "polkadot-parachain-v1.7.0"); assert_eq!(system_chain.path, temp_dir.path().join("polkadot-parachain-v1.7.0")); assert_eq!(system_chain.version, "v1.7.0"); assert_eq!(system_chain.sources.len(), 1); // Check has the binary for POP - let parachain = &zombienet.parachains[1]; + let parachain = &zombienet.parachains[1]; assert_eq!(parachain.name, "pop-node"); assert_eq!(parachain.path, temp_dir.path().join("pop-node")); assert_eq!(parachain.version, ""); @@ -567,16 +566,16 @@ mod tests { async fn test_new_fails_wrong_config_no_para_id() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); - let result_error = Zombienet::new( - cache.clone(), - "./tests/wrong_config_no_para_id.toml", - Some(&"v1.7.0".to_string()), - Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) - ) - .await; + let result_error = Zombienet::new( + cache.clone(), + "./tests/wrong_config_no_para_id.toml", + Some(&"v1.7.0".to_string()), + Some(&"v1.7.0".to_string()), + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), + ) + .await; assert!(result_error.is_err()); let error_message = result_error.err().unwrap(); @@ -588,14 +587,14 @@ mod tests { async fn test_relay_chain() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); // Parse network config let network_config_path = PathBuf::from("./tests/zombienet.toml"); let config = std::fs::read_to_string(&network_config_path)?.parse::()?; + let binary_relay_chain = + Zombienet::relay_chain(Some(&"v1.7.0".to_string()), &config, &cache).await?; - let binary_relay_chain = Zombienet::relay_chain(Some(&"v1.7.0".to_string()), &config, &cache ).await?; - assert_eq!(binary_relay_chain.name, "polkadot-v1.7.0"); assert_eq!(binary_relay_chain.path, temp_dir.path().join("polkadot-v1.7.0")); assert_eq!(binary_relay_chain.version, "v1.7.0"); @@ -607,15 +606,14 @@ mod tests { async fn test_relay_chain_no_specifying_version() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); // Parse network config let network_config_path = PathBuf::from("./tests/zombienet.toml"); let config = std::fs::read_to_string(&network_config_path)?.parse::()?; - // Ideally here we will Mock GitHub struct and its get_latest_release function response - let binary_relay_chain = Zombienet::relay_chain(None, &config, &cache ).await?; - + let binary_relay_chain = Zombienet::relay_chain(None, &config, &cache).await?; + assert!(binary_relay_chain.name.starts_with("polkadot-v")); assert!(binary_relay_chain.version.starts_with("v")); assert_eq!(binary_relay_chain.sources.len(), 1); @@ -626,12 +624,13 @@ mod tests { async fn test_relay_chain_fails_wrong_config() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); // Parse network config let network_config_path = PathBuf::from("./tests/wrong_config_no_relay.toml"); let config = std::fs::read_to_string(&network_config_path)?.parse::()?; - let result_error = Zombienet::relay_chain(Some(&"v1.7.0".to_string()), &config, &cache ).await; + let result_error = + Zombienet::relay_chain(Some(&"v1.7.0".to_string()), &config, &cache).await; assert!(result_error.is_err()); let error_message = result_error.err().unwrap(); assert_eq!(error_message.root_cause().to_string(), "expected `relaychain`"); @@ -650,10 +649,10 @@ mod tests { async fn test_system_parachain() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); + + let binary_system_chain = Zombienet::system_parachain(&"v1.7.0".to_string(), &cache)?; - let binary_system_chain = Zombienet::system_parachain(&"v1.7.0".to_string(), &cache )?; - assert_eq!(binary_system_chain.name, "polkadot-parachain-v1.7.0"); assert_eq!(binary_system_chain.path, temp_dir.path().join("polkadot-parachain-v1.7.0")); assert_eq!(binary_system_chain.version, "v1.7.0"); @@ -665,12 +664,12 @@ mod tests { async fn test_parachain() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); let url = Url::parse("https://github.com/r0gue-io/pop-node")?; - let binary_system_chain = Zombienet::parachain(url, &cache )?; - + let binary_system_chain = Zombienet::parachain(url, &cache)?; + assert_eq!(binary_system_chain.name, "pop-node"); assert_eq!(binary_system_chain.path, temp_dir.path().join("pop-node")); assert_eq!(binary_system_chain.version, ""); @@ -682,16 +681,16 @@ mod tests { async fn test_missing_binaries() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); - let zombienet = Zombienet::new( + let zombienet = Zombienet::new( cache.clone(), "./tests/zombienet.toml", Some(&"v1.7.0".to_string()), Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), ) - .await?; + .await?; let missing_binaries = zombienet.missing_binaries(); assert_eq!(missing_binaries.len(), 3); @@ -703,7 +702,7 @@ mod tests { async fn test_missing_binaries_no_missing() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); // Create "fake" binary files let relay_chain_file_path = temp_dir.path().join("polkadot-v1.7.0"); @@ -713,14 +712,14 @@ mod tests { let pop_file_path = temp_dir.path().join("pop-node"); File::create(pop_file_path)?; - let zombienet = Zombienet::new( + let zombienet = Zombienet::new( cache.clone(), "./tests/zombienet.toml", Some(&"v1.7.0".to_string()), Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), ) - .await?; + .await?; let missing_binaries = zombienet.missing_binaries(); assert_eq!(missing_binaries.len(), 0); @@ -732,45 +731,46 @@ mod tests { async fn test_configure() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); - - let mut zombienet = Zombienet::new( - cache.clone(), - "./tests/zombienet.toml", - Some(&"v1.7.0".to_string()), - Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) - ) - .await?; - + let cache = PathBuf::from(temp_dir.path()); + + let mut zombienet = Zombienet::new( + cache.clone(), + "./tests/zombienet.toml", + Some(&"v1.7.0".to_string()), + Some(&"v1.7.0".to_string()), + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), + ) + .await?; + let config = zombienet.configure(); assert!(config.is_ok()); Ok(()) } - #[tokio::test] #[ignore] // It takes long time to build + #[tokio::test] + #[ignore] // It takes long time to build async fn test_spawn() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); - - let mut zombienet = Zombienet::new( - cache.clone(), - "./tests/zombienet.toml", - Some(&"v1.7.0".to_string()), - Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) - ) - .await?; + let cache = PathBuf::from(temp_dir.path()); + + let mut zombienet = Zombienet::new( + cache.clone(), + "./tests/zombienet.toml", + Some(&"v1.7.0".to_string()), + Some(&"v1.7.0".to_string()), + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), + ) + .await?; let missing_binaries = zombienet.missing_binaries(); for binary in missing_binaries { binary.source(&cache).await?; } - - let spawn = zombienet.spawn().await; + + let spawn = zombienet.spawn().await; assert!(spawn.is_ok()); - + Ok(()) } @@ -778,31 +778,30 @@ mod tests { async fn test_spawn_error_no_binaries() -> Result<()> { //cache let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); - - let mut zombienet = Zombienet::new( - cache.clone(), - "./tests/zombienet.toml", - Some(&"v1.7.0".to_string()), - Some(&"v1.7.0".to_string()), - Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]) - ) - .await?; - - let spawn = zombienet.spawn().await; + let cache = PathBuf::from(temp_dir.path()); + + let mut zombienet = Zombienet::new( + cache.clone(), + "./tests/zombienet.toml", + Some(&"v1.7.0".to_string()), + Some(&"v1.7.0".to_string()), + Some(&vec!["https://github.com/r0gue-io/pop-node".to_string()]), + ) + .await?; + + let spawn = zombienet.spawn().await; assert!(spawn.is_err()); - + Ok(()) } - // Source tests #[tokio::test] async fn test_process_url() -> Result<()> { let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); - let source = Source::Url { + let source = Source::Url { name: "polkadot".to_string(), version: "v1.7.0".to_string(), url: "https://github.com/paritytech/polkadot-sdk/releases/download/polkadot-v1.7.0/polkadot".to_string() @@ -814,19 +813,23 @@ mod tests { Ok(()) } - #[tokio::test] #[ignore] // It takes long time to build + #[tokio::test] + #[ignore] // It takes long time to build async fn test_process_git() -> Result<()> { let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); - let cache = PathBuf::from(temp_dir.path()); + let cache = PathBuf::from(temp_dir.path()); let version = "v1.7.0".to_string(); let repo = Url::parse(POLKADOT_SDK).expect("repository url valid"); - let source = Source::Git { + let source = Source::Git { url: repo.into(), branch: Some(format!("release-polkadot-{version}")), package: "polkadot".to_string(), - binaries: ["polkadot", "polkadot-execute-worker", "polkadot-prepare-worker"].iter().map(|b| b.to_string()).collect(), - version: Some(version), + binaries: ["polkadot", "polkadot-execute-worker", "polkadot-prepare-worker"] + .iter() + .map(|b| b.to_string()) + .collect(), + version: Some(version), }; let result = source.process(&cache).await; @@ -837,13 +840,11 @@ mod tests { } #[test] fn test_versioned_name() -> Result<()> { - let versioned_name = Source::versioned_name("polkadot", Some(&"v1.7.0".to_string())); + let versioned_name = Source::versioned_name("polkadot", Some(&"v1.7.0".to_string())); assert_eq!(versioned_name, "polkadot-v1.7.0"); let versioned_name_no_version = Source::versioned_name("polkadot", None); assert_eq!(versioned_name_no_version, "polkadot"); Ok(()) } - - } diff --git a/tests/build_contract.rs b/tests/build_contract.rs index 8e34dcca..65a0bfe8 100644 --- a/tests/build_contract.rs +++ b/tests/build_contract.rs @@ -1,9 +1,9 @@ +use anyhow::{Error, Result}; use assert_cmd::Command; use predicates::prelude::*; -use anyhow::{Result, Error}; fn setup_test_environment() -> Result { - let temp_contract_dir = tempfile::tempdir().unwrap(); + let temp_contract_dir = tempfile::tempdir().unwrap(); // pop new contract test_contract Command::cargo_bin("pop") .unwrap() @@ -12,63 +12,80 @@ fn setup_test_environment() -> Result { .assert() .success(); - Ok(temp_contract_dir) + Ok(temp_contract_dir) } #[test] fn test_contract_build() -> Result<(), Error> { - let temp_contract_dir = setup_test_environment()?; + let temp_contract_dir = setup_test_environment()?; - // pop build contract + // pop build contract Command::cargo_bin("pop") - .unwrap() - .current_dir(&temp_contract_dir.path().join("test_contract")) - .args(&["build", "contract"]) - .assert() - .success(); + .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()); + // 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(()) + Ok(()) } #[test] fn test_contract_build_specify_path() -> Result<(), Error> { - let temp_contract_dir = setup_test_environment()?; + let temp_contract_dir = setup_test_environment()?; - // pop build contract --path ./test_contract + // pop build contract --path ./test_contract Command::cargo_bin("pop") - .unwrap() - .current_dir(&temp_contract_dir.path()) - .args(&["build", "contract", "--path", "./test_contract"]) - .assert() - .success(); + .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()); + // 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(()) + Ok(()) } #[test] fn test_contract_build_fails_if_no_contract_exists() -> Result<(), Error> { - - // pop build contract + // pop build contract Command::cargo_bin("pop") - .unwrap() - .args(&["build", "contract",]) - .assert() - .failure() - .stderr(predicate::str::contains("Error: No 'ink' dependency found")); + .unwrap() + .args(&["build", "contract"]) + .assert() + .failure() + .stderr(predicate::str::contains("Error: No 'ink' dependency found")); - Ok(()) + Ok(()) } diff --git a/tests/up_parachain.rs b/tests/up_parachain.rs index 4a567b84..1d469e19 100644 --- a/tests/up_parachain.rs +++ b/tests/up_parachain.rs @@ -1,76 +1,78 @@ -use std::{fs, path::PathBuf, process::{Command, Stdio}}; -use assert_cmd::{cargo::cargo_bin, Command as AssertCmd}; use anyhow::Result; +use assert_cmd::{cargo::cargo_bin, Command as AssertCmd}; use nix::{ sys::signal::{kill, Signal}, unistd::Pid, }; +use std::{ + fs, + path::PathBuf, + process::{Command, Stdio}, +}; use tokio::time::{sleep, Duration}; fn setup_test_environment() -> Result<()> { - // pop new parachain test_parachain - AssertCmd::cargo_bin("pop") - .unwrap() - .args(&["new", "parachain", "test_parachain"]) - .assert() - .success(); - println!("Parachain created, building it"); - - // pup build parachain test_parachain - AssertCmd::cargo_bin("pop") - .unwrap() - .args(&["build", "parachain", "-p", "./test_parachain"]) - .assert() - .success(); - - println!("Parachain built"); - - Ok(()) + // pop new parachain test_parachain + AssertCmd::cargo_bin("pop") + .unwrap() + .args(&["new", "parachain", "test_parachain"]) + .assert() + .success(); + println!("Parachain created, building it"); + + // pup build parachain test_parachain + AssertCmd::cargo_bin("pop") + .unwrap() + .args(&["build", "parachain", "-p", "./test_parachain"]) + .assert() + .success(); + + println!("Parachain built"); + + Ok(()) } - fn clean_test_environment() -> Result<()> { - if let Err(err) = fs::remove_dir_all("test_parachain") { - eprintln!("Failed to delete directory: {}", err); - } - Ok(()) + if let Err(err) = fs::remove_dir_all("test_parachain") { + eprintln!("Failed to delete directory: {}", err); + } + Ok(()) } - #[tokio::test] async fn test_parachain_up() -> Result<()> { - let _ = setup_test_environment(); + let _ = setup_test_environment(); - println!("pop parachain up -f ./tests/integration_tests.toml"); + println!("pop parachain up -f ./tests/integration_tests.toml"); - // pop up parachain - let mut cmd = Command::new(cargo_bin("pop")) - .stdout(Stdio::piped()) - .args(&["up", "parachain", "-f", "./tests/integration_tests.toml"]) - .spawn() - .unwrap(); + // pop up parachain + let mut cmd = Command::new(cargo_bin("pop")) + .stdout(Stdio::piped()) + .args(&["up", "parachain", "-f", "./tests/integration_tests.toml"]) + .spawn() + .unwrap(); - // Ideally should parse the output of the command + // Ideally should parse the output of the command - //let stdout = cmd.stdout.take().unwrap(); + //let stdout = cmd.stdout.take().unwrap(); - // thread::spawn(move || { - // let reader = BufReader::new(stdout); - // for line in reader.lines() { - // let output = line.unwrap(); - // println!("All the lines: {:?}", output); - // } - // }); + // thread::spawn(move || { + // let reader = BufReader::new(stdout); + // for line in reader.lines() { + // let output = line.unwrap(); + // println!("All the lines: {:?}", output); + // } + // }); - // If after 15 secs is still running probably excution is ok - sleep(Duration::from_secs(15)).await; - assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running"); + // If after 15 secs is still running probably excution is ok + sleep(Duration::from_secs(15)).await; + assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running"); - // Stop the process - kill(Pid::from_raw(cmd.id().try_into().unwrap()), Signal::SIGINT).unwrap(); + // Stop the process + kill(Pid::from_raw(cmd.id().try_into().unwrap()), Signal::SIGINT).unwrap(); - // Clean up - let _ = clean_test_environment(); + // Clean up + let _ = clean_test_environment(); - Ok(()) + Ok(()) }