diff --git a/near-sdk-macros/Cargo.toml b/near-sdk-macros/Cargo.toml index 86094e4e6..dfc64a6e7 100644 --- a/near-sdk-macros/Cargo.toml +++ b/near-sdk-macros/Cargo.toml @@ -31,6 +31,8 @@ prettyplease = { version = "0.2.15" } [features] +wasmcov = [] abi = [] __abi-embed = ["abi"] __abi-generate = ["abi"] + diff --git a/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs b/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs index fd5d507d2..98b5f6888 100644 --- a/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs +++ b/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs @@ -31,6 +31,8 @@ impl ImplItemMethodInfo { ReturnKind::HandlesResult { .. } => self.result_return_body_tokens(), }; + let wasmcov = self.wasmcov_tokens(); + quote! { #non_bindgen_attrs #[cfg(target_arch = "wasm32")] @@ -45,6 +47,7 @@ impl ImplItemMethodInfo { #callback_vec_deser #state_check #body + #wasmcov } } } @@ -247,6 +250,31 @@ impl ImplItemMethodInfo { } } + fn wasmcov_tokens(&self) -> TokenStream2 { + fn wasmcov() -> TokenStream2 { + quote! { + ::near_sdk::env::log_str("wasmcov"); + + 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); + } + } + + wasmcov() + + // Passing feature to dependency of dependency doesn't seem to work + // if cfg!(feature = "wasmcov") { + // wasmcov() + // } else { + // quote! {} + // } + } + fn contract_ser_tokens(&self) -> TokenStream2 { use MethodKind::*; diff --git a/near-sdk/Cargo.toml b/near-sdk/Cargo.toml index ce20f512e..f156d33ae 100644 --- a/near-sdk/Cargo.toml +++ b/near-sdk/Cargo.toml @@ -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" @@ -66,6 +67,7 @@ default = ["wee_alloc", "unit-testing", "legacy", "abi"] expensive-debug = [] unstable = [] legacy = [] +wasmcov = ["near-sdk-macros/wasmcov"] abi = ["borsh/unstable__schema", "near-abi", "schemars", "near-sdk-macros/abi", "near-account-id/abi", "near-gas/abi", "near-token/abi"] unit-testing = ["near-vm-runner", "near-primitives-core", "near-primitives", "near-crypto", "near-parameters"] diff --git a/near-sdk/src/lib.rs b/near-sdk/src/lib.rs index 5042b7533..a678722d7 100644 --- a/near-sdk/src/lib.rs +++ b/near-sdk/src/lib.rs @@ -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;