Skip to content

Commit 8ae6f02

Browse files
committed
ci: Add make coverage command to generate a test coverage report
This outputs a default report, but also keeps the data files. They can be found under `target/coverage`. The generated test binaries are required for generating a report or showing the coverage.
1 parent 56db1ce commit 8ae6f02

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Makefile

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ package:
130130
--set-metadata='epoch=20' \
131131
--set-metadata='license="BeeGFS EULA"' \
132132
--set-metadata='provides={"beegfs-mgmtd" = "= $(VERSION_TRIMMED)"}'
133-
133+
134134
# Replace tilde in package filename with hypens.
135135
# Github release action and api substitutes tilde (~) with dot (.) in file names when uploaded to Github packages.
136136
find $(PACKAGE_DIR)/ -type f \( -name "*~*.deb" -o -name "*~*.rpm" \) -exec bash -c 'mv "$$1" "$${1//\~/-}"' _ {} \;
@@ -142,6 +142,33 @@ clean-package:
142142

143143
### Utilities ###
144144

145+
COVERAGE_DIR=$(shell echo $$(pwd)/target/coverage)
146+
147+
# Generates test coverage of the code excluding doctests and prints a report
148+
.PHONY: coverage
149+
coverage:
150+
# Ensure the required llvm-tools are installed - these must be exactly the ones required.
151+
# See https://doc.rust-lang.org/rustc/instrument-coverage.html
152+
rustup component add llvm-tools
153+
# Figure out the path to the installed llvm binaries for the local machine
154+
LLVM=$$(rustc --print sysroot)/lib/rustlib/$$(rustc -Vv | grep host | awk '{print $$2}')/bin
155+
156+
rm -rf $(COVERAGE_DIR)/*
157+
export RUSTFLAGS="-C instrument-coverage"
158+
export LLVM_PROFILE_FILE="$(COVERAGE_DIR)/%m_%p.profraw"
159+
160+
# Run tests with instrumentation. Then print a summary of the runs to extract the test binaries.
161+
cargo test $(LOCKED_FLAG) --all-features --tests
162+
TEST_BINARIES="$$( \
163+
for f in $$(cargo test $(LOCKED_FLAG) --all-features --tests --no-run 2>&1 | grep -E -o "target\/.*[^)]"); \
164+
do \
165+
printf "%s %s " -object $$f; \
166+
done \
167+
)"
168+
$$LLVM/llvm-profdata merge -sparse $(COVERAGE_DIR)/*.profraw -o $(COVERAGE_DIR)/merged.profdata
169+
git ls-files "*.rs" | xargs $$LLVM/llvm-cov report $$TEST_BINARIES -instr-profile=$(COVERAGE_DIR)/merged.profdata -sources
170+
171+
145172
# Quickly installs the newest versions most of the tools and components used by this repo. Requires
146173
# `rustup` to be installed. You can install that by either going to `https://rustup.rs` and use the
147174
# script there or install the version from your distros package manager if available.

0 commit comments

Comments
 (0)