Skip to content

Commit f55c740

Browse files
authored
Merge pull request #1423 from nicholasbishop/bishop-add-cov
xtask: Add action to generate a code coverage report
2 parents 42df2f4 + 0efe8dc commit f55c740

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

.github/workflows/rust.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,18 @@ jobs:
189189
- uses: Swatinem/rust-cache@v2
190190
- name: Build
191191
run: cargo +nightly build --target x86_64-unknown-uefi --verbose -p uefi-std-example
192+
coverage:
193+
name: Test Coverage
194+
runs-on: ubuntu-latest
195+
steps:
196+
- name: Checkout sources
197+
uses: actions/checkout@v4
198+
- uses: Swatinem/rust-cache@v2
199+
- name: Install cargo-llvm-cov
200+
uses: taiki-e/install-action@cargo-llvm-cov
201+
- name: Generate code coverage
202+
run: cargo xtask cov --lcov
203+
- name: Upload code coverage
204+
uses: codecov/codecov-action@v4
205+
with:
206+
files: target/lcov

xtask/src/cargo.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl TargetTypes {
186186
pub enum CargoAction {
187187
Build,
188188
Clippy,
189+
Coverage {
190+
lcov: bool,
191+
open: bool,
192+
},
189193
Doc {
190194
open: bool,
191195
document_private_items: bool,
@@ -251,6 +255,17 @@ impl Cargo {
251255
tool_args.extend(["-D", "warnings"]);
252256
}
253257
}
258+
CargoAction::Coverage { lcov, open } => {
259+
action = "llvm-cov";
260+
if lcov {
261+
extra_args.extend(["--lcov", "--output-path", "target/lcov"]);
262+
} else {
263+
extra_args.push("--html");
264+
}
265+
if open {
266+
extra_args.push("--open");
267+
}
268+
}
254269
CargoAction::Doc {
255270
open,
256271
document_private_items,

xtask/src/main.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ mod tpm;
1212
mod util;
1313

1414
use crate::opt::{FmtOpt, TestOpt};
15-
use anyhow::Result;
15+
use anyhow::{bail, Result};
1616
use arch::UefiArch;
1717
use cargo::{Cargo, CargoAction, Feature, Package, TargetTypes};
1818
use clap::Parser;
1919
use itertools::Itertools;
20-
use opt::{Action, BuildOpt, ClippyOpt, DocOpt, Opt, QemuOpt, TpmVersion};
20+
use opt::{Action, BuildOpt, ClippyOpt, CovOpt, DocOpt, Opt, QemuOpt, TpmVersion};
2121
use std::process::Command;
2222
use util::run_cmd;
2323

@@ -85,6 +85,30 @@ fn clippy(opt: &ClippyOpt) -> Result<()> {
8585
run_cmd(cargo.command()?)
8686
}
8787

88+
/// Generate a code coverage report.
89+
fn code_coverage(opt: &CovOpt) -> Result<()> {
90+
if has_cmd("cargo-llvm-cov") {
91+
let cargo = Cargo {
92+
action: CargoAction::Coverage {
93+
lcov: opt.lcov,
94+
open: opt.open,
95+
},
96+
features: Feature::more_code(*opt.unstable, false),
97+
// Leave out uefi-macros; the compilation tests will just make
98+
// things slower without contributing anything to the coverage
99+
// report.
100+
packages: vec![Package::UefiRaw, Package::Uefi],
101+
release: false,
102+
target: None,
103+
warnings_as_errors: false,
104+
target_types: TargetTypes::Default,
105+
};
106+
run_cmd(cargo.command()?)
107+
} else {
108+
bail!("cargo-llvm-cov not found, see https://github.com/taiki-e/cargo-llvm-cov");
109+
}
110+
}
111+
88112
/// Build docs.
89113
fn doc(opt: &DocOpt) -> Result<()> {
90114
let cargo = Cargo {
@@ -305,6 +329,7 @@ fn main() -> Result<()> {
305329
Action::Build(build_opt) => build(build_opt),
306330
Action::CheckRaw(_) => check_raw::check_raw(),
307331
Action::Clippy(clippy_opt) => clippy(clippy_opt),
332+
Action::Cov(cov_opt) => code_coverage(cov_opt),
308333
Action::Doc(doc_opt) => doc(doc_opt),
309334
Action::GenCode(gen_opt) => device_path::gen_code(gen_opt),
310335
Action::Miri(_) => run_miri(),

xtask/src/opt.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub enum Action {
6868
Build(BuildOpt),
6969
CheckRaw(CheckRawOpt),
7070
Clippy(ClippyOpt),
71+
Cov(CovOpt),
7172
Doc(DocOpt),
7273
GenCode(GenCodeOpt),
7374
Miri(MiriOpt),
@@ -105,6 +106,21 @@ pub struct ClippyOpt {
105106
pub warning: WarningOpt,
106107
}
107108

109+
/// Generate a code coverage report.
110+
#[derive(Debug, Parser)]
111+
pub struct CovOpt {
112+
/// Open the output in a browser.
113+
#[clap(long, action)]
114+
pub open: bool,
115+
116+
/// Output raw lcov data instead of HTML.
117+
#[clap(long, action, conflicts_with = "open")]
118+
pub lcov: bool,
119+
120+
#[clap(flatten)]
121+
pub unstable: UnstableOpt,
122+
}
123+
108124
/// Build the docs for the uefi packages.
109125
#[derive(Debug, Parser)]
110126
pub struct DocOpt {

0 commit comments

Comments
 (0)