Skip to content

Commit

Permalink
Add covered lines in json report. (#325)
Browse files Browse the repository at this point in the history
measured lines = covered lines + violation lines.

total lines = measured lines + unmeasured lines.

Signed-off-by: ZSmallX <[email protected]>
  • Loading branch information
ZSmallX authored Feb 22, 2023
1 parent 7d5b22e commit 95e7f0e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions diff_cover/report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ def percent_covered(self, src_path):

return None

def covered_lines(self, src_path):
"""
Returns a list of lines covered in measured lines (integers)
in `src_path` that were changed.
If we have no coverage information for
`src_path`, returns an empty list.
"""
diff_violations = self._diff_violations().get(src_path)

if diff_violations is None:
return []

return sorted(
set(diff_violations.measured_lines).difference(
set(self.violation_lines(src_path))
)
)

def violation_lines(self, src_path):
"""
Return a list of lines in violation (integers)
Expand Down Expand Up @@ -213,13 +232,16 @@ def _src_path_stats(self, src_path):
Return a dict of statistics for the source file at `src_path`.
"""

covered_lines = self.covered_lines(src_path)

# Find violation lines
violation_lines = self.violation_lines(src_path)
violations = sorted(self._diff_violations()[src_path].violations)

return {
"percent_covered": self.percent_covered(src_path),
"violation_lines": violation_lines,
"covered_lines": covered_lines,
"violations": violations,
}

Expand Down
3 changes: 3 additions & 0 deletions tests/test_report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,13 @@ def test_generate_report(self):
"diff_name": "main",
"src_stats": {
"file1.py": {
"covered_lines": [2, 3, 4, 15],
"percent_covered": 66.66666666666667,
"violation_lines": [10, 11],
"violations": [[10, None], [11, None]],
},
"subdir/file2.py": {
"covered_lines": [2, 3, 4, 15],
"percent_covered": 66.66666666666667,
"violation_lines": [10, 11],
"violations": [[10, None], [11, None]],
Expand Down Expand Up @@ -312,6 +314,7 @@ def test_hundred_percent(self):
"diff_name": "main",
"src_stats": {
"file.py": {
"covered_lines": [2],
"percent_covered": 100.0,
"violation_lines": [],
"violations": [],
Expand Down

0 comments on commit 95e7f0e

Please sign in to comment.