-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: merge main and fix Testenvironment error
- Loading branch information
Showing
18 changed files
with
292 additions
and
146 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Failed to create new contract project: {0}")] | ||
NewContract(String), | ||
|
||
#[error("IO error: {0}")] | ||
IO(#[from] std::io::Error), | ||
|
||
#[error("Failed to execute test command: {0}")] | ||
TestCommand(String), | ||
|
||
#[error("Failed to parse balance: {0}")] | ||
BalanceParsing(String), | ||
|
||
#[error("Failed to parse account address: {0}")] | ||
AccountAddressParsing(String), | ||
|
||
#[error("Failed to get manifest path: {0}")] | ||
ManifestPath(String), | ||
|
||
#[error("Failed to parse secret URI: {0}")] | ||
ParseSecretURI(String), | ||
|
||
#[error("Failed to create keypair from URI: {0}")] | ||
KeyPairCreation(String), | ||
|
||
#[error("Failed to parse hex encoded bytes: {0}")] | ||
HexParsing(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod build; | ||
mod call; | ||
mod errors; | ||
mod new; | ||
mod test; | ||
mod up; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,62 @@ | ||
use crate::errors::Error; | ||
use duct::cmd; | ||
use std::path::PathBuf; | ||
|
||
pub fn test_smart_contract(path: &Option<PathBuf>) -> anyhow::Result<()> { | ||
cmd("cargo", vec!["test"]).dir(path.clone().unwrap_or("./".into())).run()?; | ||
|
||
pub fn test_smart_contract(path: &Option<PathBuf>) -> Result<(), Error> { | ||
// Execute `cargo test` command in the specified directory. | ||
cmd("cargo", vec!["test"]) | ||
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./"))) | ||
.run() | ||
.map_err(|e| Error::TestCommand(format!("Cargo test command failed: {}", e)))?; | ||
Ok(()) | ||
} | ||
|
||
pub fn test_e2e_smart_contract(path: &Option<PathBuf>) -> anyhow::Result<()> { | ||
pub fn test_e2e_smart_contract(path: &Option<PathBuf>) -> Result<(), Error> { | ||
// Execute `cargo test --features=e2e-tests` command in the specified directory. | ||
cmd("cargo", vec!["test", "--features=e2e-tests"]) | ||
.dir(path.clone().unwrap_or("./".into())) | ||
.run()?; | ||
|
||
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./"))) | ||
.run() | ||
.map_err(|e| Error::TestCommand(format!("Cargo test command failed: {}", e)))?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "unit_contract")] | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use anyhow::{Error, Result}; | ||
use std::fs; | ||
use tempfile; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Error in test: {0}")] | ||
TestEnvironmentError(String), | ||
} | ||
|
||
fn setup_test_environment() -> Result<tempfile::TempDir, Error> { | ||
let temp_dir = tempfile::tempdir().expect("Could not create temp dir"); | ||
let temp_dir = tempfile::tempdir().map_err(|e| { | ||
Error::TestEnvironmentError(format!("Failed to create temp dir: {}", e)) | ||
})?; | ||
let temp_contract_dir = temp_dir.path().join("test_contract"); | ||
fs::create_dir(&temp_contract_dir)?; | ||
fs::create_dir(&temp_contract_dir).map_err(|e| { | ||
Error::TestEnvironmentError(format!("Failed to create test contract directory: {}", e)) | ||
})?; | ||
let result = | ||
crate::create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path()); | ||
crate::create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path()) | ||
.map_err(|e| { | ||
Error::TestEnvironmentError(format!("Failed to create smart contract: {}", e)) | ||
}); | ||
assert!(result.is_ok(), "Contract test environment setup failed"); | ||
|
||
Ok(temp_dir) | ||
} | ||
|
||
#[test] | ||
fn test_contract_test() -> Result<(), Error> { | ||
let temp_contract_dir = setup_test_environment()?; | ||
|
||
// Run unit tests for the smart contract in the temporary contract directory. | ||
let result = test_smart_contract(&Some(temp_contract_dir.path().join("test_contract"))); | ||
|
||
assert!(result.is_ok(), "Result should be Ok"); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
use anyhow::Result; | ||
use crate::errors::Error; | ||
use contract_build::util::decode_hex; | ||
use sp_core::Bytes; | ||
use subxt_signer::{sr25519::Keypair, SecretUri}; | ||
|
||
/// Create a Signer from a secret URI. | ||
pub(crate) fn create_signer(suri: &str) -> Result<Keypair> { | ||
let uri = <SecretUri as std::str::FromStr>::from_str(suri)?; | ||
let keypair = Keypair::from_uri(&uri)?; | ||
pub(crate) fn create_signer(suri: &str) -> Result<Keypair, Error> { | ||
let uri = <SecretUri as std::str::FromStr>::from_str(suri) | ||
.map_err(|e| Error::ParseSecretURI(format!("{}", e)))?; | ||
let keypair = Keypair::from_uri(&uri).map_err(|e| Error::KeyPairCreation(format!("{}", e)))?; | ||
Ok(keypair) | ||
} | ||
|
||
/// Parse hex encoded bytes. | ||
pub fn parse_hex_bytes(input: &str) -> Result<Bytes> { | ||
let bytes = decode_hex(input)?; | ||
pub fn parse_hex_bytes(input: &str) -> Result<Bytes, Error> { | ||
let bytes = decode_hex(input).map_err(|e| Error::HexParsing(format!("{}", e)))?; | ||
Ok(bytes.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.