The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # |
| 4 | # Copyright 2008, The Android Open Source Project |
| 5 | # |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 9 | # |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 11 | # |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 16 | # limitations under the License. |
| 17 | |
| 18 | """Utilities for generating code coverage reports for Android tests.""" |
| 19 | |
| 20 | # Python imports |
| 21 | import glob |
| 22 | import optparse |
| 23 | import os |
| 24 | |
| 25 | # local imports |
| 26 | import android_build |
| 27 | import coverage_targets |
| 28 | import errors |
| 29 | import logger |
| 30 | import run_command |
| 31 | |
| 32 | |
| 33 | class CoverageGenerator(object): |
| 34 | """Helper utility for obtaining code coverage results on Android. |
| 35 | |
| 36 | Intended to simplify the process of building,running, and generating code |
| 37 | coverage results for a pre-defined set of tests and targets |
| 38 | """ |
| 39 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 40 | # path to EMMA host jar, relative to Android build root |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 41 | _EMMA_JAR = os.path.join("external", "emma", "lib", "emma.jar") |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 42 | _TEST_COVERAGE_EXT = "ec" |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 43 | # root path of generated coverage report files, relative to Android build root |
| 44 | _COVERAGE_REPORT_PATH = os.path.join("out", "emma") |
Brett Chabot | ae68f1a | 2009-05-28 18:29:24 -0700 | [diff] [blame] | 45 | _TARGET_DEF_FILE = "coverage_targets.xml" |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 46 | _CORE_TARGET_PATH = os.path.join("development", "testrunner", |
Brett Chabot | ae68f1a | 2009-05-28 18:29:24 -0700 | [diff] [blame] | 47 | _TARGET_DEF_FILE) |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 48 | # vendor glob file path patterns to tests, relative to android |
| 49 | # build root |
| 50 | _VENDOR_TARGET_PATH = os.path.join("vendor", "*", "tests", "testinfo", |
Brett Chabot | ae68f1a | 2009-05-28 18:29:24 -0700 | [diff] [blame] | 51 | _TARGET_DEF_FILE) |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 52 | |
| 53 | # path to root of target build intermediates |
| 54 | _TARGET_INTERMEDIATES_BASE_PATH = os.path.join("out", "target", "common", |
| 55 | "obj") |
| 56 | |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 57 | def __init__(self, adb_interface): |
| 58 | self._root_path = android_build.GetTop() |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 59 | self._output_root_path = os.path.join(self._root_path, |
| 60 | self._COVERAGE_REPORT_PATH) |
| 61 | self._emma_jar_path = os.path.join(self._root_path, self._EMMA_JAR) |
| 62 | self._adb = adb_interface |
| 63 | self._targets_manifest = self._ReadTargets() |
| 64 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 65 | def ExtractReport(self, test_suite, |
Brett Chabot | ae68f1a | 2009-05-28 18:29:24 -0700 | [diff] [blame] | 66 | device_coverage_path, |
Brett Chabot | 8424ffc | 2010-01-21 17:29:18 -0800 | [diff] [blame] | 67 | output_path=None, |
| 68 | test_qualifier=None): |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 69 | """Extract runtime coverage data and generate code coverage report. |
| 70 | |
| 71 | Assumes test has just been executed. |
| 72 | Args: |
| 73 | test_suite: TestSuite to generate coverage data for |
| 74 | device_coverage_path: location of coverage file on device |
| 75 | output_path: path to place output files in. If None will use |
Brett Chabot | 8424ffc | 2010-01-21 17:29:18 -0800 | [diff] [blame] | 76 | <android_root_path>/<_COVERAGE_REPORT_PATH>/<target>/<test[-qualifier]> |
| 77 | test_qualifier: designates mode test was run with. e.g size=small. |
| 78 | If not None, this will be used to customize output_path as shown above. |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 79 | |
| 80 | Returns: |
| 81 | absolute file path string of generated html report file. |
| 82 | """ |
| 83 | if output_path is None: |
Brett Chabot | 8424ffc | 2010-01-21 17:29:18 -0800 | [diff] [blame] | 84 | report_name = test_suite.GetName() |
| 85 | if test_qualifier: |
| 86 | report_name = report_name + "-" + test_qualifier |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 87 | output_path = os.path.join(self._root_path, |
| 88 | self._COVERAGE_REPORT_PATH, |
| 89 | test_suite.GetTargetName(), |
Brett Chabot | 8424ffc | 2010-01-21 17:29:18 -0800 | [diff] [blame] | 90 | report_name) |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 91 | |
Brett Chabot | 8424ffc | 2010-01-21 17:29:18 -0800 | [diff] [blame] | 92 | coverage_local_name = "%s.%s" % (report_name, |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 93 | self._TEST_COVERAGE_EXT) |
| 94 | coverage_local_path = os.path.join(output_path, |
| 95 | coverage_local_name) |
| 96 | if self._adb.Pull(device_coverage_path, coverage_local_path): |
| 97 | |
| 98 | report_path = os.path.join(output_path, |
Brett Chabot | 8424ffc | 2010-01-21 17:29:18 -0800 | [diff] [blame] | 99 | report_name) |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 100 | target = self._targets_manifest.GetTarget(test_suite.GetTargetName()) |
Brett Chabot | ae68f1a | 2009-05-28 18:29:24 -0700 | [diff] [blame] | 101 | if target is None: |
| 102 | msg = ["Error: test %s references undefined target %s." |
| 103 | % (test_suite.GetName(), test_suite.GetTargetName())] |
| 104 | msg.append(" Ensure target is defined in %s" % self._TARGET_DEF_FILE) |
| 105 | logger.Log("".join(msg)) |
| 106 | else: |
| 107 | return self._GenerateReport(report_path, coverage_local_path, [target], |
| 108 | do_src=True) |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 109 | return None |
| 110 | |
| 111 | def _GenerateReport(self, report_path, coverage_file_path, targets, |
| 112 | do_src=True): |
| 113 | """Generate the code coverage report. |
| 114 | |
| 115 | Args: |
| 116 | report_path: absolute file path of output file, without extension |
| 117 | coverage_file_path: absolute file path of code coverage result file |
| 118 | targets: list of CoverageTargets to use as base for code coverage |
| 119 | measurement. |
| 120 | do_src: True if generate coverage report with source linked in. |
| 121 | Note this will increase size of generated report. |
| 122 | |
| 123 | Returns: |
| 124 | absolute file path to generated report file. |
| 125 | """ |
| 126 | input_metadatas = self._GatherMetadatas(targets) |
| 127 | |
| 128 | if do_src: |
| 129 | src_arg = self._GatherSrcs(targets) |
| 130 | else: |
| 131 | src_arg = "" |
| 132 | |
| 133 | report_file = "%s.html" % report_path |
| 134 | cmd1 = ("java -cp %s emma report -r html -in %s %s %s " % |
| 135 | (self._emma_jar_path, coverage_file_path, input_metadatas, src_arg)) |
| 136 | cmd2 = "-Dreport.html.out.file=%s" % report_file |
| 137 | self._RunCmd(cmd1 + cmd2) |
| 138 | return report_file |
| 139 | |
| 140 | def _GatherMetadatas(self, targets): |
| 141 | """Builds the emma input metadata argument from provided targets. |
| 142 | |
| 143 | Args: |
| 144 | targets: list of CoverageTargets |
| 145 | |
| 146 | Returns: |
| 147 | input metadata argument string |
| 148 | """ |
| 149 | input_metadatas = "" |
| 150 | for target in targets: |
| 151 | input_metadata = os.path.join(self._GetBuildIntermediatePath(target), |
| 152 | "coverage.em") |
| 153 | input_metadatas += " -in %s" % input_metadata |
| 154 | return input_metadatas |
| 155 | |
| 156 | def _GetBuildIntermediatePath(self, target): |
| 157 | return os.path.join( |
| 158 | self._root_path, self._TARGET_INTERMEDIATES_BASE_PATH, target.GetType(), |
| 159 | "%s_intermediates" % target.GetName()) |
| 160 | |
| 161 | def _GatherSrcs(self, targets): |
| 162 | """Builds the emma input source path arguments from provided targets. |
| 163 | |
| 164 | Args: |
| 165 | targets: list of CoverageTargets |
| 166 | Returns: |
| 167 | source path arguments string |
| 168 | """ |
| 169 | src_list = [] |
| 170 | for target in targets: |
| 171 | target_srcs = target.GetPaths() |
| 172 | for path in target_srcs: |
| 173 | src_list.append("-sp %s" % os.path.join(self._root_path, path)) |
| 174 | return " ".join(src_list) |
| 175 | |
| 176 | def _MergeFiles(self, input_paths, dest_path): |
| 177 | """Merges a set of emma coverage files into a consolidated file. |
| 178 | |
| 179 | Args: |
| 180 | input_paths: list of string absolute coverage file paths to merge |
| 181 | dest_path: absolute file path of destination file |
| 182 | """ |
| 183 | input_list = [] |
| 184 | for input_path in input_paths: |
| 185 | input_list.append("-in %s" % input_path) |
| 186 | input_args = " ".join(input_list) |
| 187 | self._RunCmd("java -cp %s emma merge %s -out %s" % (self._emma_jar_path, |
| 188 | input_args, dest_path)) |
| 189 | |
| 190 | def _RunCmd(self, cmd): |
| 191 | """Runs and logs the given os command.""" |
| 192 | run_command.RunCommand(cmd, return_output=False) |
| 193 | |
| 194 | def _CombineTargetCoverage(self): |
| 195 | """Combines all target mode code coverage results. |
| 196 | |
| 197 | Will find all code coverage data files in direct sub-directories of |
| 198 | self._output_root_path, and combine them into a single coverage report. |
| 199 | Generated report is placed at self._output_root_path/android.html |
| 200 | """ |
| 201 | coverage_files = self._FindCoverageFiles(self._output_root_path) |
| 202 | combined_coverage = os.path.join(self._output_root_path, |
| 203 | "android.%s" % self._TEST_COVERAGE_EXT) |
| 204 | self._MergeFiles(coverage_files, combined_coverage) |
| 205 | report_path = os.path.join(self._output_root_path, "android") |
| 206 | # don't link to source, to limit file size |
| 207 | self._GenerateReport(report_path, combined_coverage, |
| 208 | self._targets_manifest.GetTargets(), do_src=False) |
| 209 | |
| 210 | def _CombineTestCoverage(self): |
| 211 | """Consolidates code coverage results for all target result directories.""" |
| 212 | target_dirs = os.listdir(self._output_root_path) |
| 213 | for target_name in target_dirs: |
| 214 | output_path = os.path.join(self._output_root_path, target_name) |
| 215 | target = self._targets_manifest.GetTarget(target_name) |
| 216 | if os.path.isdir(output_path) and target is not None: |
| 217 | coverage_files = self._FindCoverageFiles(output_path) |
| 218 | combined_coverage = os.path.join(output_path, "%s.%s" % |
| 219 | (target_name, self._TEST_COVERAGE_EXT)) |
| 220 | self._MergeFiles(coverage_files, combined_coverage) |
| 221 | report_path = os.path.join(output_path, target_name) |
| 222 | self._GenerateReport(report_path, combined_coverage, [target]) |
| 223 | else: |
| 224 | logger.Log("%s is not a valid target directory, skipping" % output_path) |
| 225 | |
| 226 | def _FindCoverageFiles(self, root_path): |
| 227 | """Finds all files in <root_path>/*/*.<_TEST_COVERAGE_EXT>. |
| 228 | |
| 229 | Args: |
| 230 | root_path: absolute file path string to search from |
| 231 | Returns: |
| 232 | list of absolute file path strings of coverage files |
| 233 | """ |
| 234 | file_pattern = os.path.join(root_path, "*", "*.%s" % |
| 235 | self._TEST_COVERAGE_EXT) |
| 236 | coverage_files = glob.glob(file_pattern) |
| 237 | return coverage_files |
| 238 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 239 | def _ReadTargets(self): |
| 240 | """Parses the set of coverage target data. |
| 241 | |
| 242 | Returns: |
| 243 | a CoverageTargets object that contains set of parsed targets. |
| 244 | Raises: |
| 245 | AbortError if a fatal error occurred when parsing the target files. |
| 246 | """ |
| 247 | core_target_path = os.path.join(self._root_path, self._CORE_TARGET_PATH) |
| 248 | try: |
| 249 | targets = coverage_targets.CoverageTargets() |
| 250 | targets.Parse(core_target_path) |
| 251 | vendor_targets_pattern = os.path.join(self._root_path, |
| 252 | self._VENDOR_TARGET_PATH) |
| 253 | target_file_paths = glob.glob(vendor_targets_pattern) |
| 254 | for target_file_path in target_file_paths: |
| 255 | targets.Parse(target_file_path) |
| 256 | return targets |
| 257 | except errors.ParseError: |
| 258 | raise errors.AbortError |
| 259 | |
| 260 | def TidyOutput(self): |
| 261 | """Runs tidy on all generated html files. |
| 262 | |
| 263 | This is needed to the html files can be displayed cleanly on a web server. |
| 264 | Assumes tidy is on current PATH. |
| 265 | """ |
| 266 | logger.Log("Tidying output files") |
| 267 | self._TidyDir(self._output_root_path) |
| 268 | |
| 269 | def _TidyDir(self, dir_path): |
| 270 | """Recursively tidy all html files in given dir_path.""" |
| 271 | html_file_pattern = os.path.join(dir_path, "*.html") |
| 272 | html_files_iter = glob.glob(html_file_pattern) |
| 273 | for html_file_path in html_files_iter: |
| 274 | os.system("tidy -m -errors -quiet %s" % html_file_path) |
| 275 | sub_dirs = os.listdir(dir_path) |
| 276 | for sub_dir_name in sub_dirs: |
| 277 | sub_dir_path = os.path.join(dir_path, sub_dir_name) |
| 278 | if os.path.isdir(sub_dir_path): |
| 279 | self._TidyDir(sub_dir_path) |
| 280 | |
| 281 | def CombineCoverage(self): |
| 282 | """Create combined coverage reports for all targets and tests.""" |
| 283 | self._CombineTestCoverage() |
| 284 | self._CombineTargetCoverage() |
| 285 | |
| 286 | |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 287 | def EnableCoverageBuild(): |
| 288 | """Enable building an Android target with code coverage instrumentation.""" |
| 289 | os.environ["EMMA_INSTRUMENT"] = "true" |
| 290 | |
| 291 | |
Guang Zhu | 863870c | 2010-04-15 10:48:26 -0700 | [diff] [blame] | 292 | def TestDeviceCoverageSupport(adb): |
| 293 | """Check if device has support for generating code coverage metrics. |
| 294 | |
| 295 | This tries to dump emma help information on device, a response containing |
| 296 | help information will indicate that emma is already on system class path. |
| 297 | |
| 298 | Returns: |
| 299 | True if device can support code coverage. False otherwise. |
| 300 | """ |
| 301 | try: |
| 302 | output = adb.SendShellCommand("exec app_process / emma -h") |
| 303 | |
| 304 | if output.find('emma usage:') == 0: |
| 305 | return True |
| 306 | except errors.AbortError: |
| 307 | pass |
| 308 | return False |
| 309 | |
| 310 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 311 | def Run(): |
| 312 | """Does coverage operations based on command line args.""" |
| 313 | # TODO: do we want to support combining coverage for a single target |
| 314 | |
| 315 | try: |
| 316 | parser = optparse.OptionParser(usage="usage: %prog --combine-coverage") |
| 317 | parser.add_option( |
| 318 | "-c", "--combine-coverage", dest="combine_coverage", default=False, |
| 319 | action="store_true", help="Combine coverage results stored given " |
| 320 | "android root path") |
| 321 | parser.add_option( |
| 322 | "-t", "--tidy", dest="tidy", default=False, action="store_true", |
| 323 | help="Run tidy on all generated html files") |
| 324 | |
| 325 | options, args = parser.parse_args() |
| 326 | |
Brett Chabot | ddbd282 | 2009-11-13 18:33:43 -0800 | [diff] [blame] | 327 | coverage = CoverageGenerator(None) |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 328 | if options.combine_coverage: |
| 329 | coverage.CombineCoverage() |
| 330 | if options.tidy: |
| 331 | coverage.TidyOutput() |
| 332 | except errors.AbortError: |
| 333 | logger.SilentLog("Exiting due to AbortError") |
| 334 | |
| 335 | if __name__ == "__main__": |
| 336 | Run() |