From 00397b232cae3c18698ca51bf8be8eea11c050e9 Mon Sep 17 00:00:00 2001
From: Lari-Matias Orjala <Lari-Matias.Orjala@arm.com>
Date: Tue, 18 Sep 2018 10:51:08 +0300
Subject: [PATCH] Unit tests: improve argument checking in coverage.py

---
 UNITTESTS/unit_test/coverage.py | 36 ++++++++++++++++++++++-----------
 UNITTESTS/unit_test/options.py  |  4 ++--
 UNITTESTS/unit_test/settings.py |  4 +++-
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/UNITTESTS/unit_test/coverage.py b/UNITTESTS/unit_test/coverage.py
index ba0adbb960..add8e1d2a9 100644
--- a/UNITTESTS/unit_test/coverage.py
+++ b/UNITTESTS/unit_test/coverage.py
@@ -25,12 +25,15 @@ import re
 
 from .utils import execute_program
 from .get_tools import get_gcov_program, \
-                       get_gcovr_program
+    get_gcovr_program
+from .settings import COVERAGE_OUTPUT_TYPES
+
 
 class CoverageAPI(object):
     """
     Generate code coverage reports for unit tests.
     """
+
     def __init__(self, mbed_os_root=None, build_dir=None):
         self.root = mbed_os_root
 
@@ -62,6 +65,8 @@ class CoverageAPI(object):
             args.extend(["-x",
                          "-o",
                          "./coverage.xml"])
+        else:
+            logging.error("Invalid coverage output type: %s" % coverage_type)
 
         # Add exclude filters:
         for path in excludes:
@@ -103,9 +108,11 @@ class CoverageAPI(object):
         build_path - build path
         """
 
+        # Check for the tool
         if get_gcovr_program() is None:
-            logging.error("No gcovr tool found in path. \
-            Cannot generate coverage reports.")
+            logging.error(
+                "No gcovr tool found in path. " +
+                "Cannot generate coverage reports.")
             return
 
         if build_path is None:
@@ -118,19 +125,24 @@ class CoverageAPI(object):
             excludes = []
 
         for output in outputs:
+            # Skip if invalid/unsupported output type
+            if output not in COVERAGE_OUTPUT_TYPES:
+                logging.warning(
+                    "Invalid output type. " +
+                    "Skip coverage report for type: %s." % output.upper())
+                continue
+
             if output == "html":
-                # Create build directory if not exist.
+                # Create a build directory if not exist
                 coverage_path = os.path.join(build_path, "coverage")
                 if not os.path.exists(coverage_path):
                     os.mkdir(coverage_path)
 
+            # Generate the command
             args = self._gen_cmd(output, excludes, filter_regex)
 
-            if output == "html":
-                execute_program(args,
-                                "HTML code coverage report generation failed.",
-                                "HTML code coverage report created.")
-            elif output == "xml":
-                execute_program(args,
-                                "XML code coverage report generation failed.",
-                                "XML code coverage report created.")
+            # Run the coverage tool
+            execute_program(
+                args,
+                "%s code coverage report generation failed." % output.upper(),
+                "%s code coverage report created." % output.upper())
diff --git a/UNITTESTS/unit_test/options.py b/UNITTESTS/unit_test/options.py
index 0c9eefa7af..211c5d10b9 100644
--- a/UNITTESTS/unit_test/options.py
+++ b/UNITTESTS/unit_test/options.py
@@ -21,7 +21,7 @@ UNIT TEST OPTIONS
 import argparse
 import logging
 
-from .settings import CMAKE_GENERATORS, MAKE_PROGRAMS, COVERAGE_TYPES
+from .settings import CMAKE_GENERATORS, MAKE_PROGRAMS, COVERAGE_ARGS
 from .get_tools import get_make_tool
 
 def get_options_parser():
@@ -71,7 +71,7 @@ def get_options_parser():
                         dest="debug_build")
 
     parser.add_argument("--coverage",
-                        choices=COVERAGE_TYPES,
+                        choices=COVERAGE_ARGS,
                         help="Generate code coverage report",
                         dest="coverage")
 
diff --git a/UNITTESTS/unit_test/settings.py b/UNITTESTS/unit_test/settings.py
index 717cab8523..883b4a82f4 100644
--- a/UNITTESTS/unit_test/settings.py
+++ b/UNITTESTS/unit_test/settings.py
@@ -29,10 +29,12 @@ DEFAULT_CMAKE_GENERATORS = {
     "ninja": "Ninja"
 }
 
-COVERAGE_TYPES = ["html",
+COVERAGE_ARGS = ["html",
                   "xml",
                   "both"]
 
+COVERAGE_OUTPUT_TYPES = ["html", "xml"]
+
 CXX_COMPILERS = ["g++-6", "g++-8", "g++-7", "g++-5", "g++-4.9", "g++"]
 
 C_COMPILERS = ["gcc-6", "gcc-8", "gcc-7", "gcc-5", "gcc-4.9", "gcc"]