-
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.
- Loading branch information
1 parent
1613722
commit 00a4c7b
Showing
8 changed files
with
259 additions
and
253 deletions.
There are no files selected for viewing
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,65 +1,64 @@ | ||
use thiserror::Error; | ||
use contract_build::{execute, ExecuteArgs}; | ||
use std::path::PathBuf; | ||
use thiserror::Error; | ||
|
||
use crate::utils::helpers::get_manifest_path; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Failed to build smart contract: {0}")] | ||
BuildError(String), | ||
#[error("Contract test environment setup failed: {0}")] | ||
SetupError(String), | ||
#[error("Failed to build smart contract: {0}")] | ||
BuildError(String), | ||
#[error("Contract test environment setup failed: {0}")] | ||
SetupError(String), | ||
} | ||
|
||
pub fn build_smart_contract(path: &Option<PathBuf>) -> Result<String, Error> { | ||
let manifest_path = match get_manifest_path(path) { | ||
Ok(path) => path, | ||
Err(e) => return Err(Error::BuildError(format!("Failed to get manifest path: {}", e))), | ||
}; | ||
let manifest_path = match get_manifest_path(path) { | ||
Ok(path) => path, | ||
Err(e) => return Err(Error::BuildError(format!("Failed to get manifest path: {}", e))), | ||
}; | ||
|
||
let args = ExecuteArgs { manifest_path, ..Default::default() }; | ||
let result = execute(args).map_err(|e| Error::BuildError(format!("{}", e)))?; | ||
let formatted_result = result.display(); | ||
Ok(formatted_result) | ||
let args = ExecuteArgs { manifest_path, ..Default::default() }; | ||
let result = execute(args).map_err(|e| Error::BuildError(format!("{}", e)))?; | ||
let formatted_result = result.display(); | ||
Ok(formatted_result) | ||
} | ||
|
||
|
||
#[cfg(feature = "unit_contract")] | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use anyhow::Result; | ||
use std::fs; | ||
use super::*; | ||
use anyhow::Result; | ||
use std::fs; | ||
|
||
fn setup_test_environment() -> Result<tempfile::TempDir> { | ||
let temp_dir = tempfile::tempdir()?; | ||
let temp_contract_dir = temp_dir.path().join("test_contract"); | ||
fs::create_dir(&temp_contract_dir)?; | ||
crate::create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path())?; | ||
Ok(temp_dir) | ||
} | ||
fn setup_test_environment() -> Result<tempfile::TempDir> { | ||
let temp_dir = tempfile::tempdir()?; | ||
let temp_contract_dir = temp_dir.path().join("test_contract"); | ||
fs::create_dir(&temp_contract_dir)?; | ||
crate::create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path())?; | ||
Ok(temp_dir) | ||
} | ||
|
||
#[test] | ||
fn test_contract_build() -> Result<()> { | ||
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"); | ||
#[test] | ||
fn test_contract_build() -> Result<()> { | ||
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"); | ||
|
||
assert!(temp_contract_dir.path().join("test_contract/target").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()); | ||
assert!(temp_contract_dir.path().join("test_contract/target").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(()) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,67 @@ | ||
use thiserror::Error; | ||
use contract_build::new_contract_project; | ||
use std::path::Path; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Failed to create new contract project: {0}")] | ||
NewContractError(String), | ||
#[error("IO error: {0}")] | ||
IoError(#[from] std::io::Error), | ||
#[error("Failed to create new contract project: {0}")] | ||
NewContractError(String), | ||
#[error("IO error: {0}")] | ||
IoError(#[from] std::io::Error), | ||
} | ||
|
||
pub fn create_smart_contract(name: String, target: &Path) -> Result<(), Error> { | ||
// Canonicalize the target path to ensure consistency and resolve any symbolic links. | ||
let canonicalized_path = target.canonicalize() | ||
// If an I/O error occurs during canonicalization, convert it into an Error enum variant. | ||
.map_err(|e| Error::IoError(e))?; | ||
|
||
// Retrieve the parent directory of the canonicalized path. | ||
let parent_path = canonicalized_path.parent() | ||
// If the parent directory cannot be retrieved (e.g., if the path has no parent), | ||
// return a NewContractError variant indicating the failure. | ||
.ok_or(Error::NewContractError("Failed to get parent directory".to_string()))?; | ||
|
||
// Create a new contract project with the provided name in the parent directory. | ||
new_contract_project(&name, Some(parent_path)) | ||
// If an error occurs during the creation of the contract project, | ||
// convert it into a NewContractError variant with a formatted error message. | ||
.map_err(|e| Error::NewContractError(format!("{}", e))) | ||
// Canonicalize the target path to ensure consistency and resolve any symbolic links. | ||
let canonicalized_path = target | ||
.canonicalize() | ||
// If an I/O error occurs during canonicalization, convert it into an Error enum variant. | ||
.map_err(|e| Error::IoError(e))?; | ||
|
||
// Retrieve the parent directory of the canonicalized path. | ||
let parent_path = canonicalized_path | ||
.parent() | ||
// If the parent directory cannot be retrieved (e.g., if the path has no parent), | ||
// return a NewContractError variant indicating the failure. | ||
.ok_or(Error::NewContractError("Failed to get parent directory".to_string()))?; | ||
|
||
// Create a new contract project with the provided name in the parent directory. | ||
new_contract_project(&name, Some(parent_path)) | ||
// If an error occurs during the creation of the contract project, | ||
// convert it into a NewContractError variant with a formatted error message. | ||
.map_err(|e| Error::NewContractError(format!("{}", e))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
use tempfile; | ||
use super::*; | ||
use anyhow::{Error, Result}; | ||
use std::fs; | ||
use tempfile; | ||
|
||
fn setup_test_environment() -> Result<tempfile::TempDir, Error> { | ||
let temp_dir = tempfile::tempdir()?; | ||
let temp_contract_dir = temp_dir.path().join("test_contract"); | ||
fs::create_dir(&temp_contract_dir)?; | ||
create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path())?; | ||
Ok(temp_dir) | ||
} | ||
fn setup_test_environment() -> Result<tempfile::TempDir, Error> { | ||
let temp_dir = tempfile::tempdir()?; | ||
let temp_contract_dir = temp_dir.path().join("test_contract"); | ||
fs::create_dir(&temp_contract_dir)?; | ||
create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path())?; | ||
Ok(temp_dir) | ||
} | ||
|
||
#[test] | ||
fn test_create_smart_contract_success() -> Result<(), Error> { | ||
let temp_dir = setup_test_environment()?; | ||
#[test] | ||
fn test_create_smart_contract_success() -> Result<(), Error> { | ||
let temp_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_contract/lib.rs")) | ||
.expect("Could not read file"); | ||
// Verify that the generated smart contract contains the expected content | ||
let generated_file_content = | ||
fs::read_to_string(temp_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 {")); | ||
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 | ||
fs::read_to_string(temp_dir.path().join("test_contract/Cargo.toml")) | ||
.expect("Could not read file"); | ||
// Verify that the generated Cargo.toml file contains the expected content | ||
fs::read_to_string(temp_dir.path().join("test_contract/Cargo.toml")) | ||
.expect("Could not read file"); | ||
|
||
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
Oops, something went wrong.