Skip to content

Commit

Permalink
Fix code coverage compilation and tests (#1943)
Browse files Browse the repository at this point in the history
The compilation was failing on the latest nightly. Most imporantly, it
caught one test that was incorrectly disabled due to incorrect
`cfg`-gating. Found in preparation for code coverage setup. After this
PR, the following command should work for getting codecov:

```bash
cargo +nightly-2024-06-05 llvm-cov --html --branch --all-features
```
  • Loading branch information
Dentosal committed Jun 7, 2024
1 parent d5b99e1 commit 8bb1c81
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion crates/chain-config/src/config/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct ContractConfig {
}

#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct ContractStateConfig {
pub key: Bytes32,
#[serde_as(as = "HexIfHumanReadable")]
Expand Down
32 changes: 18 additions & 14 deletions crates/fuel-core/src/database/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ mod tests {

mod update_contract_state {
use core::iter::repeat_with;
use fuel_core_chain_config::Randomize;
use fuel_core_chain_config::{
ContractStateConfig,
Randomize,
};

use fuel_core_storage::iter::IteratorOverTable;
use fuel_core_types::fuel_merkle::sparse::{
self,
MerkleTreeKey,
Expand All @@ -209,7 +213,7 @@ mod tests {
use std::collections::HashSet;

use super::*;
#[cfg(all(test, feature = "random", feature = "std"))]

#[test]
fn states_inserted_into_db() {
// given
Expand All @@ -231,29 +235,29 @@ mod tests {
}

// then
let states_in_db: Vec<_> = database
let mut states_in_db: Vec<_> = database
.iter_all::<ContractsState>(None)
.collect::<Result<Vec<_>, _>>()
.unwrap()
.into_iter()
.map(|(key, value)| {
let contract_id = *key.contract_id();
let key = *key.state_key();

ContractStateConfig {
contract_id,
key,
value: value.0,
}
.map(|(key, value)| ContractStateConfig {
key: *key.state_key(),
value: value.0,
})
.collect();

let original_state = state_groups
let mut original_state = state_groups
.into_iter()
.flatten()
.sorted()
.map(|entry| ContractStateConfig {
key: *entry.key.state_key(),
value: entry.value.into(),
})
.collect::<Vec<_>>();

states_in_db.sort();
original_state.sort();

assert_eq!(states_in_db, original_state);
}

Expand Down
4 changes: 4 additions & 0 deletions crates/services/upgradable-executor/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ fn build_wasm() {
cargo.env("CARGO_PROFILE_RELEASE_OPT_LEVEL", "3");
cargo.env("CARGO_PROFILE_RELEASE_STRIP", "symbols");
cargo.env("CARGO_PROFILE_RELEASE_DEBUG", "false");
if env::var("CARGO_CFG_COVERAGE").is_ok() {
// wasm doesn't support coverage
cargo.env("CARGO_ENCODED_RUSTFLAGS", "");
}
cargo.current_dir(project_root()).args(args);

let output = cargo.output();
Expand Down
6 changes: 6 additions & 0 deletions crates/services/upgradable-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,12 @@ where
}

#[allow(clippy::cast_possible_truncation)]
#[allow(unexpected_cfgs)] // for cfg(coverage)
#[cfg(test)]
mod test {
#[cfg(coverage)]
use ntest as _; // Only used outside cdg(coverage)

use super::*;
use fuel_core_storage::{
kv_store::Value,
Expand Down Expand Up @@ -980,6 +984,7 @@ mod test {
// The test verifies that `Executor::get_module` method caches the compiled WASM module.
// If it doesn't cache the modules, the test will fail with a timeout.
#[test]
#[cfg(not(coverage))] // Too slow for coverage
#[ntest::timeout(60_000)]
fn reuse_cached_compiled_module__native_strategy() {
// Given
Expand All @@ -1000,6 +1005,7 @@ mod test {
// The test verifies that `Executor::get_module` method caches the compiled WASM module.
// If it doesn't cache the modules, the test will fail with a timeout.
#[test]
#[cfg(not(coverage))] // Too slow for coverage
#[ntest::timeout(60_000)]
fn reuse_cached_compiled_module__wasm_strategy() {
// Given
Expand Down
1 change: 1 addition & 0 deletions tests/tests/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ mod full_block {
schema_path = "../crates/client/assets/schema.sdl",
graphql_type = "Block"
)]
#[allow(dead_code)]
pub struct FullBlock {
pub id: BlockId,
pub header: Header,
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/poa.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unexpected_cfgs)] // for cfg(coverage)

use fuel_core::{
combined_database::CombinedDatabase,
service::{
Expand Down Expand Up @@ -83,6 +85,7 @@ async fn can_get_sealed_block_from_poa_produced_block() {
}

#[cfg(feature = "p2p")]
#[cfg(not(coverage))] // too slow for coverage
mod p2p {
use super::*;
use fuel_core::{
Expand Down
6 changes: 5 additions & 1 deletion tests/tests/sync.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unexpected_cfgs)] // for cfg(coverage)

use fuel_core::p2p_test_helpers::*;
use fuel_core_types::{
fuel_crypto::SecretKey,
Expand All @@ -12,7 +14,6 @@ use std::{
collections::{
hash_map::DefaultHasher,
HashMap,
VecDeque,
},
hash::{
Hash,
Expand Down Expand Up @@ -127,11 +128,14 @@ async fn test_partition_single(num_txs: usize) {
#[test_case(10, 8, 4; "partition with 10 txs 8 validators 4 partitions")]
#[test_case(100, 8, 4; "partition with 100 txs 8 validators 4 partitions")]
#[tokio::test(flavor = "multi_thread")]
#[cfg(not(coverage))] // This test is too slow for coverage
async fn test_partitions_larger_groups(
num_txs: usize,
num_validators: usize,
num_partitions: usize,
) {
use std::collections::VecDeque;

// Create a random seed based on the test parameters.
let mut hasher = DefaultHasher::new();
(num_txs, num_validators, num_partitions, line!()).hash(&mut hasher);
Expand Down

0 comments on commit 8bb1c81

Please sign in to comment.