Skip to content

Commit

Permalink
revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brunopgalvao committed Apr 17, 2024
1 parent 00a4c7b commit b0df29f
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions crates/pop-contracts/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
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),
}

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))),
};

pub fn build_smart_contract(path: &Option<PathBuf>) -> anyhow::Result<String> {
let manifest_path = get_manifest_path(path)?;
// Default values
let args = ExecuteArgs { manifest_path, ..Default::default() };
let result = execute(args).map_err(|e| Error::BuildError(format!("{}", e)))?;

// Execute the build and log the output of the build
let result = execute(args)?;
let formatted_result = result.display();

Ok(formatted_result)
}

#[cfg(feature = "unit_contract")]
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use anyhow::{Error, Result};
use std::fs;

fn setup_test_environment() -> Result<tempfile::TempDir> {
let temp_dir = tempfile::tempdir()?;
fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
let temp_dir = tempfile::tempdir().expect("Could not create temp dir");
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())?;
let result =
crate::create_smart_contract("test_contract".to_string(), temp_contract_dir.as_path());
assert!(result.is_ok(), "Contract test environment setup failed");

Ok(temp_dir)
}

#[test]
fn test_contract_build() -> Result<()> {
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")))?;

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")
Expand All @@ -61,4 +58,4 @@ mod tests {

Ok(())
}
}
}

0 comments on commit b0df29f

Please sign in to comment.