From aa440cc319e8226e39e636726eb97506c70242cb Mon Sep 17 00:00:00 2001
From: Gorka Eguileor <geguileo@redhat.com>
Date: Thu, 19 Feb 2015 17:25:58 +0100
Subject: [PATCH] Add coverage report to run_test.sh

Adds argument -c to run_test.sh to generate HTML coverage reports like
Glance has.

It also displays coverage stats on terminal.

Change-Id: I3439ac43964a59e993253d1ad45ef74af86e2ad9
---
 .gitignore   |  1 +
 run_tests.sh | 24 ++++++++++++++++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index ad4e9b81..04faeaa2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,3 +22,4 @@ var/*
 ChangeLog
 AUTHORS
 subunit.log
+covhtml/
diff --git a/run_tests.sh b/run_tests.sh
index 6506df0a..b77b23ba 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -13,6 +13,7 @@ function usage {
   echo "  -u, --update                Update the virtual environment with any newer package versions"
   echo "  -p, --pep8                  Just run PEP8 and HACKING compliance check"
   echo "  -P, --no-pep8               Don't run static code checks"
+  echo "  -c, --coverage              Generate coverage report"
   echo "  -d, --debug                 Run tests with testtools instead of testr. This allows you to use the debugger."
   echo "  -h, --help                  Print this usage message"
   echo "  --virtual-env-path <path>   Location of the virtualenv directory"
@@ -42,6 +43,7 @@ function process_options {
       -u|--update) update=1;;
       -p|--pep8) just_pep8=1;;
       -P|--no-pep8) no_pep8=1;;
+      -c|--coverage) coverage=1;;
       -d|--debug) debug=1;;
       --virtual-env-path)
         (( i++ ))
@@ -80,6 +82,7 @@ testropts=
 wrapper=""
 just_pep8=0
 no_pep8=0
+coverage=0
 debug=0
 update=0
 concurrency=0
@@ -112,13 +115,17 @@ function run_tests {
     fi
     ${wrapper} python -m testtools.run $testropts $testrargs
 
-    # Short circuit because all of the testr stuff
+    # Short circuit because all of the testr and coverage stuff
     # below does not make sense when running testtools.run for
     # debugging purposes.
     return $?
   fi
 
-  TESTRTESTS="$TESTRTESTS"
+  if [ $coverage -eq 1 ]; then
+    TESTRTESTS="$TESTRTESTS --coverage"
+  else
+    TESTRTESTS="$TESTRTESTS"
+  fi
 
   # Just run the test suites in current environment
   set +e
@@ -142,6 +149,14 @@ function run_tests {
 
   copy_subunit_log
 
+  if [ $coverage -eq 1 ]; then
+    echo "Generating HTML coverage report in covhtml/"
+    # Don't compute coverage for common code, which is tested elsewhere
+    ${wrapper} coverage combine
+    ${wrapper} coverage html --include='glance_store/*' --omit='glance_store/openstack/common/*' -d covhtml -i
+    ${wrapper} coverage report --include='glance_store/*' --omit='glance_store/openstack/common/*' -i
+  fi
+
   return $RESULT
 }
 
@@ -196,6 +211,11 @@ then
   fi
 fi
 
+# Delete old coverage data from previous runs
+if [ $coverage -eq 1 ]; then
+    ${wrapper} coverage erase
+fi
+
 if [ $just_pep8 -eq 1 ]; then
     run_pep8
     exit