Skip to content

Commit

Permalink
Add --fail-under-line and --fail-under-branch options to return exit …
Browse files Browse the repository at this point in the history
…status 2 under a threshold
  • Loading branch information
mayeut committed Jul 25, 2017
1 parent 16c81cc commit 0ddcde6
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions scripts/gcovr
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
)
Expand All @@ -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
#
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 " \
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 0ddcde6

Please sign in to comment.