Skip to content

Commit

Permalink
Merge pull request #431 from Concordium/load-module-under-test
Browse files Browse the repository at this point in the history
Add support for loading the contract module under test
  • Loading branch information
lassemoldrup authored May 28, 2024
2 parents 0d0260b + 9cf2385 commit a4c56f9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions contract-testing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Integrate protocol version 7 cost semantics.
- The `ContractInvokeSuccess` and `ContractInvokeError` have additional fields
that record where parts of the energy was allocated during execution.
- Add support for loading the contract under test with the `module_load_output` function. The module path is exposed by `cargo-concordium` through the `CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH` environment variable.

## 4.2.0

Expand Down
5 changes: 5 additions & 0 deletions contract-testing/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ pub(crate) const INITIALIZE_CONTRACT_INSTANCE_CREATE_COST: Energy = Energy {
pub(crate) const UPDATE_CONTRACT_INSTANCE_BASE_COST: Energy = Energy {
energy: 300,
};

/// The name of the environment variable that holds the path to the contract
/// module file. To load the module, use the
/// [`module_load_output`](crate::module_load_output) function.
pub const CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR: &str = "CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH";
11 changes: 11 additions & 0 deletions contract-testing/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
constants,
invocation::{ChangeSet, EntrypointInvocationHandler, TestConfigurationError},
types::*,
CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR,
};
use anyhow::anyhow;
use concordium_rust_sdk::{
Expand Down Expand Up @@ -37,6 +38,7 @@ use sdk::{
};
use std::{
collections::{BTreeMap, BTreeSet},
env,
future::Future,
path::Path,
sync::Arc,
Expand Down Expand Up @@ -2058,6 +2060,15 @@ pub fn module_load_v1(module_path: impl AsRef<Path>) -> Result<WasmModule, Modul
Ok(module)
}

/// Load the current smart contract module output using the environment variable
/// `CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH` which is set when running using
/// `cargo concordium test`.
pub fn module_load_output() -> Result<WasmModule, OutputModuleLoadError> {
let module_path = env::var(CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR)?;
let module = module_load_v1(module_path)?;
Ok(module)
}

impl Signer {
/// Create a signer which always signs with one key.
pub const fn with_one_key() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions contract-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@
//! - deployment.transaction_fee
//! - initialization.transaction_fee
//! - update.transaction_fee));
//!
//! ```
mod constants;
mod impls;
mod invocation;
mod types;
pub use impls::{is_debug_enabled, module_load_v1, module_load_v1_raw};
pub use constants::CONTRACT_MODULE_OUTPUT_PATH_ENV_VAR;
pub use impls::{is_debug_enabled, module_load_output, module_load_v1, module_load_v1_raw};
pub use types::*;

// Re-export types.
Expand Down
12 changes: 12 additions & 0 deletions contract-testing/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use concordium_rust_sdk::{
};
use std::{
collections::{BTreeMap, BTreeSet},
env,
path::PathBuf,
sync::Arc,
};
Expand Down Expand Up @@ -325,6 +326,17 @@ pub enum ModuleLoadErrorKind {
UnsupportedModuleVersion(WasmVersion),
}

#[derive(Debug, Error)]
pub enum OutputModuleLoadError {
#[error(
"Module path environment variable was not set or invalid. Please ensure that \
cargo-concordium is up-to-date. Details: {0}"
)]
MissingEnvVar(#[from] env::VarError),
#[error("{0}")]
ModuleLoad(#[from] ModuleLoadError),
}

/// The error produced when trying to read a smart contract
/// module from a file.
#[derive(Debug, Error)]
Expand Down

0 comments on commit a4c56f9

Please sign in to comment.