PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/build/android/emma_coverage_stats.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 477 lines | 446 code | 7 blank | 24 comment | 0 complexity | 87a4372f26d5c82c0a989ba959ccf64f MD5 | raw file
  1. #!/usr/bin/python
  2. # Copyright 2015 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Generates incremental code coverage reports for Java code in Chromium.
  6. Usage:
  7. build/android/emma_coverage_stats.py -v --out <output file path> --emma-dir
  8. <EMMA file directory> --lines-for-coverage-file
  9. <path to file containing lines for coverage>
  10. Creates a JSON representation of the overall and file coverage stats and saves
  11. this information to the specified output file.
  12. """
  13. import argparse
  14. import collections
  15. import json
  16. import logging
  17. import os
  18. import re
  19. import sys
  20. from xml.etree import ElementTree
  21. from devil.utils import run_tests_helper
  22. NOT_EXECUTABLE = -1
  23. NOT_COVERED = 0
  24. COVERED = 1
  25. PARTIALLY_COVERED = 2
  26. # Coverage information about a single line of code.
  27. LineCoverage = collections.namedtuple(
  28. 'LineCoverage',
  29. ['lineno', 'source', 'covered_status', 'fractional_line_coverage'])
  30. class _EmmaHtmlParser(object):
  31. """Encapsulates HTML file parsing operations.
  32. This class contains all operations related to parsing HTML files that were
  33. produced using the EMMA code coverage tool.
  34. Example HTML:
  35. Package links:
  36. <a href="_files/1.html">org.chromium.chrome</a>
  37. This is returned by the selector |XPATH_SELECT_PACKAGE_ELEMENTS|.
  38. Class links:
  39. <a href="1e.html">DoActivity.java</a>
  40. This is returned by the selector |XPATH_SELECT_CLASS_ELEMENTS|.
  41. Line coverage data:
  42. <tr class="p">
  43. <td class="l" title="78% line coverage (7 out of 9)">108</td>
  44. <td title="78% line coverage (7 out of 9 instructions)">
  45. if (index < 0 || index = mSelectors.size()) index = 0;</td>
  46. </tr>
  47. <tr>
  48. <td class="l">109</td>
  49. <td> </td>
  50. </tr>
  51. <tr class="c">
  52. <td class="l">110</td>
  53. <td> if (mSelectors.get(index) != null) {</td>
  54. </tr>
  55. <tr class="z">
  56. <td class="l">111</td>
  57. <td> for (int i = 0; i < mSelectors.size(); i++) {</td>
  58. </tr>
  59. Each <tr> element is returned by the selector |XPATH_SELECT_LOC|.
  60. We can parse this to get:
  61. 1. Line number
  62. 2. Line of source code
  63. 3. Coverage status (c, z, or p)
  64. 4. Fractional coverage value (% out of 100 if PARTIALLY_COVERED)
  65. """
  66. # Selector to match all <a> elements within the rows that are in the table
  67. # that displays all of the different packages.
  68. _XPATH_SELECT_PACKAGE_ELEMENTS = './/BODY/TABLE[4]/TR/TD/A'
  69. # Selector to match all <a> elements within the rows that are in the table
  70. # that displays all of the different classes within a package.
  71. _XPATH_SELECT_CLASS_ELEMENTS = './/BODY/TABLE[3]/TR/TD/A'
  72. # Selector to match all <tr> elements within the table containing Java source
  73. # code in an EMMA HTML file.
  74. _XPATH_SELECT_LOC = './/BODY/TABLE[4]/TR'
  75. # Children of HTML elements are represented as a list in ElementTree. These
  76. # constants represent list indices corresponding to relevant child elements.
  77. # Child 1 contains percentage covered for a line.
  78. _ELEMENT_PERCENT_COVERED = 1
  79. # Child 1 contains the original line of source code.
  80. _ELEMENT_CONTAINING_SOURCE_CODE = 1
  81. # Child 0 contains the line number.
  82. _ELEMENT_CONTAINING_LINENO = 0
  83. # Maps CSS class names to corresponding coverage constants.
  84. _CSS_TO_STATUS = {'c': COVERED, 'p': PARTIALLY_COVERED, 'z': NOT_COVERED}
  85. # UTF-8 no break space.
  86. _NO_BREAK_SPACE = '\xc2\xa0'
  87. def __init__(self, emma_file_base_dir):
  88. """Initializes _EmmaHtmlParser.
  89. Args:
  90. emma_file_base_dir: Path to the location where EMMA report files are
  91. stored. Should be where index.html is stored.
  92. """
  93. self._base_dir = emma_file_base_dir
  94. self._emma_files_path = os.path.join(self._base_dir, '_files')
  95. self._index_path = os.path.join(self._base_dir, 'index.html')
  96. def GetLineCoverage(self, emma_file_path):
  97. """Returns a list of LineCoverage objects for the given EMMA HTML file.
  98. Args:
  99. emma_file_path: String representing the path to the EMMA HTML file.
  100. Returns:
  101. A list of LineCoverage objects.
  102. """
  103. line_tr_elements = self._FindElements(
  104. emma_file_path, self._XPATH_SELECT_LOC)
  105. line_coverage = []
  106. for tr in line_tr_elements:
  107. # Get the coverage status.
  108. coverage_status = self._CSS_TO_STATUS.get(tr.get('CLASS'), NOT_EXECUTABLE)
  109. # Get the fractional coverage value.
  110. if coverage_status == PARTIALLY_COVERED:
  111. title_attribute = (tr[self._ELEMENT_PERCENT_COVERED].get('TITLE'))
  112. # Parse string that contains percent covered: "83% line coverage ...".
  113. percent_covered = title_attribute.split('%')[0]
  114. fractional_coverage = int(percent_covered) / 100.0
  115. else:
  116. fractional_coverage = 1.0
  117. # Get the line number.
  118. lineno_element = tr[self._ELEMENT_CONTAINING_LINENO]
  119. # Handles oddly formatted HTML (where there is an extra <a> tag).
  120. lineno = int(lineno_element.text or
  121. lineno_element[self._ELEMENT_CONTAINING_LINENO].text)
  122. # Get the original line of Java source code.
  123. raw_source = tr[self._ELEMENT_CONTAINING_SOURCE_CODE].text
  124. utf8_source = raw_source.encode('UTF-8')
  125. source = utf8_source.replace(self._NO_BREAK_SPACE, ' ')
  126. line = LineCoverage(lineno, source, coverage_status, fractional_coverage)
  127. line_coverage.append(line)
  128. return line_coverage
  129. def GetPackageNameToEmmaFileDict(self):
  130. """Returns a dict mapping Java packages to EMMA HTML coverage files.
  131. Parses the EMMA index.html file to get a list of packages, then parses each
  132. package HTML file to get a list of classes for that package, and creates
  133. a dict with this info.
  134. Returns:
  135. A dict mapping string representation of Java packages (with class
  136. names appended) to the corresponding file paths of EMMA HTML files.
  137. """
  138. # These <a> elements contain each package name and the path of the file
  139. # where all classes within said package are listed.
  140. package_link_elements = self._FindElements(
  141. self._index_path, self._XPATH_SELECT_PACKAGE_ELEMENTS)
  142. # Maps file path of package directory (EMMA generated) to package name.
  143. # Example: emma_dir/f.html: org.chromium.chrome.
  144. package_links = {
  145. os.path.join(self._base_dir, link.attrib['HREF']): link.text
  146. for link in package_link_elements if 'HREF' in link.attrib
  147. }
  148. package_to_emma = {}
  149. for package_emma_file_path, package_name in package_links.iteritems():
  150. # These <a> elements contain each class name in the current package and
  151. # the path of the file where the coverage info is stored for each class.
  152. coverage_file_link_elements = self._FindElements(
  153. package_emma_file_path, self._XPATH_SELECT_CLASS_ELEMENTS)
  154. for class_name_element in coverage_file_link_elements:
  155. emma_coverage_file_path = os.path.join(
  156. self._emma_files_path, class_name_element.attrib['HREF'])
  157. full_package_name = '%s.%s' % (package_name, class_name_element.text)
  158. package_to_emma[full_package_name] = emma_coverage_file_path
  159. return package_to_emma
  160. # pylint: disable=no-self-use
  161. def _FindElements(self, file_path, xpath_selector):
  162. """Reads a HTML file and performs an XPath match.
  163. Args:
  164. file_path: String representing the path to the HTML file.
  165. xpath_selector: String representing xpath search pattern.
  166. Returns:
  167. A list of ElementTree.Elements matching the given XPath selector.
  168. Returns an empty list if there is no match.
  169. """
  170. with open(file_path) as f:
  171. file_contents = f.read().decode('ISO-8859-1').encode('UTF-8')
  172. root = ElementTree.fromstring(file_contents)
  173. return root.findall(xpath_selector)
  174. class _EmmaCoverageStats(object):
  175. """Computes code coverage stats for Java code using the coverage tool EMMA.
  176. This class provides an API that allows users to capture absolute code coverage
  177. and code coverage on a subset of lines for each Java source file. Coverage
  178. reports are generated in JSON format.
  179. """
  180. # Regular expression to get package name from Java package statement.
  181. RE_PACKAGE_MATCH_GROUP = 'package'
  182. RE_PACKAGE = re.compile(r'package (?P<%s>[\w.]*);' % RE_PACKAGE_MATCH_GROUP)
  183. def __init__(self, emma_file_base_dir, files_for_coverage):
  184. """Initialize _EmmaCoverageStats.
  185. Args:
  186. emma_file_base_dir: String representing the path to the base directory
  187. where EMMA HTML coverage files are stored, i.e. parent of index.html.
  188. files_for_coverage: A list of Java source code file paths to get EMMA
  189. coverage for.
  190. """
  191. self._emma_parser = _EmmaHtmlParser(emma_file_base_dir)
  192. self._source_to_emma = self._GetSourceFileToEmmaFileDict(files_for_coverage)
  193. def GetCoverageDict(self, lines_for_coverage):
  194. """Returns a dict containing detailed coverage information.
  195. Gets detailed coverage stats for each file specified in the
  196. |lines_for_coverage| dict and the total incremental number of lines covered
  197. and executable for all files in |lines_for_coverage|.
  198. Args:
  199. lines_for_coverage: A dict mapping Java source file paths to lists of line
  200. numbers.
  201. Returns:
  202. A dict containing coverage stats for the given dict of files and lines.
  203. Contains absolute coverage stats for each file, coverage stats for each
  204. file's lines specified in |lines_for_coverage|, line by line coverage
  205. for each file, and overall coverage stats for the lines specified in
  206. |lines_for_coverage|.
  207. """
  208. file_coverage = {}
  209. for file_path, line_numbers in lines_for_coverage.iteritems():
  210. file_coverage_dict = self.GetCoverageDictForFile(file_path, line_numbers)
  211. if file_coverage_dict:
  212. file_coverage[file_path] = file_coverage_dict
  213. else:
  214. logging.warning(
  215. 'No code coverage data for %s, skipping.', file_path)
  216. covered_statuses = [s['incremental'] for s in file_coverage.itervalues()]
  217. num_covered_lines = sum(s['covered'] for s in covered_statuses)
  218. num_total_lines = sum(s['total'] for s in covered_statuses)
  219. return {
  220. 'files': file_coverage,
  221. 'patch': {
  222. 'incremental': {
  223. 'covered': num_covered_lines,
  224. 'total': num_total_lines
  225. }
  226. }
  227. }
  228. def GetCoverageDictForFile(self, file_path, line_numbers):
  229. """Returns a dict containing detailed coverage info for the given file.
  230. Args:
  231. file_path: The path to the Java source file that we want to create the
  232. coverage dict for.
  233. line_numbers: A list of integer line numbers to retrieve additional stats
  234. for.
  235. Returns:
  236. A dict containing absolute, incremental, and line by line coverage for
  237. a file.
  238. """
  239. if file_path not in self._source_to_emma:
  240. return None
  241. emma_file = self._source_to_emma[file_path]
  242. total_line_coverage = self._emma_parser.GetLineCoverage(emma_file)
  243. incremental_line_coverage = [line for line in total_line_coverage
  244. if line.lineno in line_numbers]
  245. line_by_line_coverage = [
  246. {
  247. 'line': line.source,
  248. 'coverage': line.covered_status,
  249. 'changed': line.lineno in line_numbers,
  250. 'fractional_coverage': line.fractional_line_coverage,
  251. }
  252. for line in total_line_coverage
  253. ]
  254. total_covered_lines, total_lines = (
  255. self.GetSummaryStatsForLines(total_line_coverage))
  256. incremental_covered_lines, incremental_total_lines = (
  257. self.GetSummaryStatsForLines(incremental_line_coverage))
  258. file_coverage_stats = {
  259. 'absolute': {
  260. 'covered': total_covered_lines,
  261. 'total': total_lines
  262. },
  263. 'incremental': {
  264. 'covered': incremental_covered_lines,
  265. 'total': incremental_total_lines
  266. },
  267. 'source': line_by_line_coverage,
  268. }
  269. return file_coverage_stats
  270. # pylint: disable=no-self-use
  271. def GetSummaryStatsForLines(self, line_coverage):
  272. """Gets summary stats for a given list of LineCoverage objects.
  273. Args:
  274. line_coverage: A list of LineCoverage objects.
  275. Returns:
  276. A tuple containing the number of lines that are covered and the total
  277. number of lines that are executable, respectively
  278. """
  279. partially_covered_sum = 0
  280. covered_status_totals = {COVERED: 0, NOT_COVERED: 0, PARTIALLY_COVERED: 0}
  281. for line in line_coverage:
  282. status = line.covered_status
  283. if status == NOT_EXECUTABLE:
  284. continue
  285. covered_status_totals[status] += 1
  286. if status == PARTIALLY_COVERED:
  287. partially_covered_sum += line.fractional_line_coverage
  288. total_covered = covered_status_totals[COVERED] + partially_covered_sum
  289. total_lines = sum(covered_status_totals.values())
  290. return total_covered, total_lines
  291. def _GetSourceFileToEmmaFileDict(self, files):
  292. """Gets a dict used to correlate Java source files with EMMA HTML files.
  293. This method gathers the information needed to correlate EMMA HTML
  294. files with Java source files. EMMA XML and plain text reports do not provide
  295. line by line coverage data, so HTML reports must be used instead.
  296. Unfortunately, the HTML files that are created are given garbage names
  297. (i.e 1.html) so we need to manually correlate EMMA HTML files
  298. with the original Java source files.
  299. Args:
  300. files: A list of file names for which coverage information is desired.
  301. Returns:
  302. A dict mapping Java source file paths to EMMA HTML file paths.
  303. """
  304. # Maps Java source file paths to package names.
  305. # Example: /usr/code/file.java -> org.chromium.file.java.
  306. source_to_package = {}
  307. for file_path in files:
  308. package = self.GetPackageNameFromFile(file_path)
  309. if package:
  310. source_to_package[file_path] = package
  311. else:
  312. logging.warning("Skipping %s because it doesn\'t have a package "
  313. "statement.", file_path)
  314. # Maps package names to EMMA report HTML files.
  315. # Example: org.chromium.file.java -> out/coverage/1a.html.
  316. package_to_emma = self._emma_parser.GetPackageNameToEmmaFileDict()
  317. # Finally, we have a dict mapping Java file paths to EMMA report files.
  318. # Example: /usr/code/file.java -> out/coverage/1a.html.
  319. source_to_emma = {source: package_to_emma[package]
  320. for source, package in source_to_package.iteritems()
  321. if package in package_to_emma}
  322. return source_to_emma
  323. @staticmethod
  324. def NeedsCoverage(file_path):
  325. """Checks to see if the file needs to be analyzed for code coverage.
  326. Args:
  327. file_path: A string representing path to the file.
  328. Returns:
  329. True for Java files that exist, False for all others.
  330. """
  331. if os.path.splitext(file_path)[1] == '.java' and os.path.exists(file_path):
  332. return True
  333. else:
  334. logging.info('Skipping file %s, cannot compute code coverage.', file_path)
  335. return False
  336. @staticmethod
  337. def GetPackageNameFromFile(file_path):
  338. """Gets the full package name including the file name for a given file path.
  339. Args:
  340. file_path: String representing the path to the Java source file.
  341. Returns:
  342. A string representing the full package name with file name appended or
  343. None if there is no package statement in the file.
  344. """
  345. with open(file_path) as f:
  346. file_content = f.read()
  347. package_match = re.search(_EmmaCoverageStats.RE_PACKAGE, file_content)
  348. if package_match:
  349. package = package_match.group(_EmmaCoverageStats.RE_PACKAGE_MATCH_GROUP)
  350. file_name = os.path.basename(file_path)
  351. return '%s.%s' % (package, file_name)
  352. else:
  353. return None
  354. def GenerateCoverageReport(line_coverage_file, out_file_path, coverage_dir):
  355. """Generates a coverage report for a given set of lines.
  356. Writes the results of the coverage analysis to the file specified by
  357. |out_file_path|.
  358. Args:
  359. line_coverage_file: The path to a file which contains a dict mapping file
  360. names to lists of line numbers. Example: {file1: [1, 2, 3], ...} means
  361. that we should compute coverage information on lines 1 - 3 for file1.
  362. out_file_path: A string representing the location to write the JSON report.
  363. coverage_dir: A string representing the file path where the EMMA
  364. HTML coverage files are located (i.e. folder where index.html is located).
  365. """
  366. with open(line_coverage_file) as f:
  367. potential_files_for_coverage = json.load(f)
  368. files_for_coverage = {f: lines
  369. for f, lines in potential_files_for_coverage.iteritems()
  370. if _EmmaCoverageStats.NeedsCoverage(f)}
  371. coverage_results = {}
  372. if files_for_coverage:
  373. code_coverage = _EmmaCoverageStats(coverage_dir, files_for_coverage.keys())
  374. coverage_results = code_coverage.GetCoverageDict(files_for_coverage)
  375. else:
  376. logging.info('No Java files requiring coverage were included in %s.',
  377. line_coverage_file)
  378. with open(out_file_path, 'w+') as out_status_file:
  379. json.dump(coverage_results, out_status_file)
  380. def main():
  381. argparser = argparse.ArgumentParser()
  382. argparser.add_argument('--out', required=True, type=str,
  383. help='Report output file path.')
  384. argparser.add_argument('--emma-dir', required=True, type=str,
  385. help='EMMA HTML report directory.')
  386. argparser.add_argument('--lines-for-coverage-file', required=True, type=str,
  387. help='File containing a JSON object. Should contain a '
  388. 'dict mapping file names to lists of line numbers of '
  389. 'code for which coverage information is desired.')
  390. argparser.add_argument('-v', '--verbose', action='count',
  391. help='Print verbose log information.')
  392. args = argparser.parse_args()
  393. run_tests_helper.SetLogLevel(args.verbose)
  394. GenerateCoverageReport(args.lines_for_coverage_file, args.out, args.emma_dir)
  395. if __name__ == '__main__':
  396. sys.exit(main())