Skip to content

Commit

Permalink
cargo fmt --all
Browse files Browse the repository at this point in the history
  • Loading branch information
brunopgalvao committed Apr 17, 2024
1 parent 1613722 commit 00a4c7b
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 253 deletions.
87 changes: 43 additions & 44 deletions crates/pop-contracts/src/build.rs
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(())
}
}
92 changes: 47 additions & 45 deletions crates/pop-contracts/src/new.rs
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(())
}
}
98 changes: 53 additions & 45 deletions crates/pop-contracts/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,72 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to execute test command: {0}")]
TestCommandError(String),
#[error("Failed to set test directory: {0}")]
TestDirError(String),
#[error("Failed to create test environment: {0}")]
TestEnvironmentError(String),
#[error("Failed to execute test command: {0}")]
TestCommandError(String),
#[error("Failed to set test directory: {0}")]
TestDirError(String),
#[error("Failed to create test environment: {0}")]
TestEnvironmentError(String),
}

pub fn test_smart_contract(path: &Option<PathBuf>) -> Result<(), Error> {
// Execute `cargo test` command in the specified directory.
let result = cmd("cargo", vec!["test"])
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./")))
.run()
.map_err(|e| Error::TestCommandError(format!("Cargo test command failed: {}", e)))?;
// Execute `cargo test` command in the specified directory.
let result = cmd("cargo", vec!["test"])
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./")))
.run()
.map_err(|e| Error::TestCommandError(format!("Cargo test command failed: {}", e)))?;

if result.status.success() {
Ok(())
} else {
Err(Error::TestCommandError("Cargo test command failed.".to_string()))
}
if result.status.success() {
Ok(())
} else {
Err(Error::TestCommandError("Cargo test command failed.".to_string()))
}
}

pub fn test_e2e_smart_contract(path: &Option<PathBuf>) -> Result<(), Error> {
// Execute `cargo test --features=e2e-tests` command in the specified directory.
let result = cmd("cargo", vec!["test", "--features=e2e-tests"])
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./")))
.run()
.map_err(|e| Error::TestCommandError(format!("Cargo test command failed: {}", e)))?;
// Execute `cargo test --features=e2e-tests` command in the specified directory.
let result = cmd("cargo", vec!["test", "--features=e2e-tests"])
.dir(path.clone().unwrap_or_else(|| PathBuf::from("./")))
.run()
.map_err(|e| Error::TestCommandError(format!("Cargo test command failed: {}", e)))?;

if result.status.success() {
Ok(())
} else {
Err(Error::TestCommandError("Cargo test command failed.".to_string()))
}
if result.status.success() {
Ok(())
} else {
Err(Error::TestCommandError("Cargo test command failed.".to_string()))
}
}

#[cfg(feature = "unit_contract")]
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile;
use super::*;
use std::fs;
use tempfile;

fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
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).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()).map_err(|e| Error::TestEnvironmentError(format!("Failed to create smart contract: {}", e)))?;
assert!(result.is_ok(), "Contract test environment setup failed");
Ok(temp_dir)
}
fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
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).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())
.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(())
}
#[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(())
}
}
Loading

0 comments on commit 00a4c7b

Please sign in to comment.