Skip to content

Commit

Permalink
xtask: Add action to generate a code coverage report
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicholasbishop committed Oct 8, 2024
1 parent 19da496 commit 23b6d65
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions xtask/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ impl TargetTypes {
pub enum CargoAction {
Build,
Clippy,
Coverage {
open: bool,
},
Doc {
open: bool,
document_private_items: bool,
Expand Down Expand Up @@ -251,6 +254,13 @@ impl Cargo {
tool_args.extend(["-D", "warnings"]);
}
}
CargoAction::Coverage { open } => {
action = "llvm-cov";
extra_args.push("--html");
if open {
extra_args.push("--open");
}
}
CargoAction::Doc {
open,
document_private_items,
Expand Down
26 changes: 24 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ mod tpm;
mod util;

use crate::opt::{FmtOpt, TestOpt};
use anyhow::Result;
use anyhow::{bail, Result};
use arch::UefiArch;
use cargo::{Cargo, CargoAction, Feature, Package, TargetTypes};
use clap::Parser;
use itertools::Itertools;
use opt::{Action, BuildOpt, ClippyOpt, DocOpt, Opt, QemuOpt, TpmVersion};
use opt::{Action, BuildOpt, ClippyOpt, CovOpt, DocOpt, Opt, QemuOpt, TpmVersion};
use std::process::Command;
use util::run_cmd;

Expand Down Expand Up @@ -85,6 +85,27 @@ fn clippy(opt: &ClippyOpt) -> Result<()> {
run_cmd(cargo.command()?)
}

/// Generate a code coverage report.
fn code_coverage(opt: &CovOpt) -> Result<()> {
if has_cmd("cargo-llvm-cov") {
let cargo = Cargo {
action: CargoAction::Coverage { open: opt.open },
features: Feature::more_code(*opt.unstable, false),
// Leave out uefi-macros; the compilation tests will just make
// things slower without contributing anything to the coverage
// report.
packages: vec![Package::UefiRaw, Package::Uefi],
release: false,
target: None,
warnings_as_errors: false,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)
} else {
bail!("cargo-llvm-cov not found, see https://github.com/taiki-e/cargo-llvm-cov");
}
}

/// Build docs.
fn doc(opt: &DocOpt) -> Result<()> {
let cargo = Cargo {
Expand Down Expand Up @@ -305,6 +326,7 @@ fn main() -> Result<()> {
Action::Build(build_opt) => build(build_opt),
Action::CheckRaw(_) => check_raw::check_raw(),
Action::Clippy(clippy_opt) => clippy(clippy_opt),
Action::Cov(cov_opt) => code_coverage(cov_opt),
Action::Doc(doc_opt) => doc(doc_opt),
Action::GenCode(gen_opt) => device_path::gen_code(gen_opt),
Action::Miri(_) => run_miri(),
Expand Down
12 changes: 12 additions & 0 deletions xtask/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub enum Action {
Build(BuildOpt),
CheckRaw(CheckRawOpt),
Clippy(ClippyOpt),
Cov(CovOpt),
Doc(DocOpt),
GenCode(GenCodeOpt),
Miri(MiriOpt),
Expand Down Expand Up @@ -105,6 +106,17 @@ pub struct ClippyOpt {
pub warning: WarningOpt,
}

/// Generate a code coverage report.
#[derive(Debug, Parser)]
pub struct CovOpt {
/// Open the output in a browser.
#[clap(long, action)]
pub open: bool,

#[clap(flatten)]
pub unstable: UnstableOpt,
}

/// Build the docs for the uefi packages.
#[derive(Debug, Parser)]
pub struct DocOpt {
Expand Down

0 comments on commit 23b6d65

Please sign in to comment.