Skip to content

Commit

Permalink
fix: merge the missing groups with max annotation gap (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ideepu authored Jun 22, 2024
1 parent 6967d30 commit 0ee8cda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
33 changes: 21 additions & 12 deletions codecov/diff_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,34 @@ def get_branch_missing_groups(
) -> Iterable[groups.Group]:
for path, _ in diff_coverage.files.items():
coverage_file = coverage.files[path]
separators = {
*flatten_branches(coverage_file.executed_branches),
*coverage_file.excluded_lines,
}
joiners = set(range(1, coverage_file.info.num_statements)) - separators

for start, end in groups.compute_contiguous_groups(
values=flatten_branches(branches=coverage_file.missing_branches),
separators=separators,
joiners=joiners,
max_gap=MAX_ANNOTATION_GAP,
):
for start, end in coverage_file.missing_branches or []:
yield groups.Group(
file=path,
line_start=start,
line_end=end,
)


def group_branches(coverage: coverage_module.Coverage) -> coverage_module.Coverage:
for file_coverage in coverage.files.values():
separators = {
*flatten_branches(file_coverage.executed_branches),
*file_coverage.excluded_lines,
}
joiners = set(range(1, file_coverage.info.num_statements)) - separators

file_coverage.missing_branches = [
[start, end]
for start, end in groups.compute_contiguous_groups(
values=flatten_branches(branches=file_coverage.missing_branches),
separators=separators,
joiners=joiners,
max_gap=MAX_ANNOTATION_GAP,
)
]
return coverage


def get_diff_missing_groups(
coverage: coverage_module.Coverage,
diff_coverage: coverage_module.DiffCoverage,
Expand Down
2 changes: 2 additions & 0 deletions codecov/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def process_pr( # pylint: disable=too-many-locals
pr_number: int,
) -> int:
coverage = coverage_module.get_coverage_info(coverage_path=config.COVERAGE_PATH)
if config.BRANCH_COVERAGE:
coverage = diff_grouper.group_branches(coverage=coverage)
base_ref = config.GITHUB_BASE_REF or repo_info.default_branch
pr_diff = github.get_pr_diff(github=gh, repository=config.GITHUB_REPOSITORY, pr_number=pr_number)
added_lines = coverage_module.parse_diff_output(diff=pr_diff)
Expand Down
10 changes: 7 additions & 3 deletions tests/test_diff_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def test_group_branch_annotations(coverage_obj, diff_coverage_obj):
result = diff_grouper.get_branch_missing_groups(coverage=coverage_obj, diff_coverage=diff_coverage_obj)

assert list(result) == [
groups.Group(file=pathlib.Path('codebase/code.py'), line_start=5, line_end=11),
groups.Group(file=pathlib.Path('codebase/code.py'), line_start=5, line_end=6),
groups.Group(file=pathlib.Path('codebase/code.py'), line_start=10, line_end=11),
]


Expand All @@ -76,6 +77,9 @@ def test_group_branch_annotations_more_files(coverage_obj_more_files, diff_cover
)

assert list(result) == [
groups.Group(file=pathlib.Path('codebase/code.py'), line_start=4, line_end=8),
groups.Group(file=pathlib.Path('codebase/other.py'), line_start=2, line_end=9),
groups.Group(file=pathlib.Path('codebase/code.py'), line_start=4, line_end=5),
groups.Group(file=pathlib.Path('codebase/code.py'), line_start=7, line_end=8),
groups.Group(file=pathlib.Path('codebase/other.py'), line_start=2, line_end=3),
groups.Group(file=pathlib.Path('codebase/other.py'), line_start=4, line_end=5),
groups.Group(file=pathlib.Path('codebase/other.py'), line_start=8, line_end=9),
]

0 comments on commit 0ee8cda

Please sign in to comment.