Skip to content

Commit 23b6d65

Browse files
xtask: Add action to generate a code coverage report
This uses llvm-cov (https://github.com/taiki-e/cargo-llvm-cov) to create a simple HTML code coverage report. For now this only includes coverage from host tests, but in the future we can use https://github.com/Amanieu/minicov to include coverage from VM tests as well.
1 parent 19da496 commit 23b6d65

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

xtask/src/cargo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ impl TargetTypes {
186186
pub enum CargoAction {
187187
Build,
188188
Clippy,
189+
Coverage {
190+
open: bool,
191+
},
189192
Doc {
190193
open: bool,
191194
document_private_items: bool,
@@ -251,6 +254,13 @@ impl Cargo {
251254
tool_args.extend(["-D", "warnings"]);
252255
}
253256
}
257+
CargoAction::Coverage { open } => {
258+
action = "llvm-cov";
259+
extra_args.push("--html");
260+
if open {
261+
extra_args.push("--open");
262+
}
263+
}
254264
CargoAction::Doc {
255265
open,
256266
document_private_items,

xtask/src/main.rs

Lines changed: 24 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,27 @@ 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 { open: opt.open },
93+
features: Feature::more_code(*opt.unstable, false),
94+
// Leave out uefi-macros; the compilation tests will just make
95+
// things slower without contributing anything to the coverage
96+
// report.
97+
packages: vec![Package::UefiRaw, Package::Uefi],
98+
release: false,
99+
target: None,
100+
warnings_as_errors: false,
101+
target_types: TargetTypes::Default,
102+
};
103+
run_cmd(cargo.command()?)
104+
} else {
105+
bail!("cargo-llvm-cov not found, see https://github.com/taiki-e/cargo-llvm-cov");
106+
}
107+
}
108+
88109
/// Build docs.
89110
fn doc(opt: &DocOpt) -> Result<()> {
90111
let cargo = Cargo {
@@ -305,6 +326,7 @@ fn main() -> Result<()> {
305326
Action::Build(build_opt) => build(build_opt),
306327
Action::CheckRaw(_) => check_raw::check_raw(),
307328
Action::Clippy(clippy_opt) => clippy(clippy_opt),
329+
Action::Cov(cov_opt) => code_coverage(cov_opt),
308330
Action::Doc(doc_opt) => doc(doc_opt),
309331
Action::GenCode(gen_opt) => device_path::gen_code(gen_opt),
310332
Action::Miri(_) => run_miri(),

xtask/src/opt.rs

Lines changed: 12 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,17 @@ 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+
#[clap(flatten)]
117+
pub unstable: UnstableOpt,
118+
}
119+
108120
/// Build the docs for the uefi packages.
109121
#[derive(Debug, Parser)]
110122
pub struct DocOpt {

0 commit comments

Comments
 (0)