Skip to content

Commit

Permalink
chore: add cargo fmt check (#85)
Browse files Browse the repository at this point in the history
* add cargo fmt check
  • Loading branch information
brunopgalvao authored Mar 26, 2024
1 parent a083c63 commit 5894fac
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ jobs:
target: wasm32-unknown-unknown
override: true
components: rustfmt, clippy, rust-src


- uses: actions/checkout@v4
- name: Check formatting
run: cargo fmt --all -- --check

- uses: actions/checkout@v3
- name: Check Feature Contracts Excl.
run: cargo check --no-default-features --features contract
Expand Down
12 changes: 8 additions & 4 deletions src/commands/new/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ mod tests {
#[test]
fn test_new_contract_command_execute_success() -> Result<()> {
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())) };
let command = NewContractCommand {
name: "test_contract".to_string(),
path: Some(PathBuf::from(temp_contract_dir.path())),
};
let result = command.execute();
assert!(result.is_ok());

Expand All @@ -76,8 +78,10 @@ mod tests {
#[test]
fn test_new_contract_command_execute_fails_path_no_exist() -> Result<()> {
let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir");
let command =
NewContractCommand { name: "test_contract".to_string(), path: Some(temp_contract_dir.path().join("new_contract")) };
let command = NewContractCommand {
name: "test_contract".to_string(),
path: Some(temp_contract_dir.path().join("new_contract")),
};
let result_error = command.execute();
assert!(result_error.is_err());
Ok(())
Expand Down
19 changes: 14 additions & 5 deletions src/engines/contract_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ pub async fn dry_run_call(
#[cfg(test)]
mod tests {
use super::*;
use anyhow::{Error, Result};
use std::{fs, path::PathBuf};
use anyhow::{Result, Error};

fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir");
Expand Down Expand Up @@ -199,7 +199,7 @@ mod tests {

// Verify that the generated Cargo.toml file contains the expected content
fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml"))
.expect("Could not read file");
.expect("Could not read file");
Ok(())
}

Expand All @@ -213,9 +213,18 @@ mod tests {
// 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());
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(())
}
Expand Down
9 changes: 6 additions & 3 deletions src/engines/parachain_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ mod tests {
let generated_file_content =
fs::read_to_string(temp_dir.path().join("node/src/chain_spec.rs"))
.expect("Failed to read file");
assert!(generated_file_content.contains("properties.insert(\"tokenSymbol\".into(), \"DOT\".into());"));
assert!(generated_file_content.contains("properties.insert(\"tokenDecimals\".into(), 18.into());"));
assert!(generated_file_content
.contains("properties.insert(\"tokenSymbol\".into(), \"DOT\".into());"));
assert!(generated_file_content
.contains("properties.insert(\"tokenDecimals\".into(), 18.into());"));
assert!(generated_file_content.contains("1000000"));

// Verify network.toml contains expected content
Expand All @@ -124,7 +126,8 @@ mod tests {

#[test]
fn test_parachain_build_after_instantiating_template() -> Result<()> {
let temp_dir = setup_template_and_instantiate().expect("Failed to setup template and instantiate");
let temp_dir =
setup_template_and_instantiate().expect("Failed to setup template and instantiate");
let build = build_parachain(&Some(temp_dir.path().to_path_buf()));
assert!(build.is_ok(), "Result should be Ok");
Ok(())
Expand Down
95 changes: 56 additions & 39 deletions tests/build_contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::{Error, Result};
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();
let temp_contract_dir = tempfile::tempdir().unwrap();
// pop new contract test_contract
Command::cargo_bin("pop")
.unwrap()
Expand All @@ -12,63 +12,80 @@ fn setup_test_environment() -> Result<tempfile::TempDir, Error> {
.assert()
.success();

Ok(temp_contract_dir)
Ok(temp_contract_dir)
}

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

// pop build contract
// pop build contract
Command::cargo_bin("pop")
.unwrap()
.current_dir(&temp_contract_dir.path().join("test_contract"))
.args(&["build", "contract"])
.assert()
.success();
.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());
// 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(())
Ok(())
}

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

// pop build contract --path ./test_contract
// 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();
.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());
// 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(())
Ok(())
}

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

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

Ok(())
Ok(())
}

0 comments on commit 5894fac

Please sign in to comment.