Setup test code coverage with lcov
authorDidier 'Ptitjes <ptitjes@free.fr>
Thu, 10 Sep 2009 14:31:29 +0000 (16:31 +0200)
committerDidier 'Ptitjes <ptitjes@free.fr>
Thu, 10 Sep 2009 14:42:25 +0000 (16:42 +0200)
Code coverage analysis is enabled with --enable-coverage.

.gitignore
Makefile.am
configure.ac
gee/Makefile.am
tests/Makefile.am

index 5c4ffb5..0c6ccee 100644 (file)
@@ -24,10 +24,12 @@ ylwrap
 compile
 stamp-h1
 *.pc
+lcov.info
 gee/gee-1.0.vapi
 gee/gee-internals-1.0.vapi
 benchmark/benchmarks
 tests/tests
+tests/coverage
 tests/testhashmap
 tests/testhashset
 tests/testtreemap
index 4b3717d..844b0ff 100644 (file)
@@ -20,6 +20,19 @@ SUBDIRS = \
        $(BENCHMARK_SUBDIR) \
        $(NULL)
 
+if ENABLE_COVERAGE
+coverage:
+       $(MAKE) -C tests coverage-reset
+       $(MAKE) -C tests test
+       $(MAKE) -C tests coverage-report
+else
+coverage:
+       @echo "The project has not been configured for coverage, check that you have gcov, lcov."
+       @echo "Of course, do not use --disable-coverage"
+       @exit 1
+endif
+.PHONY: coverage
+
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = gee-1.0.pc
 
index 95a30e7..965f2b7 100644 (file)
@@ -25,6 +25,25 @@ AS_IF([test "x$enable_doc" != xno],
         AS_IF([test "$VALADOC" = :],
                [AC_MSG_ERROR([valadoc not found])])])
 
+AC_ARG_ENABLE(coverage,
+       AS_HELP_STRING([--enable-coverage],
+       [compile with coverage profiling and debug (gcc only)]),
+       enable_coverage=$enableval,enable_coverage=no)
+AM_CONDITIONAL(ENABLE_COVERAGE, test x$enable_coverage != xno)
+
+AS_IF([test "x$enable_coverage" != xno],
+       [COVERAGE_CFLAGS="-fprofile-arcs -ftest-coverage"
+        COVERAGE_LIBS="-lgcov"
+        AC_SUBST(COVERAGE_CFLAGS)
+        AC_SUBST(COVERAGE_LIBS)
+        
+        #FIXME: preserve non -Wall/-g/-O flags from users CFLAGS
+        CFLAGS="-Wall -g -O0"
+        AC_PATH_PROG(LCOV, lcov, :)
+        AC_SUBST(LCOV)
+        AS_IF([test "$LCOV" = :],
+               [AC_MSG_ERROR([lcov is necessary for testing code coverage (http://ltp.sourceforge.net/coverage/lcov.php)])])])
+
 AC_ARG_ENABLE(benchmark, AS_HELP_STRING([--enable-benchmark], [Enable benchmark]), enable_benchmark=$enableval, enable_benchmark=no)
 AM_CONDITIONAL(ENABLE_BENCHMARK, test x$enable_benchmark = xyes)
 
index 1c323ec..6fa4869 100644 (file)
@@ -4,6 +4,7 @@ NULL =
 
 AM_CPPFLAGS = \
        $(GLIB_CFLAGS) \
+       $(COVERAGE_CFLAGS) \
        $(NULL)
 
 BUILT_SOURCES = gee.vala.stamp
@@ -65,6 +66,7 @@ gee-1.0.vapi gee.vala.stamp: $(libgee_la_VALASOURCES)
 
 libgee_la_LIBADD = \
        $(GLIB_LIBS) \
+       $(COVERAGE_LIBS) \
        $(NULL)
 
 libgee_la_LDFLAGS = -version-info 1:0:0
index 15f0088..4162ac5 100644 (file)
@@ -36,6 +36,33 @@ tests.vala.stamp: $(tests_VALASOURCES)
 tests_LDADD = $(progs_ldadd)
 EXTRA_DIST += $(tests_VALASOURCES)
 
+coverage-reset:
+       lcov --base-directory=@top_srcdir@ --directory @top_srcdir@/gee --zerocounters
+
+coverage-report:
+       lcov --directory @top_srcdir@/gee \
+               --capture \
+               --output-file @top_builddir@/lcov.info
+
+       lcov --directory @top_srcdir@/gee \
+               --output-file @top_builddir@/lcov.info \
+               --remove @top_builddir@/lcov.info \
+               /usr/include/\*
+
+       $(mkdir_p) @top_builddir@/tests/coverage
+       git_commit=`GIT_DIR=@top_srcdir@/.git git log -1 --pretty=format:%h 2>/dev/null`;\
+       genhtml --title "@PACKAGE_STRING@ $$git_commit" \
+               --output-directory @top_builddir@/tests/coverage @top_builddir@/lcov.info
+       @echo
+       @echo 'lcov report can be found in:'
+       @echo 'file://@abs_top_builddir@/tests/coverage/index.html'
+       @echo
+
+clean-local:
+       -rm -rf coverage
+
+.PHONY: coverage-report
+
 TEST_PROGS += testhashmap
 testhashmap_VALASOURCES = testhashmap.vala
 testhashmap_SOURCES = testhashmap.c