Update run_tests.sh to support coverage report

User could use "run_tests.sh coverage" as an option to enable
coverage report.
Thing to note:
User needs to 'pip install coverage' to use coverage tool.

Test: Run script manually
Bug: none
Change-Id: Ibb0d8e706f527bbd64dbfeae2d035de427b88b08
diff --git a/.coveragerc b/.coveragerc
new file mode 100644
index 0000000..b730a35
--- /dev/null
+++ b/.coveragerc
@@ -0,0 +1,14 @@
+[run]
+branch = True
+
+# module names must be listed one per line.
+source =
+    acloud
+
+# omit file patterns must be listed one per line. */.local/* /usr/*
+omit =
+    *_test.py
+
+[html]
+directory = htmlcov
+
diff --git a/.gitignore b/.gitignore
index 415166b..000f4de 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
 *.pyc
 *.*~
+.coverage
+htmlcov/
diff --git a/run_tests.sh b/run_tests.sh
index e093e7a..5059618 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -10,19 +10,59 @@
     exit 1
 fi
 
-rc=0
-# Runs all unit tests under tools/acloud.
-for t in $(find $ACLOUD_DIR -type f -name "*_test.py");
-do
-    if ! PYTHONPATH=$ANDROID_BUILD_TOP/tools python $t; then
-      rc=1
-      echo -e "${RED}$t failed${NC}"
-    fi
-done
+function helper() {
+    echo "usage: $0 [options]"
+    echo "  options:"
+    echo "    coverage, test all unit tests with coverage report"
+}
 
-if [[ $rc -eq 0 ]]; then
-  echo -e "${GREEN}All unittests pass${NC}!"
-else
-  echo -e "${RED}There was a unittest failure${NC}"
-fi
-exit $rc
+function print_summary() {
+    local test_results=$1
+    local coverage_run=$2
+    if [[ $coverage_run == "coverage" ]]; then
+        PYTHONPATH=$ANDROID_BUILD_TOP/tools coverage report -m
+        PYTHONPATH=$ANDROID_BUILD_TOP/tools coverage html
+    fi
+    if [[ $test_results -eq 0 ]]; then
+        echo -e "${GREEN}All unittests pass${NC}!"
+    else
+        echo -e "${RED}There was a unittest failure${NC}"
+    fi
+}
+
+function run_unittests() {
+    local coverage_run=$1
+    local run_cmd="python"
+    local rc=0
+    if [[ $coverage_run == "coverage" ]]; then
+        # clear previously collected coverage data.
+        PYTHONPATH=$ANDROID_BUILD_TOP/tools coverage erase
+        run_cmd="coverage run --append"
+    fi
+
+    # Runs all unit tests under tools/acloud.
+    for t in $(find $ACLOUD_DIR -type f -name "*_test.py");
+    do
+        if ! PYTHONPATH=$ANDROID_BUILD_TOP/tools $run_cmd $t; then
+            rc=1
+            echo -e "${RED}$t failed${NC}"
+        fi
+    done
+
+    print_summary $rc $coverage_run
+    exit $rc
+}
+
+
+case "$1" in
+    'help')
+        helper
+        ;;
+    'coverage')
+        run_unittests "coverage"
+        ;;
+    *)
+        run_unittests
+        ;;
+esac
+