From 77b81adb71f2409fb48a1b7c6efbc6cb5fc17827 Mon Sep 17 00:00:00 2001
From: Thomas Hindoe Paaboel Andersen <phomes@gmail.com>
Date: Thu, 28 Mar 2013 14:47:46 +0100
Subject: [PATCH] Add test coverage and generate report

Add support for creating a test coverage report using lcov.
Enable the coverage with --enable-coverage.
"make lcov" will create the report locally.
"make lcov-upload" is intended to push the report to a server.
It currently just rsyncs to ~/coverage.

Requires lcov version 1.10 to handle naming in systemd and to
use the --no-external option.
---
 Makefile.am  | 42 ++++++++++++++++++++++++++++++++++++++++++
 configure.ac | 24 ++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/Makefile.am b/Makefile.am
index 37c1cc2..f8bb6b6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -171,6 +171,11 @@ AM_CPPFLAGS = \
 AM_CFLAGS = $(OUR_CFLAGS)
 AM_LDFLAGS = $(OUR_LDFLAGS)
 
+if ENABLE_COVERAGE
+	AM_CFLAGS += \
+		$(COVERAGE_CFLAGS)
+endif
+
 # ------------------------------------------------------------------------------
 define move-to-rootlibdir
 	if test "$(libdir)" != "$(rootlibdir)"; then \
@@ -1297,6 +1302,43 @@ test_sched_prio_LDADD = \
 	libsystemd-daemon.la
 
 # ------------------------------------------------------------------------------
+## .PHONY so it always rebuilds it
+.PHONY: lcov-reset lcov lcov-run lcov-report lcov-upload
+
+# run lcov from scratch, always
+lcov-reset:
+	$(MAKE) lcov-run
+	$(MAKE) lcov-report
+
+# run lcov from scratch if the dir is not there
+lcov:
+	$(MAKE) lcov-reset
+
+if ENABLE_COVERAGE
+# reset run coverage tests
+lcov-run:
+	@-rm -rf lcov
+	lcov --directory . --zerocounters
+	-$(MAKE) check
+
+# generate report based on current coverage data
+lcov-report:
+	mkdir lcov
+	lcov --compat-libtool --base-directory . --directory . --no-external --capture --output-file lcov/lcov.info
+	genhtml -t "systemd test coverage" -o lcov lcov/lcov.info
+
+lcov-upload: lcov
+	rsync -rvz --delete lcov/* ~/coverage
+
+else
+lcov-run:
+	echo "Need to reconfigure with --enable-coverage"
+
+lcov-report:
+	echo "Need to reconfigure with --enable-coverage"
+endif
+
+# ------------------------------------------------------------------------------
 systemd_initctl_SOURCES = \
 	src/initctl/initctl.c
 
diff --git a/configure.ac b/configure.ac
index 5b88bcf..1f18ed9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -207,6 +207,29 @@ m4_pattern_forbid([^_?PKG_[A-Z_]+$],[*** pkg.m4 missing, please install pkg-conf
 PKG_CHECK_MODULES(DBUS, [dbus-1 >= 1.3.2])
 
 # ------------------------------------------------------------------------------
+have_coverage=no
+AC_ARG_ENABLE(coverage, AS_HELP_STRING([--enable-coverage], [enable test coverage]))
+if test "x$enable_coverage" = "xyes" ; then
+        AC_CHECK_PROG(lcov_found, [lcov], [yes], [no])
+        if test "x$lcov_found" = xno ; then
+                AC_MSG_ERROR([*** lcov support requested but the program was not found])
+        else
+                lcov_version_major=`lcov --version | cut -d ' ' -f 4 | cut -d '.' -f 1`
+                lcov_version_minor=`lcov --version | cut -d ' ' -f 4 | cut -d '.' -f 2`
+                if test $lcov_version_major -eq 1 -a $lcov_version_minor -lt 10; then
+                        AC_MSG_ERROR([*** lcov version is too old. 1.10 required])
+                else
+                        have_coverage=yes
+                        CC_CHECK_FLAGS_APPEND([with_coverage_cflags], [CFLAGS], [\
+                        -fprofile-arcs \
+                        -ftest-coverage])
+                        AC_SUBST([COVERAGE_CFLAGS], $with_coverage_cflags)
+                fi
+        fi
+fi
+AM_CONDITIONAL(ENABLE_COVERAGE, [test "$have_coverage" = "yes"])
+
+# ------------------------------------------------------------------------------
 have_kmod=no
 AC_ARG_ENABLE(kmod, AS_HELP_STRING([--disable-kmod], [disable loadable modules support]))
 if test "x$enable_kmod" != "xno"; then
@@ -935,6 +958,7 @@ AC_MSG_RESULT([
         Python Headers:          ${have_python_devel}
         man pages:               ${have_manpages}
         gtk-doc:                 ${enable_gtk_doc}
+        test coverage:           ${have_coverage}
         Split /usr:              ${enable_split_usr}
         SysV compatibility:      ${SYSTEM_SYSV_COMPAT}
 
-- 
1.8.1.4

