From 81d14be459cb9e0a75f1f592d745c4c8bdea411f Mon Sep 17 00:00:00 2001 From: Pradeep Tammali Date: Sat, 22 Jun 2024 22:29:32 +0200 Subject: [PATCH] fix: merge the missing groups with max annotation gap --- codecov/diff_grouper.py | 33 +++++++++++++++++++++------------ codecov/main.py | 2 ++ tests/test_diff_grouper.py | 10 +++++++--- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/codecov/diff_grouper.py b/codecov/diff_grouper.py index 9b69197..fd2b176 100644 --- a/codecov/diff_grouper.py +++ b/codecov/diff_grouper.py @@ -57,18 +57,7 @@ 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, @@ -76,6 +65,26 @@ def get_branch_missing_groups( ) +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, diff --git a/codecov/main.py b/codecov/main.py index 0a70acb..3857045 100644 --- a/codecov/main.py +++ b/codecov/main.py @@ -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) diff --git a/tests/test_diff_grouper.py b/tests/test_diff_grouper.py index 5fbce7c..4d89a4b 100644 --- a/tests/test_diff_grouper.py +++ b/tests/test_diff_grouper.py @@ -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), ] @@ -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), ]