Skip to content

Commit

Permalink
Add support for decision coverage metric in text report (#864)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Pieper <Pascal.Pieper@DLR.de>
Co-authored-by: Michael Förderer <40258682+Spacetown@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 24, 2024
1 parent 9037a2b commit 77fdee0
Show file tree
Hide file tree
Showing 31 changed files with 149 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ New features and notable changes:
than several flags.

- The development branch is renamed from ``master`` to ``main``. (:issue:`829`, :issue:`873`)
- Add support for decision coverage metric in text report (:issue:`864`)

Bug fixes and small improvements:

Expand Down
20 changes: 18 additions & 2 deletions gcovr/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def sort_coverage(
covdata: CovData,
sort_key: Literal["filename", "uncovered-number", "uncovered-percent"],
sort_reverse: bool,
by_metric: Literal["line", "branch"],
by_metric: Literal["line", "branch", "decision"],
filename_uses_relative_pathname: bool = False,
) -> List[str]:
"""Sort a coverage dict.
covdata (dict): the coverage dictionary
sort_key ("filename", "uncovered-number", "uncovered-percent"): the values to sort by
sort_reverse (bool): reverse order if True
by_metric ("line", "branch"): select the metric to sort
by_metric ("line", "branch", "decision"): select the metric to sort
filename_uses_relative_pathname (bool): for html, we break down a pathname to the
relative path, but not for other formats.
Expand All @@ -86,6 +86,8 @@ def coverage_stat(key: str) -> CoverageStat:
cov = covdata[key]
if by_metric == "branch":
return cov.branch_coverage()
elif by_metric == "decision":
return cov.decision_coverage()
return cov.line_coverage()

def key_num_uncovered(key: str) -> int:
Expand Down Expand Up @@ -339,6 +341,20 @@ def is_uncovered(self) -> bool:
def has_uncovered_branch(self) -> bool:
return not all(branch.is_covered for branch in self.branches.values())

@property
def has_uncovered_decision(self) -> bool:
if self.decision is None:
return False

if isinstance(self.decision, DecisionCoverageUncheckable):
return False

if isinstance(self.decision, DecisionCoverageConditional):
return self.decision.count_true == 0 or self.decision.count_false == 0

if isinstance(self.decision, DecisionCoverageSwitch):
return self.decision.count == 0

def branch_coverage(self) -> CoverageStat:
total = len(self.branches)
covered = 0
Expand Down
1 change: 0 additions & 1 deletion gcovr/formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(self, options: Options):
"root",
"root_dir",
"root_filter",
"show_decision",
"sort_branches",
"sort_key",
"sort_reverse",
Expand Down
2 changes: 2 additions & 0 deletions gcovr/formats/gcov/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
class GcovHandler(BaseHandler):
def get_options() -> List[GcovrConfigOption]:
return [
# Global options needed for report
"show_decision",
# Global options used for merging end exclusion processing.
"exclude_calls",
"exclude_noncode_lines",
Expand Down
1 change: 1 addition & 0 deletions gcovr/formats/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def get_options() -> List[GcovrConfigOption]:
return [
# Global options needed for report
"exclude_calls",
"show_decision",
# Local options
GcovrConfigOption(
"html",
Expand Down
3 changes: 2 additions & 1 deletion gcovr/formats/txt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ def get_options() -> List[GcovrConfigOption]:
return [
# Global options needed for report
"exclude_calls",
"show_decision", # Only for summary report
# Local options
GcovrConfigOption(
"txt_metric",
["--txt-metric"],
config="txt-metric",
group="output_options",
help=("The metric type to report."),
choices=["line", "branch"],
choices=["line", "branch", "decision"],
default="line",
),
GcovrConfigOption(
Expand Down
38 changes: 34 additions & 4 deletions gcovr/formats/txt/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ def write_report(covdata: CovData, output_file: str, options: Options) -> None:
fh.write("Directory: " + force_unix_separator(options.root) + "\n")

fh.write("-" * LINE_WIDTH + "\n")
title_total = "Branches" if options.txt_metric == "branch" else "Lines"
title_covered = "Taken" if options.txt_metric == "branch" else "Exec"
if options.txt_metric == "branch":
title_total = "Branches"
title_covered = "Taken"
elif options.txt_metric == "decision":
title_total = "Decisions"
title_covered = "Taken"
else:
title_total = "Lines"
title_covered = "Exec"

title_percentage = "Cover"
title_un_covered = "Covered" if options.txt_report_covered else "Missing"
fh.write(
Expand Down Expand Up @@ -120,6 +128,9 @@ def _summarize_file_coverage(coverage: FileCoverage, options):
if options.txt_metric == "branch":
stat = coverage.branch_coverage()
covered_lines = _covered_branches_str(coverage)
elif options.txt_metric == "decision":
stat = coverage.decision_coverage()
covered_lines = _covered_decisions_str(coverage)
else:
stat = coverage.line_coverage()
covered_lines = _covered_lines_str(coverage)
Expand All @@ -129,6 +140,9 @@ def _summarize_file_coverage(coverage: FileCoverage, options):
if options.txt_metric == "branch":
stat = coverage.branch_coverage()
uncovered_lines = _uncovered_branches_str(coverage)
elif options.txt_metric == "decision":
stat = coverage.decision_coverage()
uncovered_lines = _uncovered_decisions_str(coverage)
else:
stat = coverage.line_coverage()
uncovered_lines = _uncovered_lines_str(coverage)
Expand Down Expand Up @@ -202,16 +216,32 @@ def _covered_branches_str(filecov: FileCoverage) -> str:
line.lineno for line in filecov.lines.values() if not line.has_uncovered_branch
)

# Dn't do any aggregation on branch results.
# Don't do any aggregation on branch results.
return ",".join(str(lineno) for lineno in covered_lines)


def _covered_decisions_str(filecov: FileCoverage) -> str:
covered_decisions = sorted(
line.lineno
for line in filecov.lines.values()
if not line.has_uncovered_decision
)
return ",".join(str(lineno) for lineno in covered_decisions)


def _uncovered_decisions_str(filecov: FileCoverage) -> str:
uncovered_decisions = sorted(
line.lineno for line in filecov.lines.values() if line.has_uncovered_decision
)
return ",".join(str(lineno) for lineno in uncovered_decisions)


def _uncovered_branches_str(filecov: FileCoverage) -> str:
uncovered_lines = sorted(
line.lineno for line in filecov.lines.values() if line.has_uncovered_branch
)

# Dn't do any aggregation on branch results.
# Don't do any aggregation on branch results.
return ",".join(str(lineno) for lineno in uncovered_lines)


Expand Down
7 changes: 5 additions & 2 deletions gcovr/tests/decisions-neg-delta/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all:
$(CXX) -std=c++11 -fprofile-arcs -ftest-coverage -O0 main.cpp -o testcase

run: html json
run: html txt json

coverage.json:
./testcase
Expand All @@ -10,10 +10,13 @@ coverage.json:
html: coverage.json
$(GCOVR) --verbose -a coverage.json --decisions --html-details -o coverage.html

txt: coverage.json
$(GCOVR) --verbose -a coverage.json --txt-metric decision -o coverage.txt

json: coverage.json
# pass

clean:
rm -f testcase
rm -f *.gc*
rm -f coverage*.*
rm -f coverage*.*
10 changes: 10 additions & 0 deletions gcovr/tests/decisions-neg-delta/reference/clang-10/coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Decisions Taken Cover Missing
------------------------------------------------------------------------------
main.cpp 4 3 75% 6
------------------------------------------------------------------------------
TOTAL 4 3 75%
------------------------------------------------------------------------------
10 changes: 10 additions & 0 deletions gcovr/tests/decisions-neg-delta/reference/gcc-5/coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Decisions Taken Cover Missing
------------------------------------------------------------------------------
main.cpp 4 3 75% 6
------------------------------------------------------------------------------
TOTAL 4 3 75%
------------------------------------------------------------------------------
5 changes: 4 additions & 1 deletion gcovr/tests/decisions/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all:
$(CXX) -fprofile-arcs -ftest-coverage main.cpp switch_test.cpp -o testcase

run: html json
run: html txt json

coverage.json:
./testcase
Expand All @@ -10,6 +10,9 @@ coverage.json:
html: coverage.json
$(GCOVR) --verbose -a coverage.json --decisions --html-details -o coverage.html

txt: coverage.json
$(GCOVR) --verbose -a coverage.json --txt-metric decision -o coverage.txt

json: coverage.json
# pass

Expand Down
12 changes: 12 additions & 0 deletions gcovr/tests/decisions/reference/clang-10/coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Decisions Taken Cover Missing
------------------------------------------------------------------------------
main.cpp 65 33 50% 10,22,46,58,70,82,94,106,118,130,142,146,158,162,174,178,196,202,214,220,230,233,243,248,345
switch_test.cpp 0 0 --%
switch_test.h 3 1 33% 18,19
------------------------------------------------------------------------------
TOTAL 68 34 50%
------------------------------------------------------------------------------
10 changes: 10 additions & 0 deletions gcovr/tests/decisions/reference/gcc-5/coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Decisions Taken Cover Missing
------------------------------------------------------------------------------
main.cpp 65 33 50% 10,22,46,58,70,82,94,106,118,130,142,146,158,162,174,178,196,202,214,220,230,233,243,248,345
------------------------------------------------------------------------------
TOTAL 65 33 50%
------------------------------------------------------------------------------
12 changes: 12 additions & 0 deletions gcovr/tests/decisions/reference/gcc-6/coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Decisions Taken Cover Missing
------------------------------------------------------------------------------
main.cpp 65 33 50% 10,22,46,58,70,82,94,106,118,130,142,146,158,162,174,178,196,202,214,220,230,233,243,248,345
switch_test.cpp 0 0 --%
switch_test.h 3 1 33% 18,19
------------------------------------------------------------------------------
TOTAL 68 34 50%
------------------------------------------------------------------------------
12 changes: 12 additions & 0 deletions gcovr/tests/decisions/reference/gcc-8/coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Decisions Taken Cover Missing
------------------------------------------------------------------------------
main.cpp 65 33 50% 10,22,46,58,70,82,94,106,118,130,142,146,158,162,174,178,194,200,213,219,229,232,243,248,345
switch_test.cpp 0 0 --%
switch_test.h 3 1 33% 18,19
------------------------------------------------------------------------------
TOTAL 68 34 50%
------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>GCC Code Coverage Report</h1>
</tr>
<tr>
<th scope="row">Date:</th>
<td>2023-03-09 20:14:36+00:00</td>
<td>0000-00-00 00:00:00</td>
</tr>
</table>
</div>
Expand Down
Loading

0 comments on commit 77fdee0

Please sign in to comment.