25
25
26
26
from .utils import execute_program
27
27
from .get_tools import get_gcov_program , \
28
- get_gcovr_program
28
+ get_gcovr_program
29
+ from .settings import COVERAGE_OUTPUT_TYPES
30
+
29
31
30
32
class CoverageAPI (object ):
31
33
"""
32
34
Generate code coverage reports for unit tests.
33
35
"""
36
+
34
37
def __init__ (self , mbed_os_root = None , build_dir = None ):
35
38
self .root = mbed_os_root
36
39
@@ -62,6 +65,8 @@ def _gen_cmd(self, coverage_type, excludes, filter_regex):
62
65
args .extend (["-x" ,
63
66
"-o" ,
64
67
"./coverage.xml" ])
68
+ else :
69
+ logging .error ("Invalid coverage output type: %s" % coverage_type )
65
70
66
71
# Add exclude filters:
67
72
for path in excludes :
@@ -103,9 +108,11 @@ def generate_reports(self,
103
108
build_path - build path
104
109
"""
105
110
111
+ # Check for the tool
106
112
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." )
109
116
return
110
117
111
118
if build_path is None :
@@ -118,19 +125,24 @@ def generate_reports(self,
118
125
excludes = []
119
126
120
127
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
+
121
135
if output == "html" :
122
- # Create build directory if not exist.
136
+ # Create a build directory if not exist
123
137
coverage_path = os .path .join (build_path , "coverage" )
124
138
if not os .path .exists (coverage_path ):
125
139
os .mkdir (coverage_path )
126
140
141
+ # Generate the command
127
142
args = self ._gen_cmd (output , excludes , filter_regex )
128
143
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 ())
0 commit comments