From 0ddcde6e9aa6340fa61f64dcfceae56199dbdbd3 Mon Sep 17 00:00:00 2001 From: mayeut Date: Sun, 23 Jul 2017 00:44:50 +0200 Subject: [PATCH] Add --fail-under-line and --fail-under-branch options to return exit status 2 under a threshold --- scripts/gcovr | 66 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/scripts/gcovr b/scripts/gcovr index a7b733eb0..eec77971c 100755 --- a/scripts/gcovr +++ b/scripts/gcovr @@ -989,7 +989,7 @@ def print_text_report(covdata): OUTPUT.write(" " * 27 + "GCC Code Coverage Report\n") if options.root is not None: OUTPUT.write("Directory: " + options.root + "\n") - + OUTPUT.write("-" * 78 + '\n') a = options.show_branch and "Branches" or "Lines" b = options.show_branch and "Taken" or "Exec" @@ -1025,11 +1025,10 @@ def print_text_report(covdata): if options.output: OUTPUT.close() - # -# Prints a small report to the standard output +# Get global statistics # -def print_summary(covdata): +def get_global_stats(covdata): lines_total = 0 lines_covered = 0 branches_total = 0 @@ -1052,6 +1051,17 @@ def print_summary(covdata): percent_branches = branches_total and \ (100.0 * branches_covered / branches_total) + return (lines_total, lines_covered, percent, + branches_total, branches_covered, percent_branches) + +# +# Prints a small report to the standard output +# +def print_summary(covdata): + (lines_total, lines_covered, percent, + branches_total, branches_covered, + percent_branches) = get_global_stats(covdata) + lines_out = "lines: %0.1f%% (%s out of %s)\n" % ( percent, lines_covered, lines_total ) @@ -1062,6 +1072,19 @@ def print_summary(covdata): sys.stdout.write(lines_out) sys.stdout.write(branches_out) +# +# Exits with status 2 if below threshold +# +def fail_under(covdata, threshold_line, threshold_branch): + (lines_total, lines_covered, percent, + branches_total, branches_covered, + percent_branches) = get_global_stats(covdata) + + if percent < threshold_line: + sys.exit(2) + if percent_branches < threshold_branch: + sys.exit(2) + # # CSS declarations for the HTML output # @@ -1264,19 +1287,19 @@ css = Template(''' border-right: 1px gray solid; background-color: lightgray; } - + span.takenBranch { color: ${takenBranch_color}!important; cursor: help; } - + span.notTakenBranch { color: ${notTakenBranch_color}!important; cursor: help; } - + .src { padding-left: 12px; @@ -1593,7 +1616,7 @@ def print_html_report(covdata, details): else: cdata._sourcefile = \ ttmp[0] + '.' + longname + longname_hash + '.html' - # we add a hash at the end and attempt to shorten the + # we add a hash at the end and attempt to shorten the # filename if it exceeds common filesystem limitations if len(os.path.basename(cdata._sourcefile)) < 256: break @@ -2225,6 +2248,26 @@ parser.add_option( dest="print_summary", default=False ) +parser.add_option( + "--fail-under-line", + type="float", + metavar="MIN", + help="Exit with a status of 2 if the total line coverage is less " + "than MIN.", + action="store", + dest="fail_under_line", + default=0.0 +) +parser.add_option( + "--fail-under-branch", + type="float", + metavar="MIN", + help="Exit with a status of 2 if the total branch coverage is less " + "than MIN.", + action="store", + dest="fail_under_branch", + default=0.0 +) parser.usage = "gcovr [options]" parser.description = \ "A utility to run gcov and generate a simple report that summarizes " \ @@ -2314,10 +2357,10 @@ if len(args) == 1: search_paths = ["."] else: search_paths = [options.root] - + if options.objdir is not None: search_paths.append(options.objdir) - + datafiles = get_datafiles(search_paths, options) else: datafiles = get_datafiles(args[1:], options) @@ -2346,3 +2389,6 @@ else: if options.print_summary: print_summary(covdata) + +if options.fail_under_line > 0.0 or options.fail_under_branch > 0.0: + fail_under(covdata, options.fail_under_line, options.fail_under_branch)