Skip to content

Commit

Permalink
Generate and publish code coverage reports in the CI (#1947)
Browse files Browse the repository at this point in the history
Fuel-core counterpart for FuelLabs/fuel-vm#750

This PR sets up code coverage reporting. It commits the report to [a
branch called
`codecov-pages`](https://github.com/FuelLabs/fuel-core/tree/codecov-pages),
which contains a folder for each branch. Branch names are
urlsafe-base64-encoded to avoid (security) bugs. Every time a PR
workflow runs or the `master` receives new commits, a CI action pushes
updated codecov report there.

## Repository setup

The configuration has to be done by someone who has permission to edit
[the repo
settings](https://github.com/FuelLabs/fuel-core/settings/pages): set
deployment from `codecov-pages` branch, like this:
<img width="510" alt="Screenshot of the settings page"
src="https://github.com/FuelLabs/fuel-vm/assets/2204863/e22d5c43-825d-4fd2-b5e1-0a2c3e826ce8">

---------

Co-authored-by: Mitchell Turner <james.mitchell.turner@gmail.com>
  • Loading branch information
Dentosal and MitchTurner committed Jun 17, 2024
1 parent 8b1bf02 commit 12cb17f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
50 changes: 47 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ concurrency:
cancel-in-progress: true

env:
GIT_BRANCH: ${{ github.head_ref || github.ref_name }}
CARGO_TERM_COLOR: always
RUST_VERSION: 1.75.0
NIGHTLY_RUST_VERSION: nightly-2023-10-29
RUST_VERSION_FMT: nightly-2023-10-29
RUST_VERSION_COV: nightly-2024-06-05
RUSTFLAGS: -D warnings
REGISTRY: ghcr.io
SEGMENT_DOWNLOAD_TIMEOUT_MINS: 2
Expand All @@ -39,11 +41,11 @@ jobs:
- name: Install latest nightly
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.NIGHTLY_RUST_VERSION }}
toolchain: ${{ env.RUST_VERSION_FMT }}
components: rustfmt

- name: Rustfmt check
run: cargo +${{ env.NIGHTLY_RUST_VERSION }} fmt --all -- --check
run: cargo +${{ env.RUST_VERSION_FMT }} fmt --all -- --check

lint-toml-files:
runs-on: buildjet-4vcpu-ubuntu-2204
Expand Down Expand Up @@ -193,6 +195,48 @@ jobs:
check-repo: false
ignore-unpublished-changes: true

publish-codecov:
name: Publish code coverage report on GitHub pages branch
runs-on: buildjet-4vcpu-ubuntu-2204
needs:
- cargo-verifications
permissions: # Write access to push changes to pages
contents: write
steps:
- uses: actions/checkout@v3
- name: Install latest Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_VERSION_COV }}
targets: wasm32-unknown-unknown

- name: Install cargo-llvm-codecov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Code coverage report
run: cargo +${{ env.RUST_VERSION_COV }} llvm-cov --all-features --html --branch

- name: Checkout the repo again for pushing pages revision
uses: actions/checkout@v3
with:
ref: 'codecov-pages'
path: 'pages-branch'

- name: Push codecov report to pages branch
working-directory: ./pages-branch
run: |
export BRANCH_B64=$(echo -n "${{ env.GIT_BRANCH }}" | basenc --base64url)
git config user.email "2204863+Dentosal@users.noreply.github.com"
git config user.name "Dentosal"
cp -r ../target/llvm-cov/html "$BRANCH_B64"
python3 ../.github/workflows/scripts/generate_pages_index.py > index.html
git add .
git commit -m "Update codecov for ${{ env.GIT_BRANCH }}"
git push
export PAGES_URL="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/$BRANCH_B64/index.html"
echo "$PAGES_URL"
echo "Codecov report $PAGES_URL" >> $GITHUB_STEP_SUMMARY
verifications-complete:
needs:
- cargo-verifications
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/scripts/generate_pages_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!python3
# Generate index.html for code coverage pages

from glob import iglob
from base64 import urlsafe_b64encode, urlsafe_b64decode

master_path = urlsafe_b64encode(b"master").decode("utf-8")

print("<html><head><title>Code Coverage</title>")
print("<style>body { font-size: 1.2em }</style>")
print(f"</head><body><h1>Code coverage</h1>")
print(f"<h2>Per branch (<a href=\"{master_path}/index.html\">master</a>)</h2><ul>")
for path in sorted(list(iglob("*/index.html", recursive=True))):
name = urlsafe_b64decode(path.split("/")[0].encode()).decode()
style = "font-weight: bold" if name == "master" else ""
print(f'<li style="{style}"><a href="{path}">{name}</a></li>')
print("</ul></body></html>")
5 changes: 4 additions & 1 deletion tests/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use fuel_core_types::{
fuel_crypto::SecretKey,
fuel_tx::Input,
};
use itertools::Itertools;
use rand::{
rngs::StdRng,
SeedableRng,
Expand Down Expand Up @@ -134,6 +133,7 @@ async fn test_partitions_larger_groups(
num_validators: usize,
num_partitions: usize,
) {
use itertools::Itertools;
use std::collections::VecDeque;

// Create a random seed based on the test parameters.
Expand Down Expand Up @@ -218,7 +218,10 @@ async fn test_partitions_larger_groups(
}

#[tokio::test(flavor = "multi_thread")]
#[cfg(not(coverage))] // This test is too slow for coverage
async fn test_multiple_producers_different_keys() {
use itertools::Itertools;

// Create a random seed based on the test parameters.
let mut hasher = DefaultHasher::new();
let num_txs = 10;
Expand Down
10 changes: 5 additions & 5 deletions tests/tests/tx_gossip.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::{
make_nodes,
BootstrapSetup,
Expand All @@ -16,10 +18,7 @@ use fuel_core_types::{
*,
},
fuel_vm::*,
services::{
block_importer::SharedImportResult,
executor::TransactionExecutionResult,
},
services::block_importer::SharedImportResult,
};
use futures::StreamExt;
use rand::{
Expand Down Expand Up @@ -206,6 +205,7 @@ async fn test_tx_gossiping_invalid_txs(
}

#[tokio::test(flavor = "multi_thread")]
#[cfg(not(coverage))]
async fn test_tx_gossiping_reserved_nodes_invalid_txs() {
// Test verifies that gossiping of invalid transactions from reserved
// nodes doesn't decrease its reputation.
Expand All @@ -218,7 +218,7 @@ async fn test_tx_gossiping_reserved_nodes_invalid_txs() {
assert_eq!(result.tx_status.len(), 2);
assert!(matches!(
result.tx_status[0].result,
TransactionExecutionResult::Success { .. }
fuel_core_types::services::executor::TransactionExecutionResult::Success { .. }
));
}

Expand Down

0 comments on commit 12cb17f

Please sign in to comment.