pop_contracts/
testing.rs

1// SPDX-License-Identifier: GPL-3.0
2
3use crate::{create_smart_contract, Contract};
4use anyhow::Result;
5use std::{
6	fs::{copy, create_dir},
7	path::Path,
8};
9
10/// Generates a smart contract test environment.
11///
12/// * `name` - The name of the contract to be created.
13pub fn new_environment(name: &str) -> Result<tempfile::TempDir> {
14	let temp_dir = tempfile::tempdir().expect("Could not create temp dir");
15	let temp_contract_dir = temp_dir.path().join(name);
16	create_dir(&temp_contract_dir)?;
17	create_smart_contract(name, temp_contract_dir.as_path(), &Contract::Standard)?;
18	Ok(temp_dir)
19}
20
21/// Mocks the build process by generating contract artifacts in a specified temporary directory.
22///
23/// * `temp_contract_dir` - The root directory where the `target` folder and artifacts will be
24///   created.
25/// * `contract_file` - The path to the mocked contract file to be copied.
26/// * `metadata_file` - The path to the mocked metadata file to be copied.
27pub fn mock_build_process<P>(temp_contract_dir: P, contract_file: P, metadata_file: P) -> Result<()>
28where
29	P: AsRef<Path>,
30{
31	// Create a target directory
32	let target_contract_dir = temp_contract_dir.as_ref().join("target");
33	create_dir(&target_contract_dir)?;
34	create_dir(target_contract_dir.join("ink"))?;
35	// Copy a mocked testing.contract and testing.json files inside the target directory
36	copy(contract_file, target_contract_dir.join("ink/testing.contract"))?;
37	copy(metadata_file, target_contract_dir.join("ink/testing.json"))?;
38	Ok(())
39}