Rust coverage and Coverage Gutters

Here is the script I use to generate coverage for my Rust crate. It’s mostly adapted from the official documentation. It play well with the Coverge Gutters plugin in VSCode.

#! /bin/sh
# Enable coverage in rustc
export RUSTFLAGS="-C instrument-coverage"

# Collect object file names
files=$(cargo test --no-run --message-format=json | jq -r "select(.profile.test == true) | .filenames[]" | grep -v dSYM -)
objects_args=$(for file in $files; do printf "%s %s " -object $file; done)
common_args="--ignore-filename-regex=/.cargo/registry --instr-profile=coverage.profdata $objects_args"

# Run tests
cargo test
llvm-profdata merge -sparse default_*.profraw -o coverage.profdata

# Display summary to the console
llvm-cov report $common_args

# HTML output
llvm-cov show $common_args --show-instantiations --show-line-counts-or-regions -format=html > coverage.html

# Create a lcov file for Coverage Gutters
# https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters
llvm-cov export $common_args -format=lcov > lcov.info

#rust, #vscode