From ba3d163fa78f63d3aaa3f512cbafb9bb3cb31896 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Mon, 1 Jul 2024 20:36:10 +0200 Subject: [PATCH 1/2] fix: set variable CARGO_TARGET_DIR --- crates/pop-contracts/src/build.rs | 40 +++++++++++++++++++++++++----- crates/pop-contracts/src/errors.rs | 3 +++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/pop-contracts/src/build.rs b/crates/pop-contracts/src/build.rs index 6b1c2744..6b7cd831 100644 --- a/crates/pop-contracts/src/build.rs +++ b/crates/pop-contracts/src/build.rs @@ -1,13 +1,15 @@ // SPDX-License-Identifier: GPL-3.0 +use crate::{errors::Error, utils::helpers::get_manifest_path}; use contract_build::{execute, BuildMode, ExecuteArgs}; -use std::path::PathBuf; - -use crate::utils::helpers::get_manifest_path; +use std::{env, path::PathBuf}; /// Build the smart contract located at the specified `path` in `build_release` mode. -pub fn build_smart_contract(path: &Option, build_release: bool) -> anyhow::Result { - let manifest_path = get_manifest_path(path)?; +pub fn build_smart_contract(path: &Option, build_release: bool) -> Result { + let manifest_path = get_manifest_path(&path)?; + // Set the CARGO_TARGET_DIR variable to this project target + let target_path = manifest_path.absolute_directory()?.join("target"); + env::set_var("CARGO_TARGET_DIR", target_path.display().to_string()); let build_mode = match build_release { true => BuildMode::Release, @@ -17,8 +19,34 @@ pub fn build_smart_contract(path: &Option, build_release: bool) -> anyh let args = ExecuteArgs { manifest_path, build_mode, ..Default::default() }; // Execute the build and log the output of the build - let result = execute(args)?; + let result = + execute(args).map_err(|error| Error::BuildContractError(format!("{:?}", error)))?; let formatted_result = result.display(); Ok(formatted_result) } + +#[cfg(test)] +mod tests { + use super::*; + use anyhow::Result; + use duct::cmd; + use tempfile::tempdir; + + #[test] + fn build_parachain_fails_no_ink_project() -> Result<()> { + let temp_dir = tempdir()?; + let name = "my_contract"; + cmd("cargo", ["new", name, "--bin"]).dir(temp_dir.path()).run()?; + assert!(matches!( + build_smart_contract(&Some(PathBuf::from(temp_dir.path().join(name))), false), + Err(Error::BuildContractError(..)) + )); + // Assert env variable has been set + assert_eq!( + env::var("CARGO_TARGET_DIR")?, + temp_dir.path().join("my_contract/target").display().to_string() + ); + Ok(()) + } +} diff --git a/crates/pop-contracts/src/errors.rs b/crates/pop-contracts/src/errors.rs index 6f7201ca..7350cb7f 100644 --- a/crates/pop-contracts/src/errors.rs +++ b/crates/pop-contracts/src/errors.rs @@ -57,4 +57,7 @@ pub enum Error { #[error("HTTP error: {0}")] HttpError(#[from] reqwest::Error), + + #[error("Failed to build the contract: {0}")] + BuildContractError(String), } From 2360eb4af6f0bfaf1b00419e4f0ecb7f750b3701 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Tue, 2 Jul 2024 10:45:13 +0200 Subject: [PATCH 2/2] fix: unit test --- crates/pop-contracts/src/build.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/pop-contracts/src/build.rs b/crates/pop-contracts/src/build.rs index 6b7cd831..ead63d69 100644 --- a/crates/pop-contracts/src/build.rs +++ b/crates/pop-contracts/src/build.rs @@ -45,7 +45,10 @@ mod tests { // Assert env variable has been set assert_eq!( env::var("CARGO_TARGET_DIR")?, - temp_dir.path().join("my_contract/target").display().to_string() + format!( + "/private{}", // manifest_path.absolute_directory() adds a /private prefix + PathBuf::from(temp_dir.path().join("my_contract/target")).display().to_string() + ) ); Ok(()) }