Crates.io | cosmoline |
lib.rs | cosmoline |
version | 0.1.1 |
source | src |
created_at | 2021-07-08 19:55:28.800638 |
updated_at | 2021-07-08 19:55:28.800638 |
description | Command line tool to generate HTML code coverage reports for rust projects |
homepage | |
repository | https://github.com/inferiorhumanorgans/cosmoline |
max_upload_size | |
id | 420455 |
size | 88,433 |
Cosmoline is a quick and dirty code coverage report generator for rust. It takes advantage of the source-based code coverage that landed in nightly back in November 2020.
Input: JSON from llvm-cov export
(or its convenient wrapper cargo cov
)
Output: Pretty HTML reports are rendered with handlebars-rs
. The templates are located in the template directory and compiled into the cosmoline
binary.
Instructions on how to configure nightly to build profiling data in the Rust Unstable Book.
Install jq
(useful, but not strictly necessary):
On e.g. macOS:
brew install jq
Or Debian:
sudo apt-get install jq
Or FreeBSD:
sudo pkg install jq
cosmoline
# Check out this repo
git clone https://github.com/inferiorhumanorgans/cosmoline
# Install it with cargo
cargo +nightly install --path ./cosmoline
For a single library from llvm
using jq
:
# Back in the directory for our own project
cd /path/to/my-app
export OUT_DIR="$(PWD)/coverage-report"
export APP_NAME="my-app"
# Run tests, save results as JUnit data
LLVM_PROFILE_FILE="${OUT_DIR}/${APP_NAME}-%m.profraw" RUSTFLAGS="-Z instrument-coverage" cargo +nightly test --lib -- -Z unstable-options --format=junit > ${OUT_DIR}/junit.xml
# Grab test executable
COV_EXEC=$(RUSTFLAGS="-Z instrument-coverage" cargo +nightly test --message-format=json --lib --no-run | jq -r "select(.profile.test == true) | .filenames[]" | head -1)
# Merge sparse files
cargo +nightly profdata -- merge --sparse "${OUT_DIR}/${APP_NAME}-"*.profraw -o "${OUT_DIR}/${APP_NAME}.profdata"
# Extract JSON formatted details
cargo +nightly cov -- export "${COV_EXEC}" -instr-profile="${OUT_DIR}/${APP_NAME}.profdata" > "${OUT_DIR}/${APP_NAME}.coverage.json"
cosmoline
cosmoline --input "${OUT_DIR}/${APP_NAME}.coverage.json" --source-directory "$(PWD)" --output-directory "${OUT_DIR}/report"
The resulting report is self-contained and will be placed in ${OUT_DIR}/report/index.html
.
A typical report might look like this:
Note that the percentages listed will be colored red, yellow, or green depending on the proportion of the file that's been covered.
Clicking on a filename will take you to an annotated rendering of that file's contents:
Code that's been instrumented is highlighted in red if it was not executed and green if the code's been executed. Code that has not been instrumented remains white.