Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add covered lines in json report. #325

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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