2020-03-25 03:57:53 +00:00
|
|
|
#!/usr/bin/env python
|
2019-04-18 17:51:19 +00:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
2020-03-25 03:57:53 +00:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import optparse
|
2013-08-01 20:59:01 +00:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# the gcov report follows certain pattern. Each file will have two lines
|
|
|
|
# of report, from which we can extract the file name, total lines and coverage
|
|
|
|
# percentage.
|
|
|
|
def parse_gcov_report(gcov_input):
|
|
|
|
per_file_coverage = {}
|
|
|
|
total_coverage = None
|
|
|
|
|
|
|
|
for line in sys.stdin:
|
|
|
|
line = line.strip()
|
|
|
|
|
|
|
|
# --First line of the coverage report (with file name in it)?
|
|
|
|
match_obj = re.match("^File '(.*)'$", line)
|
|
|
|
if match_obj:
|
|
|
|
# fetch the file name from the first line of the report.
|
|
|
|
current_file = match_obj.group(1)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# -- Second line of the file report (with coverage percentage)
|
|
|
|
match_obj = re.match("^Lines executed:(.*)% of (.*)", line)
|
|
|
|
|
|
|
|
if match_obj:
|
|
|
|
coverage = float(match_obj.group(1))
|
|
|
|
lines = int(match_obj.group(2))
|
|
|
|
|
|
|
|
if current_file is not None:
|
|
|
|
per_file_coverage[current_file] = (coverage, lines)
|
|
|
|
current_file = None
|
|
|
|
else:
|
|
|
|
# If current_file is not set, we reach the last line of report,
|
|
|
|
# which contains the summarized coverage percentage.
|
|
|
|
total_coverage = (coverage, lines)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# If the line's pattern doesn't fall into the above categories. We
|
|
|
|
# can simply ignore them since they're either empty line or doesn't
|
|
|
|
# find executable lines of the given file.
|
|
|
|
current_file = None
|
|
|
|
|
|
|
|
return per_file_coverage, total_coverage
|
|
|
|
|
2022-09-21 00:47:52 +00:00
|
|
|
|
2013-08-01 20:59:01 +00:00
|
|
|
def get_option_parser():
|
2022-09-21 00:47:52 +00:00
|
|
|
usage = (
|
|
|
|
"Parse the gcov output and generate more human-readable code "
|
|
|
|
+ "coverage report."
|
|
|
|
)
|
2020-03-25 03:57:53 +00:00
|
|
|
parser = optparse.OptionParser(usage)
|
2013-08-01 20:59:01 +00:00
|
|
|
|
|
|
|
parser.add_option(
|
2022-09-21 00:47:52 +00:00
|
|
|
"--interested-files",
|
|
|
|
"-i",
|
2013-08-01 20:59:01 +00:00
|
|
|
dest="filenames",
|
2022-09-21 00:47:52 +00:00
|
|
|
help="Comma separated files names. if specified, we will display "
|
|
|
|
+ "the coverage report only for interested source files. "
|
|
|
|
+ "Otherwise we will display the coverage report for all "
|
|
|
|
+ "source files.",
|
2013-08-01 20:59:01 +00:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
2022-09-21 00:47:52 +00:00
|
|
|
|
2013-08-01 20:59:01 +00:00
|
|
|
def display_file_coverage(per_file_coverage, total_coverage):
|
|
|
|
# To print out auto-adjustable column, we need to know the longest
|
|
|
|
# length of file names.
|
2022-09-21 00:47:52 +00:00
|
|
|
max_file_name_length = max(len(fname) for fname in per_file_coverage.keys())
|
2013-08-01 20:59:01 +00:00
|
|
|
|
|
|
|
# -- Print header
|
|
|
|
# size of separator is determined by 3 column sizes:
|
|
|
|
# file name, coverage percentage and lines.
|
2022-09-21 00:47:52 +00:00
|
|
|
header_template = "%" + str(max_file_name_length) + "s\t%s\t%s"
|
2013-08-01 20:59:01 +00:00
|
|
|
separator = "-" * (max_file_name_length + 10 + 20)
|
2022-09-21 00:47:52 +00:00
|
|
|
print(
|
|
|
|
header_template % ("Filename", "Coverage", "Lines")
|
|
|
|
) # noqa: E999 T25377293 Grandfathered in
|
2020-03-25 03:57:53 +00:00
|
|
|
print(separator)
|
2013-08-01 20:59:01 +00:00
|
|
|
|
|
|
|
# -- Print body
|
|
|
|
# template for printing coverage report for each file.
|
|
|
|
record_template = "%" + str(max_file_name_length) + "s\t%5.2f%%\t%10d"
|
|
|
|
|
|
|
|
for fname, coverage_info in per_file_coverage.items():
|
|
|
|
coverage, lines = coverage_info
|
2020-03-25 03:57:53 +00:00
|
|
|
print(record_template % (fname, coverage, lines))
|
2013-08-01 20:59:01 +00:00
|
|
|
|
|
|
|
# -- Print footer
|
|
|
|
if total_coverage:
|
2020-03-25 03:57:53 +00:00
|
|
|
print(separator)
|
|
|
|
print(record_template % ("Total", total_coverage[0], total_coverage[1]))
|
2013-08-01 20:59:01 +00:00
|
|
|
|
2022-09-21 00:47:52 +00:00
|
|
|
|
2013-08-01 20:59:01 +00:00
|
|
|
def report_coverage():
|
|
|
|
parser = get_option_parser()
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
interested_files = set()
|
|
|
|
if options.filenames is not None:
|
2022-09-21 00:47:52 +00:00
|
|
|
interested_files = {f.strip() for f in options.filenames.split(",")}
|
2013-08-01 20:59:01 +00:00
|
|
|
|
|
|
|
# To make things simple, right now we only read gcov report from the input
|
|
|
|
per_file_coverage, total_coverage = parse_gcov_report(sys.stdin)
|
|
|
|
|
|
|
|
# Check if we need to display coverage info for interested files.
|
|
|
|
if len(interested_files):
|
|
|
|
per_file_coverage = dict(
|
2022-09-21 00:47:52 +00:00
|
|
|
(fname, per_file_coverage[fname])
|
|
|
|
for fname in interested_files
|
2013-08-01 20:59:01 +00:00
|
|
|
if fname in per_file_coverage
|
|
|
|
)
|
|
|
|
# If we only interested in several files, it makes no sense to report
|
|
|
|
# the total_coverage
|
|
|
|
total_coverage = None
|
|
|
|
|
|
|
|
if not len(per_file_coverage):
|
2020-03-25 03:57:53 +00:00
|
|
|
print("Cannot find coverage info for the given files.", file=sys.stderr)
|
2013-08-01 20:59:01 +00:00
|
|
|
return
|
|
|
|
display_file_coverage(per_file_coverage, total_coverage)
|
|
|
|
|
2022-09-21 00:47:52 +00:00
|
|
|
|
2013-08-01 20:59:01 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
report_coverage()
|