Skip to content

Commit

Permalink
[infra] Fix jacoco_report_converter.py
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Chan <[email protected]>
  • Loading branch information
arthurscchan committed Jun 4, 2024
1 parent 1515519 commit 5274968
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions infra/base-images/base-runner/jacoco_report_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,31 @@ def convert(xml):

# Since Java compilation does not track source file location, we match
# coverage info to source files via the full class name, e.g. we search for
# a path in /out/src ending in foo/bar/Baz.java for the class foo.bar.Baz.
# a path in /src ending in foo/bar/Baz.java for the class foo.bar.Baz.
# Under the assumptions that a given project only ever contains a single
# version of a class and that no class name appears as a suffix of another
# class name, we can assign coverage info to every source file matched in that
# way.
src_files = list_src_files()

for class_element in report.findall('./package/class'):
class_name = class_element.attrib['name']
package_name = os.path.dirname(class_name)
# Skip fuzzer classes
if is_fuzzer_class(class_element):
continue

# Skip non class elements
if 'sourcefilename' not in class_element.attrib:
continue

class_name = class_element.attrib['name']
package_name = os.path.dirname(class_name)
basename = class_element.attrib['sourcefilename']
# This path is 'foo/Bar.java' for the class element
# <class name="foo/Bar" sourcefilename="Bar.java">.
canonical_path = os.path.join(package_name, basename)

class_summary = make_element_summary(class_element)
src_files = relative_to_src_path(src_files, canonical_path)
for src_file in src_files:
for src_file in relative_to_src_path(src_files, canonical_path):
summary['data'][0]['files'].append({
'filename': src_file,
'summary': class_summary,
Expand All @@ -68,20 +73,26 @@ def convert(xml):


def list_src_files():
"""Returns a map from basename to full path for all files in $OUT/$SRC."""
"""Returns a map from basename to full path for all files in $SRC."""
filename_to_paths = {}
out_path = os.environ['OUT'] + '/'
src_path = os.environ['SRC']
src_in_out = out_path + src_path
for dirpath, _, filenames in os.walk(src_in_out):
for dirpath, _, filenames in os.walk(src_path):
for filename in filenames:
full_path = dirpath + '/' + filename
# Map /out//src/... to /src/...
src_path = full_path[len(out_path):]
filename_to_paths.setdefault(filename, []).append(src_path)
filename_to_paths.setdefault(filename, []).append(full_path)

return filename_to_paths


def is_fuzzer_class(class_element):
"""Check if the class is fuzzer class."""
method_elements = class_element.find('./method[@name=\"fuzzerTestOneInput\"]')
if method_elements:
return True

return False


def relative_to_src_path(src_files, canonical_path):
"""Returns all paths in src_files ending in canonical_path."""
basename = os.path.basename(canonical_path)
Expand Down

0 comments on commit 5274968

Please sign in to comment.