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: pop build contract #83

Merged
merged 6 commits into from
Mar 25, 2024
Merged
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
100 changes: 100 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ tracing-subscriber = { version = "0.3", optional = true }
zombienet-sdk = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", optional = true }
zombienet-support = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", optional = true }

[dev-dependencies]
assert_cmd = "2.0.14"
predicates = "3.1.0"

[features]
default = ["contract", "parachain"]
contract = [
Expand Down
14 changes: 13 additions & 1 deletion src/commands/new/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ impl NewContractCommand {
mod tests {
use super::*;
use std::fs;
use anyhow::{Result, Error};

#[test]
fn test_new_contract_command_execute() -> anyhow::Result<()> {
fn test_new_contract_command_execute_success() -> Result<(), Error> {
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
let command =
NewContractCommand { name: "test_contract".to_string(), path: Some(PathBuf::new()) };
let result = command.execute();
Expand All @@ -75,4 +76,15 @@ mod tests {
}
Ok(())
}

#[test]
fn test_new_contract_command_execute_fails_path_no_exist() -> Result<(), Error> {
let mut path = PathBuf::new();
path.push("test_contract");
let command =
NewContractCommand { name: "test_contract".to_string(), path: Some(path) };
let result_error = command.execute();
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
assert!(result_error.is_err());
Ok(())
}
}
17 changes: 17 additions & 0 deletions src/engines/contract_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ mod tests {
Ok(())
}

#[test]
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")));
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").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(())
}

#[test]
fn test_contract_test() -> Result<(), Error> {
let temp_contract_dir = setup_test_environment()?;
Expand Down
74 changes: 74 additions & 0 deletions tests/build_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use assert_cmd::Command;
use predicates::prelude::*;
use anyhow::{Result, Error};

fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
let temp_contract_dir = tempfile::tempdir().unwrap();
// pop new contract test_contract
Command::cargo_bin("pop")
.unwrap()
.current_dir(&temp_contract_dir)
.args(&["new", "contract", "test_contract"])
.assert()
.success();

Ok(temp_contract_dir)
}

#[test]
fn test_contract_build() -> Result<(), Error> {
let temp_contract_dir = setup_test_environment()?;

// pop build contract
Command::cargo_bin("pop")
.unwrap()
.current_dir(&temp_contract_dir.path().join("test_contract"))
.args(&["build", "contract"])
.assert()
.success();

// 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").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(())
}

#[test]
fn test_contract_build_specify_path() -> Result<(), Error> {
let temp_contract_dir = setup_test_environment()?;

// pop build contract --path ./test_contract
Command::cargo_bin("pop")
.unwrap()
.current_dir(&temp_contract_dir.path())
.args(&["build", "contract", "--path", "./test_contract"])
.assert()
.success();

// 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").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(())
}

#[test]
fn test_contract_build_fails_if_no_contract_exists() -> Result<(), Error> {

// pop build contract
Command::cargo_bin("pop")
.unwrap()
.args(&["build", "contract",])
.assert()
.failure()
.stderr(predicate::str::contains("Error: No 'ink' dependency found"));

Ok(())
}
Loading