unit: support text & HTML targets; improve coverage rules

The main change is switching `unit-coverage` to run
scripts/coverage_report.pl, to get nice coverage summary output on the
commandline. The previous behaviour moves to `unit-coverage-html`.

Calls to lcov and genhtml are now silencing more warnings, and the
output file now gets branch coverage as well.

This should be compatible with both lcov 1.x and 2.x. It takes advantage
of the fact that 1.x is far more forgiving of both options it doesn't
understand, and of various kinds of "inconsistency" in the input data.

The rest is both simplifying and improving the rules. We keep the
coverage output around now, but still rebuild it if the binary changes.
The `clean` target now removes the coverage output too. And we use the
target name more often for building path names, as its far less noisy.

Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@truenas.com>
Closes #18619
This commit is contained in:
Rob Norris
2026-06-03 15:12:42 +10:00
committed by Brian Behlendorf
parent 9f23793d53
commit 9a6dd0e1bc
+40 -14
View File
@@ -39,27 +39,50 @@ nodist_%C%_test_zap_SOURCES = \
# test run and coverage targets below
PHONY += unit unit-coverage
PHONY += unit unit-coverage unit-coverage-html
_unit_run_%: %D%/%
@echo " UNITTEST $<" ; $< $(TOPT)
_unit_coverage_%: %D%/%
@${LCOV} --quiet --zerocounters --directory $(top_srcdir) >/dev/null
# note: any changes in switches to lcov or genhtml must be carefully checked
# on 1.x and 2.x; the current option set is carefully chosen to allow
# both to work sensibly
# .info is marked PRECIOUS, because its usually only created as an intermediate
# from one of the unit phony targets, but once it exists there's no point
# remaking it until and unless the test binary itself changes
.PRECIOUS: %D%/%.info
%D%/%.info: %D%/%
@-${RM} $@
@${LCOV} --quiet --quiet --zerocounters --directory $(top_srcdir)
@echo " UNITTEST $<" ; $< $(TOPT)
@${LCOV} --quiet --capture \
--test-name $(subst _unit_coverage_, , $@) \
@${LCOV} --quiet --quiet --capture \
--test-name $(notdir $<) \
--directory $(top_srcdir) \
--output-file \
%D%/$(join $(subst _unit_coverage_, , $@), .info) \
$(addprefix --include , $(call $(join \
$(subst _unit_coverage_, nodist_%C%_, $@), _SOURCES)))
@${GENHTML} --quiet \
%D%/$(join $(subst _unit_coverage_, , $@), .info) \
--output-directory \
%D%/$(join $(subst _unit_coverage_, , $@), _coverage)
--output-file $@ \
--rc lcov_branch_coverage=1 \
--rc geninfo_unexecuted_blocks=1 \
$(addprefix --include $(abs_top_builddir)/, $(call \
$(join $(join nodist_%C%_, $(notdir $<)), _SOURCES))) \
2>/dev/null
_unit_coverage_%: %D%/%.info
@scripts/coverage_report.pl $<
_unit_coverage_html_%: %D%/%.info
@-${RM} -r $(subst .info,_coverage, $<)
@${GENHTML} --quiet -quiet \
--rc lcov_branch_coverage=1 \
--rc check_data_consistency=0 \
--output-directory $(subst .info,_coverage, $<) \
$< \
2>/dev/null
@echo "coverage results:" \
"file://$(realpath %D%)/$(join $(subst _unit_coverage_, , $@), _coverage)/index.html"
"file://$(realpath %D%)/$(subst .info,_coverage,$(notdir $<))/index.html"
CLEAN_LOCAL += unit-clean-local
unit-clean-local:
-${RM} -r %D%/*.info %D%/*_coverage/
_UNIT_ALL_TARGETS = $(notdir $(UNIT_TESTS))
_UNIT_FIND_TARGET = \
@@ -76,9 +99,12 @@ unit: $(addprefix _unit_run_, $(_UNIT_TARGETS))
if CODE_COVERAGE_ENABLED
unit-coverage: $(addprefix _unit_coverage_, $(_UNIT_TARGETS))
@$(if $^, true, echo "ERROR: couldn't find unit test: $(T)" && false)
unit-coverage-html: $(addprefix _unit_coverage_html_, $(_UNIT_TARGETS))
@$(if $^, true, echo "ERROR: couldn't find unit test: $(T)" && false)
else
unit-coverage:
@echo "unit test coverage not enabled."
@echo "re-run configure with --enable-code-coverage"
@false
unit-coverage-html: unit-coverage
endif