Skip to content

Commit

Permalink
feat: add code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
devwckd committed Jan 31, 2025
1 parent 7973988 commit 2f5055b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ concurrency:

jobs:
check:
permissions:
pull-requests: write
id-token: write
pages: write
checks: write
contents: write

runs-on: ubuntu-24.04-8core-bakunin

steps:
Expand Down Expand Up @@ -49,3 +56,52 @@ jobs:
RUST_BACKTRACE: 1
SKIP_WASM_BUILD: 1
run: cargo test

- uses: jwalton/gh-find-current-pr@v1
id: findPr

- name: Install cargo-llvm-cov
if: success() && steps.findPr.outputs.number
uses: taiki-e/install-action@cargo-llvm-cov

- name: Install cargo-xtask
if: success() && steps.findPr.outputs.number
run: cargo install cargo-xtask

- name: Generate code coverage lcov
if: success() && steps.findPr.outputs.number
run: cargo xtask coverage

- name: Generate Coverage Summary Report
if: success() && steps.findPr.outputs.number
uses: irongut/[email protected]
with:
filename: target/cov.xml
badge: true
format: markdown
hide_branch_rate: false
# hide_complexity: true
indicators: true
output: both

- name: Add Coverage PR Report Comment
if: success() && steps.findPr.outputs.number
uses: marocchino/sticky-pull-request-comment@v2
with:
header: report
number: ${{ steps.finder.outputs.pr }}
recreate: true
path: code-coverage-results.md

- name: Add Coverage PR Detailed Link
if: success() && steps.findPr.outputs.number
uses: marocchino/sticky-pull-request-comment@v2
with:
header: detailed
number: ${{ steps.finder.outputs.pr }}
recreate: true
message: |
[Detailed coverage report]({{ }})
17 changes: 13 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// Editor
"editor.formatOnSave": true,
// Rust
"rust-analyzer.cargo.extraEnv": { "SKIP_WASM_BUILD": "1" },
"rust-analyzer.check.extraEnv": { "SKIP_WASM_BUILD": "1" },
"rust-analyzer.cargo.features": ["runtime-benchmarks"]
}
"rust-analyzer.cargo.extraEnv": {
"SKIP_WASM_BUILD": "1"
},
"rust-analyzer.check.extraEnv": {
"SKIP_WASM_BUILD": "1"
},
"rust-analyzer.cargo.features": [
"runtime-benchmarks"
],
"coverage-gutters.coverageFileNames": [
"target/cov.xml",
],
}
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
pkgs.act
# Python
pkgs.python310
# Code coverage tool
pkgs.cargo-llvm-cov
];
in
{
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
# but we use the oxalica overlay, which does not fix this yet.
# Should be fixed by https://github.com/rust-lang/rust/issues/123151.
channel = "1.82.0"
components = ["clippy", "rustfmt", "rust-src", "rust-analyzer"]
components = ["clippy", "rustfmt", "rust-src", "rust-analyzer", "llvm-tools-preview"]
targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
profile = "minimal"
10 changes: 10 additions & 0 deletions xtask/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ xflags::xflags! {

optional --sudo sudo_key: String
}

cmd coverage {
optional --html
}
}
}

Expand All @@ -76,6 +80,7 @@ pub struct Xtask {
pub enum XtaskCmd {
Run(Run),
GenerateSpec(GenerateSpec),
Coverage(Coverage),
}

#[derive(Debug)]
Expand Down Expand Up @@ -114,6 +119,11 @@ pub struct GenerateSpec {
pub sudo: Option<String>,
}

#[derive(Debug)]
pub struct Coverage {
pub html: bool,
}

impl Xtask {
#[allow(dead_code)]
pub fn from_env_or_exit() -> Self {
Expand Down
23 changes: 22 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, net::IpAddr, path::Path};
use std::{borrow::Cow, net::IpAddr, os::unix::process::CommandExt, path::Path};

use polkadot_sdk::sp_keyring;

Expand Down Expand Up @@ -75,6 +75,27 @@ fn main() {

std::fs::write(cmd.out, out).expect("failed to write resulting ");
}
flags::XtaskCmd::Coverage(coverage) => {
const PALLETS: [&str; 3] = ["pallet-emission0", "pallet-governance", "pallet-torus0"];

let mut cmd = std::process::Command::new("cargo");
let mut args = vec!["llvm-cov", "--all-features"];

for pallet in PALLETS {
args.extend_from_slice(&["-p", pallet]);
}

if coverage.html {
let dev_args = ["--html"];
args.extend_from_slice(&dev_args);
} else {
let ci_args = ["--cobertura", "--output-path", "target/cov.xml"];
args.extend_from_slice(&ci_args);
}

cmd.args(args);
cmd.exec();
}
}
}

Expand Down

0 comments on commit 2f5055b

Please sign in to comment.