Skip to content

Commit

Permalink
add wasmcov macro
Browse files Browse the repository at this point in the history
  • Loading branch information
njelich committed Jan 18, 2024
1 parent 1343059 commit 1f78437
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ prettyplease = { version = "0.2.15" }
abi = []
__abi-embed = ["abi"]
__abi-generate = ["abi"]
wasmcov = []
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ impl ImplItemMethodInfo {
fn void_return_body_tokens(&self) -> TokenStream2 {
let contract_init = self.contract_init_tokens();
let method_invocation = self.method_invocation_tokens();
let wasmcov = self.wasmcov_tokens();
let contract_ser = self.contract_ser_tokens();

quote! {
#contract_init
#method_invocation;
#wasmcov
#contract_ser
}
}
Expand All @@ -67,12 +69,14 @@ impl ImplItemMethodInfo {
let contract_ser = self.contract_ser_tokens();
let value_ser = self.value_ser_tokens();
let value_return = self.value_return_tokens();
let wasmcov = self.wasmcov_tokens();

quote! {
#contract_init
#method_invocation_with_return
#value_ser
#value_return
#wasmcov
#contract_ser
}
}
Expand All @@ -84,6 +88,7 @@ impl ImplItemMethodInfo {
let value_ser = self.value_ser_tokens();
let value_return = self.value_return_tokens();
let result_identifier = self.result_identifier();
let wasmcov = self.wasmcov_tokens();

quote! {
#contract_init
Expand All @@ -92,6 +97,7 @@ impl ImplItemMethodInfo {
::std::result::Result::Ok(#result_identifier) => {
#value_ser
#value_return
#wasmcov
#contract_ser
}
::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err)
Expand Down Expand Up @@ -247,6 +253,26 @@ impl ImplItemMethodInfo {
}
}

fn wasmcov_tokens(&self) -> TokenStream2 {
fn wasmcov() -> TokenStream2 {
quote! {
let mut coverage = vec![];
unsafe {
// Note that this function is not thread-safe! Use a lock if needed.
::near_sdk::minicov::capture_coverage(&mut coverage).unwrap();
};
let base64_string = near_sdk::base64::encode(coverage);
::near_sdk::env::log_str(&base64_string);
}
}

if cfg!(feature = "wasmcov") {
wasmcov()
} else {
quote! {}
}
}

fn contract_ser_tokens(&self) -> TokenStream2 {
use MethodKind::*;

Expand Down
2 changes: 2 additions & 0 deletions near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "compilation_tests/all.rs"
required-features = ["abi", "unstable"]

[dependencies]
minicov = "0.3"
# Provide near_bidgen macros.
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down Expand Up @@ -67,6 +68,7 @@ unit-testing = ["near-vm-logic", "near-primitives-core", "near-primitives", "nea

__abi-embed = ["near-sdk-macros/__abi-embed"]
__abi-generate = ["abi", "near-sdk-macros/__abi-generate"]
wasmcov = ["near-sdk-macros/wasmcov"]

[package.metadata.docs.rs]
features = ["unstable", "legacy"]
2 changes: 2 additions & 0 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub use base64;
pub use borsh;
pub use bs58;
#[doc(hidden)]
pub use minicov;
#[cfg(feature = "abi")]
pub use schemars;
pub use serde;
Expand Down

0 comments on commit 1f78437

Please sign in to comment.