Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase coverage build function #194

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions crates/pop-cli/src/commands/build/contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-License-Identifier: GPL-3.0

use std::path::PathBuf;

use clap::Args;
use cliclack::{clear_screen, intro, log, outro, set_theme};
use console::style;
use std::path::PathBuf;

AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(test)]
use crate::mock::build_smart_contract;
use crate::style::Theme;
#[cfg(not(test))]
use pop_contracts::build_smart_contract;

#[derive(Args)]
Expand All @@ -27,3 +28,32 @@ impl BuildContractCommand {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::new::contract::NewContractCommand;
use anyhow::{Error, Result};

async fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir");
let command = NewContractCommand {
name: "test_contract".to_string(),
path: Some(PathBuf::from(temp_contract_dir.path())),
};
command.execute().await?;

Ok(temp_contract_dir)
}

#[tokio::test]
async fn test_build_success() -> Result<()> {
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
let temp_dir = setup_test_environment().await?;
let command = BuildContractCommand {
path: Some(PathBuf::from(temp_dir.path().join("test_contract"))),
};

command.execute()?;
Ok(())
}
}
39 changes: 38 additions & 1 deletion crates/pop-cli/src/commands/build/parachain.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: GPL-3.0

#[cfg(test)]
use crate::mock::build_parachain;
use crate::style::{style, Theme};
use clap::Args;
use cliclack::{clear_screen, intro, log::warning, outro, set_theme};
#[cfg(not(test))]
use pop_parachains::build_parachain;
use std::path::PathBuf;

Expand All @@ -29,3 +31,38 @@ impl BuildParachainCommand {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::new::parachain::NewParachainCommand;
use anyhow::{Error, Result};
use pop_parachains::{Provider, Template};

async fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
let temp_dir = tempfile::tempdir().expect("Could not create temp dir");
let command = NewParachainCommand {
name: Some(temp_dir.path().join("test_parachain").to_str().unwrap().to_string()),
provider: Some(Provider::Pop),
template: Some(Template::Standard),
release_tag: None,
symbol: Some("UNIT".to_string()),
decimals: Some(12),
initial_endowment: Some("1u64 << 60".to_string()),
};
command.execute().await?;

Ok(temp_dir)
}

#[tokio::test]
async fn test_build_success() -> Result<()> {
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
let temp_dir = setup_test_environment().await?;
let command = BuildParachainCommand {
path: Some(PathBuf::from(temp_dir.path().join("test_parachain"))),
};

command.execute()?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions crates/pop-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ compile_error!("feature \"contract\" or feature \"parachain\" must be enabled");

#[cfg(any(feature = "parachain", feature = "contract"))]
mod commands;
#[cfg(test)]
mod mock;
mod style;

use anyhow::anyhow;
Expand Down
14 changes: 14 additions & 0 deletions crates/pop-cli/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-3.0
use std::path::PathBuf;

// Mock the build function for parachains
#[cfg(test)]
pub fn build_parachain(_path: &Option<PathBuf>) -> anyhow::Result<()> {
Ok(())
}

// Mock the build function for smart contracts
#[cfg(test)]
pub fn build_smart_contract(_path: &Option<PathBuf>) -> anyhow::Result<String> {
Ok("Ok".to_string())
}
24 changes: 22 additions & 2 deletions crates/pop-contracts/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// SPDX-License-Identifier: GPL-3.0
use contract_build::{execute, ExecuteArgs};
#[cfg(test)]
use crate::mock::execute;
#[cfg(not(test))]
use contract_build::execute;
use std::path::PathBuf;

use crate::utils::helpers::get_manifest_path;
Expand All @@ -8,11 +11,28 @@ use crate::utils::helpers::get_manifest_path;
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 args = contract_build::ExecuteArgs { manifest_path, ..Default::default() };

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

Ok(formatted_result)
}

#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;

#[test]
fn test_build_contract() -> Result<()> {
let temp_dir = tempfile::tempdir().expect("Could not create temp dir");
let result = build_smart_contract(&Some(PathBuf::from(temp_dir.path())))?;
assert!(result.contains("Original wasm size:"));
assert!(result.contains("64.0K"));
assert!(result.contains("Optimized:"));
assert!(result.contains("32.0K"));
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/pop-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ mod test;
mod up;
mod utils;

AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(test)]
mod mock;

pub use build::build_smart_contract;
pub use call::{
call_smart_contract, dry_run_call, dry_run_gas_estimate_call, set_up_call, CallOpts,
Expand Down
23 changes: 23 additions & 0 deletions crates/pop-contracts/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0
use std::path::PathBuf;

AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
use contract_build::{
BuildResult, ExecuteArgs, MetadataArtifacts, OptimizationResult, OutputType, Verbosity,
};
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
// Mock the call to the `execute` function of the `contract_build' crate that builds the contract
pub fn execute(_args: ExecuteArgs) -> anyhow::Result<BuildResult> {
Ok(BuildResult {
dest_wasm: Some(PathBuf::from("/path/to/contract.wasm")),
metadata_result: Some(MetadataArtifacts {
dest_metadata: PathBuf::from("/path/to/contract.json"),
dest_bundle: PathBuf::from("/path/to/contract.contract"),
}),
target_directory: PathBuf::from("/path/to/target"),
optimization_result: Some(OptimizationResult { original_size: 64.0, optimized_size: 32.0 }),
build_mode: Default::default(),
build_artifact: Default::default(),
image: None,
verbosity: Verbosity::Quiet,
output_type: OutputType::Json,
})
}
16 changes: 16 additions & 0 deletions crates/pop-parachains/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// SPDX-License-Identifier: GPL-3.0
#[cfg(test)]
use crate::mock::cmd;
#[cfg(not(test))]
use duct::cmd;
use std::path::PathBuf;

Expand All @@ -10,3 +13,16 @@ pub fn build_parachain(path: &Option<PathBuf>) -> anyhow::Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;

#[test]
fn test_build_parachain() -> Result<()> {
let temp_dir = tempfile::tempdir().expect("Could not create temp dir");
build_parachain(&Some(PathBuf::from(temp_dir.path())))?;
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/pop-parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ mod templates;
mod up;
mod utils;

#[cfg(test)]
mod mock;

pub use build::build_parachain;
pub use errors::Error;
pub use new_pallet::{create_pallet_template, TemplatePalletConfig};
Expand Down
6 changes: 6 additions & 0 deletions crates/pop-parachains/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: GPL-3.0

// Mock the command that builds the parachain
pub fn cmd(_program: &str, _args: Vec<&str>) -> duct::Expression {
duct::cmd!("echo")
}
Loading