Skip to content

Commit 00397b2

Browse files
committed
Unit tests: improve argument checking in coverage.py
1 parent 04c98b0 commit 00397b2

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

UNITTESTS/unit_test/coverage.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525

2626
from .utils import execute_program
2727
from .get_tools import get_gcov_program, \
28-
get_gcovr_program
28+
get_gcovr_program
29+
from .settings import COVERAGE_OUTPUT_TYPES
30+
2931

3032
class CoverageAPI(object):
3133
"""
3234
Generate code coverage reports for unit tests.
3335
"""
36+
3437
def __init__(self, mbed_os_root=None, build_dir=None):
3538
self.root = mbed_os_root
3639

@@ -62,6 +65,8 @@ def _gen_cmd(self, coverage_type, excludes, filter_regex):
6265
args.extend(["-x",
6366
"-o",
6467
"./coverage.xml"])
68+
else:
69+
logging.error("Invalid coverage output type: %s" % coverage_type)
6570

6671
# Add exclude filters:
6772
for path in excludes:
@@ -103,9 +108,11 @@ def generate_reports(self,
103108
build_path - build path
104109
"""
105110

111+
# Check for the tool
106112
if get_gcovr_program() is None:
107-
logging.error("No gcovr tool found in path. \
108-
Cannot generate coverage reports.")
113+
logging.error(
114+
"No gcovr tool found in path. " +
115+
"Cannot generate coverage reports.")
109116
return
110117

111118
if build_path is None:
@@ -118,19 +125,24 @@ def generate_reports(self,
118125
excludes = []
119126

120127
for output in outputs:
128+
# Skip if invalid/unsupported output type
129+
if output not in COVERAGE_OUTPUT_TYPES:
130+
logging.warning(
131+
"Invalid output type. " +
132+
"Skip coverage report for type: %s." % output.upper())
133+
continue
134+
121135
if output == "html":
122-
# Create build directory if not exist.
136+
# Create a build directory if not exist
123137
coverage_path = os.path.join(build_path, "coverage")
124138
if not os.path.exists(coverage_path):
125139
os.mkdir(coverage_path)
126140

141+
# Generate the command
127142
args = self._gen_cmd(output, excludes, filter_regex)
128143

129-
if output == "html":
130-
execute_program(args,
131-
"HTML code coverage report generation failed.",
132-
"HTML code coverage report created.")
133-
elif output == "xml":
134-
execute_program(args,
135-
"XML code coverage report generation failed.",
136-
"XML code coverage report created.")
144+
# Run the coverage tool
145+
execute_program(
146+
args,
147+
"%s code coverage report generation failed." % output.upper(),
148+
"%s code coverage report created." % output.upper())

UNITTESTS/unit_test/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import argparse
2222
import logging
2323

24-
from .settings import CMAKE_GENERATORS, MAKE_PROGRAMS, COVERAGE_TYPES
24+
from .settings import CMAKE_GENERATORS, MAKE_PROGRAMS, COVERAGE_ARGS
2525
from .get_tools import get_make_tool
2626

2727
def get_options_parser():
@@ -71,7 +71,7 @@ def get_options_parser():
7171
dest="debug_build")
7272

7373
parser.add_argument("--coverage",
74-
choices=COVERAGE_TYPES,
74+
choices=COVERAGE_ARGS,
7575
help="Generate code coverage report",
7676
dest="coverage")
7777

UNITTESTS/unit_test/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
"ninja": "Ninja"
3030
}
3131

32-
COVERAGE_TYPES = ["html",
32+
COVERAGE_ARGS = ["html",
3333
"xml",
3434
"both"]
3535

36+
COVERAGE_OUTPUT_TYPES = ["html", "xml"]
37+
3638
CXX_COMPILERS = ["g++-6", "g++-8", "g++-7", "g++-5", "g++-4.9", "g++"]
3739

3840
C_COMPILERS = ["gcc-6", "gcc-8", "gcc-7", "gcc-5", "gcc-4.9", "gcc"]

0 commit comments

Comments
 (0)