Skip to content

Commit

Permalink
Add coverage and coverage_all targets
Browse files Browse the repository at this point in the history
Create a test coverage report in HTML.
  • Loading branch information
Thomasdezeeuw committed Sep 21, 2024
1 parent 88f1518 commit 4a9a206
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ $(TARGETS): $(CRATES)
$(CRATES):
@$(MAKE) -C $@ $(patsubst %_all,%,$(MAKECMDGOALS))

coverage_all:
$(MAKE) coverage test=test_all

lint_all: clippy_all

doc_all:
Expand Down
42 changes: 41 additions & 1 deletion Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ TEST_OPTS = -- --quiet -Z unstable-options --shuffle
TARGETS ?= x86_64-unknown-linux-gnu
# Command to run in `dev` target, e.g. `make RUN=check dev`.
RUN ?= test
# The location of LLVM tools, rustup doesn't make this easy on us.
RUSTUP_TARGET=$(strip $(shell rustup show | head -n 1 | sed -e 's/^Default host: \(.*\)/\1/'))
LLVM_TOOLS_BIN=$(shell rustc --print sysroot)/lib/rustlib/$(RUSTUP_TARGET)/bin
TARGET_DIR=$(shell cargo metadata --format-version 1 | jq --raw-output '.target_directory')

# Development loop, runs $RUN whenever a source file changes.
dev:
Expand All @@ -35,6 +39,42 @@ test_sanitizer:
test_miri:
cargo miri test --all-features $(TEST_FLAGS)

# Generate coverage report.
#
# Install tools using:
# $ rustup component add llvm-tools
coverage: export RUSTFLAGS=-C instrument-coverage
coverage: export LLVM_PROFILE_FILE=$(TARGET_DIR)/coverage/data-%p-%9m.profraw
coverage: test?=test
coverage:
rm -rf '$(TARGET_DIR)/coverage/' &> /dev/null || true
mkdir -p '$(TARGET_DIR)/coverage/'
$(MAKE) $(test)
$(LLVM_TOOLS_BIN)/llvm-profdata merge --sparse '$(TARGET_DIR)/coverage/'*.profraw -o '$(TARGET_DIR)/coverage/merged.profdata'
$(LLVM_TOOLS_BIN)/llvm-cov show \
--instr-profile='$(TARGET_DIR)/coverage/merged.profdata' \
--show-branches=count \
--show-instantiations=false \
--show-regions \
--show-line-counts-or-regions \
--ignore-filename-regex='/rustc/' \
--ignore-filename-regex='/.cargo/' \
--ignore-filename-regex='/tests/' \
--ignore-filename-regex='tests.rs' \
--format=html \
--output-dir='$(TARGET_DIR)/coverage' \
$$( \
for file in \
$$( \
$(MAKE) $(test) --silent 'TEST_FLAGS=--quiet --no-run --message-format=json' \
| jq --raw-output 'select(.executable != null) | .filenames[]' \
| grep 'target' \
); do \
printf -- '--object %s ' $$file; \
done \
)
@echo 'See $(TARGET_DIR)/coverage/index.html'

check:
cargo check --all-features --all-targets

Expand Down Expand Up @@ -98,4 +138,4 @@ doc_private:
clean:
cargo clean

.PHONY: dev test test_sanitizers test_sanitizer test_miri check check_all_targets $(TARGETS) lint clippy doc doc_private clean
.PHONY: dev test test_sanitizers test_sanitizer test_miri coverage check check_all_targets $(TARGETS) lint clippy doc doc_private clean

0 comments on commit 4a9a206

Please sign in to comment.