---
 .gitignore             |    1 +
 Makefile.am            |   21 +++++++++++++++++----
 autotools/gen-coverage |   44 ++++++++++++++++++++++++++++++++++++++++++++
 doc/devnotes.rst       |    3 +++
 4 files changed, 65 insertions(+), 4 deletions(-)
 create mode 100755 autotools/gen-coverage

diff --git a/.gitignore b/.gitignore
index 494481b..9aa21f8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,6 +41,7 @@
 # doc
 /doc/api
 /doc/build
+/doc/coverage
 /doc/html
 /doc/install-quick.rst
 /doc/news.rst
diff --git a/Makefile.am b/Makefile.am
index 3066d37..6262581 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -54,7 +54,7 @@ MAINTAINERCLEANFILES = \
        doc/news.rst
 
 maintainer-clean-local:
-       rm -rf doc/api doc/html
+       rm -rf doc/api doc/html doc/coverage
 
 CLEANFILES = \
        autotools/replace_vars.sed \
@@ -245,6 +245,7 @@ EXTRA_DIST = \
        autotools/check-python-code \
        autotools/check-man \
        autotools/docbook-wrapper \
+       autotools/gen-coverage \
        $(RUN_IN_TEMPDIR) \
        daemons/daemon-util.in \
        daemons/ganeti-cleaner.in \
@@ -340,9 +341,12 @@ nodist_TESTS =
 
 TESTS = $(dist_TESTS) $(nodist_TESTS)
 
-TESTS_ENVIRONMENT = \
-       PYTHONPATH=. TOP_SRCDIR=$(abs_top_srcdir) \
-       $(RUN_IN_TEMPDIR) $(PYTHON)
+# Environment for all tests
+PLAIN_TESTS_ENVIRONMENT = \
+       PYTHONPATH=. TOP_SRCDIR=$(abs_top_srcdir) PYTHON=$(PYTHON) 
$(RUN_IN_TEMPDIR)
+
+# Environment for tests run by automake
+TESTS_ENVIRONMENT = $(PLAIN_TESTS_ENVIRONMENT) $(PYTHON)
 
 all_python_code = \
        $(dist_sbin_SCRIPTS) \
@@ -535,4 +539,13 @@ TAGS: $(BUILT_SOURCES)
          -path 'daemons/ganeti-*' -o -path 'tools/*' | \
          etags -
 
+.PHONY: coverage
+coverage: $(BUILT_SOURCES) $(TESTS)
+       set -e; \
+       mkdir -p doc/coverage; \
+       COVERAGE_FILE=$(CURDIR)/doc/coverage/data \
+       TEXT_COVERAGE=$(CURDIR)/doc/coverage/report.txt \
+       HTML_COVERAGE=$(CURDIR)/doc/coverage \
+       $(PLAIN_TESTS_ENVIRONMENT) $(abs_top_srcdir)/autotools/gen-coverage 
$(TESTS)
+
 # vim: set noet :
diff --git a/autotools/gen-coverage b/autotools/gen-coverage
new file mode 100755
index 0000000..f8f3865
--- /dev/null
+++ b/autotools/gen-coverage
@@ -0,0 +1,44 @@
+#!/bin/bash
+#
+
+# Copyright (C) 2010 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+set -e
+
+: ${COVERAGE:=coverage}
+: ${PYTHON:=python}
+: ${COVERAGE_FILE:?}
+: ${TEXT_COVERAGE:?}
+
+omit=$($PYTHON -c 'import sys;
+print " ".join(["--omit=%s/" % i for i in
+                set([sys.prefix, sys.exec_prefix])])')
+
+$COVERAGE erase
+
+for script; do
+  $COVERAGE run --branch --append $script
+done
+
+echo "Writing text report to $TEXT_COVERAGE ..." >&2
+$COVERAGE report $omit | tee "$TEXT_COVERAGE"
+
+if [[ -n "$HTML_COVERAGE" ]]; then
+  echo "Generating HTML report in $HTML_COVERAGE ..." >&2
+  $COVERAGE html $omit -d "$HTML_COVERAGE"
+fi
diff --git a/doc/devnotes.rst b/doc/devnotes.rst
index e794469..1f50309 100644
--- a/doc/devnotes.rst
+++ b/doc/devnotes.rst
@@ -15,6 +15,9 @@ Most dependencies from :doc:`install-quick`, plus:
   (tested with version 0.6.1)
 - `graphviz <http://www.graphviz.org/>`_
 
+To generate unittest coverage reports (``make coverage``), `coverage
+<http://pypi.python.org/pypi/coverage>`_ needs to be installed.
+
 
 Configuring for development
 ---------------------------
-- 
1.6.6

Reply via email to