Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(986)

Unified Diff: build/android/coverage.py

Issue 1211243016: Added coverage script and tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed nits. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | build/android/coverage_test.py » ('j') | build/android/coverage_test.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/coverage.py
diff --git a/build/android/coverage.py b/build/android/coverage.py
new file mode 100755
index 0000000000000000000000000000000000000000..68fe61c0130fcc316b874324629795581d689ee0
--- /dev/null
+++ b/build/android/coverage.py
@@ -0,0 +1,236 @@
+#!/usr/bin/python
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Generates incremental code coverage reports for Java code in Chromium."""
+
+import os
+from lxml import html
jbudorick 2015/07/14 16:26:24 I'm somewhat concerned about this. What if we don'
estevenson1 2015/07/21 00:01:15 Was able to replace this with the standard library
+
+
+class LineCoverage(object):
jbudorick 2015/07/14 16:26:24 This is basically a namedtuple with constants defi
estevenson1 2015/07/21 00:01:15 Done.
+ """Coverage information about a single line of code."""
+
+ NOT_EXECUTABLE = -1
+ NOT_COVERED = 0
+ COVERED = 1
+ PARTIALLY_COVERED = 2
+
+ def __init__(self, lineno, source, covered_status, fractional_line_coverage):
+ """Initializes LineCoverage.
+
+ Args:
+ lineno: Integer line number.
+ source: A string containing the original line of source code.
+ covered_status: The covered status of the line.
+ fractional_line_coverage: The fractional value representing the fraction
+ of instructions executed for a given line of code. Should be a floating
+ point number between (0.0, 1.0).
+ """
+ self.lineno = lineno
+ self.source = source
+ self.covered_status = covered_status
+ self.fractional_line_coverage = fractional_line_coverage
+
+
+class _EmmaHtmlParser(object):
+ """Encapsulates HTML file parsing operations.
+
+ This class contains all operations related to parsing HTML files that were
+ produced using the EMMA code coverage tool. It uses the lxml module for
+ parsing.
+
+ Example HTML:
+
+ Package links:
+ <a href="_files/1.html">org.chromium.chrome</a>
+ This is returned by the selector |XPATH_SELECT_PACKAGE_ELEMENTS|.
+
+ Class links:
+ <a href="1e.html">DoActivity.java</a>
+ This is returned by the selector |XPATH_SELECT_CLASS_ELEMENTS|.
+
+ Line coverage data:
+ <tr class="p">
+ <td class="l" title="78% line coverage (7 out of 9)">108</td>
+ <td title="78% line coverage (7 out of 9 instructions)">
+ if (index < 0 || index = mSelectors.size()) index = 0;</td>
+ </tr>
+ <tr>
+ <td class="l">109</td>
+ <td> </td>
+ </tr>
+ <tr class="c">
+ <td class="l">110</td>
+ <td> if (mSelectors.get(index) != null) {</td>
+ </tr>
+ <tr class="z">
+ <td class="l">111</td>
+ <td> for (int i = 0; i < mSelectors.size(); i++) {</td>
+ </tr>
+ Each <tr> element is returned by the selector |XPATH_SELECT_LOC|.
+
+ We can parse this to get:
+ 1. Line number
+ 2. Line of source code
+ 3. Coverage status (c, z, or p)
+ 4. Fractional coverage value (% out of 100 if PARTIALLY_COVERED)
+ """
+ # Selector to match all <a> elements within the rows that are in the table
+ # that displays all of the different packages.
+ _XPATH_SELECT_PACKAGE_ELEMENTS = '/html/body/table[4]/tr[*]/td/a'
+
+ # Selector to match all <a> elements within the rows that are in the table
+ # that displays all of the different packages within a class.
+ _XPATH_SELECT_CLASS_ELEMENTS = '/html/body/table[3]/tr[*]/td/a'
+
+ # Selector to match all <tr> elements within the table containing Java source
+ # code in an EMMA HTML file.
+ _XPATH_SELECT_LOC = '/html/body/table[4]/tr'
+
+ # Children of HTML elements are represented as a list in lxml. These constants
+ # represent list indices corresponding to relevant child elements.
+
+ # Child 1 contains percentage covered for a line.
+ _ELEMENT_PERCENT_COVERED = 1
+
+ # Child 1 contains the original line of source code.
+ _ELEMENT_CONTAINING_SOURCE_CODE = 1
+
+ # Child 0 contains the line number.
+ _ELEMENT_CONTAINING_LINENO = 0
+
+ # Maps CSS class names to corresponding coverage constants.
+ _CSS_TO_STATUS = {
+ 'c': LineCoverage.COVERED,
+ 'p': LineCoverage.PARTIALLY_COVERED,
+ 'z': LineCoverage.NOT_COVERED
+ }
+
+ # UTF-8 no break space.
+ _NO_BREAK_SPACE = '\xc2\xa0'
+
+ def __init__(self, emma_file_base_dir):
+ """Initializes _EmmaHtmlParser.
+
+ Args:
+ emma_file_base_dir: Path to the location where EMMA report files are
+ stored. Should be where index.html is stored.
+ """
+ self._base_dir = emma_file_base_dir
+ self._emma_files_path = os.path.join(self._base_dir, '_files')
+ self._index_path = os.path.join(self._base_dir, 'index.html')
+
+ def GetLineCoverage(self, emma_file_path):
+ """Returns a list of LineCoverage objects for the given EMMA HTML file.
+
+ Args:
+ emma_file_path: String representing the path to the EMMA HTML file.
+
+ Returns:
+ A list of LineCoverage objects.
+ """
+ def get_status(tr_element):
jbudorick 2015/07/14 16:26:24 I'm not sure that these need to be local functions
estevenson1 2015/07/21 00:01:15 Done.
+ """Returns coverage status for a <tr> element containing coverage info."""
+ if 'class' not in tr_element.attrib:
+ status = LineCoverage.NOT_EXECUTABLE
+ else:
+ status = self._CSS_TO_STATUS.get(
+ tr_element.attrib['class'], LineCoverage.NOT_EXECUTABLE)
+ return status
+
+ def get_fractional_line_coverage(tr_element, status):
+ """Returns coverage value for a <tr> element containing coverage info."""
+ # If line is partially covered, parse the <td> tag to get the
+ # coverage percent.
+ if status == LineCoverage.PARTIALLY_COVERED:
+ title_attribute = (
+ tr_element[self._ELEMENT_PERCENT_COVERED].attrib['title'])
+ # Parse string that contains percent covered: "83% line coverage ,,,".
mikecase (-- gone --) 2015/07/13 16:50:07 Is this supposed to be "83% line coverage ,,," or
estevenson1 2015/07/21 00:01:15 Done.
+ percent_covered = title_attribute.split('%')[0]
+ fractional_coverage_value = int(percent_covered) / 100.0
+ else:
+ fractional_coverage_value = 1.0
+ return fractional_coverage_value
+
+ def get_lineno(tr_element):
+ """Returns line number for a <tr> element containing coverage info."""
+ lineno_element = tr_element[self._ELEMENT_CONTAINING_LINENO]
+ # Handles oddly formatted HTML (where there is an extra <a> tag).
+ lineno = int(lineno_element.text or
+ lineno_element[self._ELEMENT_CONTAINING_LINENO].text)
+ return lineno
+
+ def get_source_code(tr_element):
+ """Returns Java source for a <tr> element containing coverage info."""
+ raw_source = tr_element[self._ELEMENT_CONTAINING_SOURCE_CODE].text
+ utf8_source = raw_source.encode('UTF-8')
+ readable_source = utf8_source.replace(self._NO_BREAK_SPACE, ' ')
+ return readable_source
+
+ line_tr_elements = self._FindElements(
+ emma_file_path, self._XPATH_SELECT_LOC)
+ line_coverage = []
+ for tr in line_tr_elements:
+ coverage_status = get_status(tr)
+ fractional_coverage = get_fractional_line_coverage(tr, coverage_status)
+ lineno = get_lineno(tr)
+ source = get_source_code(tr)
+ line = LineCoverage(lineno, source, coverage_status, fractional_coverage)
jbudorick 2015/07/14 16:26:24 I know I mentioned above about turning LineCoverag
estevenson1 2015/07/21 00:01:15 Chose to change LineCoverage into a namedtuple. I
+ line_coverage.append(line)
+
+ return line_coverage
+
+ def GetPackageNameToEmmaFileDict(self):
+ """Returns a dict mapping Java packages to EMMA HTML coverage files.
+
+ Parses the EMMA index.html file to get a list of packages, then parses each
+ package HTML file to get a list of classes for that package, and creates
+ a dict with this info.
+
+ Returns:
+ A dict mapping string representation of Java packages (with class
+ names appended) to the corresponding file paths of EMMA HTML files.
+ """
+ # These <a> elements contain each package name and the path of the file
+ # where all classes within said package are listed.
+ package_link_elements = self._FindElements(
+ self._index_path, self._XPATH_SELECT_PACKAGE_ELEMENTS)
+ # Maps file path of package directory (EMMA generated) to package name.
+ # Ex. emma_dir/f.html: org.chromium.chrome.
+ package_links = {
+ os.path.join(self._base_dir, link.attrib['href']): link.text
+ for link in package_link_elements if 'href' in link.attrib
+ }
+
+ package_to_emma = {}
+ for package_emma_file_path, package_name in package_links.iteritems():
+ # These <a> elements contain each class name in the current package and
+ # the path of the file where the coverage info is stored for each class.
+ coverage_file_link_elements = self._FindElements(
+ package_emma_file_path, self._XPATH_SELECT_CLASS_ELEMENTS)
+
+ for coverage_file_element in coverage_file_link_elements:
+ emma_coverage_file_path = os.path.join(
+ self._emma_files_path, coverage_file_element.attrib['href'])
+ full_package_name = package_name + '.' + coverage_file_element.text
jbudorick 2015/07/14 16:26:24 nit: '%s.%s' % (package_name, coverage_file_elemen
estevenson1 2015/07/21 00:01:15 Done.
+ package_to_emma[full_package_name] = emma_coverage_file_path
+
+ return package_to_emma
+
+ def _FindElements(self, file_path, xpath_selector):
+ """Reads a HTML file and performs an XPath match.
+
+ Args:
+ file_path: String representing the path to the HTML file.
+ xpath_selector: String representing xpath search pattern.
+
+ Returns:
+ A list of lxml.html.HtmlElements matching the given XPath selector.
+ Returns an empty list if there is no match.
+ """
+ with open(file_path) as f:
+ file_contents = f.read().decode('ISO-8859-1')
+ root = html.fromstring(file_contents)
+ return root.xpath(xpath_selector)
« no previous file with comments | « no previous file | build/android/coverage_test.py » ('j') | build/android/coverage_test.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698