Skip to content

Commit d1c133f

Browse files
committed
Redo sorting of files to ensure most interesting files come first
1 parent 3695bc5 commit d1c133f

File tree

2 files changed

+128
-9
lines changed

2 files changed

+128
-9
lines changed

coverage_comment/template.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,6 @@ class FileInfo:
108108
diff: coverage_module.FileDiffCoverage | None
109109
previous: coverage_module.FileCoverage | None
110110

111-
@property
112-
def new_missing_lines(self) -> list[int]:
113-
missing_lines = set(self.coverage.missing_lines)
114-
if self.previous:
115-
missing_lines -= set(self.previous.missing_lines)
116-
117-
return sorted(missing_lines)
118-
119111

120112
def get_comment_markdown(
121113
*,
@@ -220,12 +212,31 @@ def select_files(
220212
files.append(file_info)
221213

222214
count_files = len(files)
223-
files = sorted(files, key=lambda x: len(x.new_missing_lines), reverse=True)
215+
files = sorted(files, key=sort_order, reverse=True)
224216
if max_files is not None:
225217
files = files[:max_files]
226218
return sorted(files, key=lambda x: x.path), count_files
227219

228220

221+
def sort_order(file_info: FileInfo) -> tuple[int, int, int]:
222+
"""
223+
Sort order for files:
224+
1. Files with the most new missing lines
225+
2. Files with the most added lines (from the diff)
226+
3. Files with the most new executed lines (including not in the diff)
227+
"""
228+
new_missing_lines = len(file_info.coverage.missing_lines)
229+
if file_info.previous:
230+
new_missing_lines -= len(file_info.previous.missing_lines)
231+
232+
added_statements = len(file_info.diff.added_statements) if file_info.diff else 0
233+
new_covered_lines = len(file_info.coverage.executed_lines)
234+
if file_info.previous:
235+
new_covered_lines -= len(file_info.previous.executed_lines)
236+
237+
return abs(new_missing_lines), added_statements, abs(new_covered_lines)
238+
239+
229240
def get_readme_markdown(
230241
is_public: bool,
231242
readme_url: str,

tests/unit/test_template.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,30 @@ def test_get_marker(marker_id, result):
639639
3,
640640
id="truncated_and_ordered",
641641
),
642+
pytest.param(
643+
"""
644+
# file: a.py
645+
1 covered
646+
# file: c.py
647+
1 covered
648+
# file: b.py
649+
1 covered
650+
""",
651+
"""
652+
# file: a.py
653+
1
654+
2 covered
655+
# file: c.py
656+
+ 1 covered
657+
# file: b.py
658+
1 covered
659+
1 covered
660+
""",
661+
2,
662+
["b.py", "c.py"],
663+
2,
664+
id="truncated_and_ordered_sort_order_advanced",
665+
),
642666
pytest.param(
643667
"""
644668
# file: a.py
@@ -703,6 +727,90 @@ def test_select_files__no_previous(
703727
assert total == 1
704728

705729

730+
@pytest.mark.parametrize(
731+
"previous_code, current_code_and_diff, expected_new_missing, expected_added, expected_new_covered",
732+
[
733+
pytest.param(
734+
"""
735+
# file: a.py
736+
1 covered
737+
2 missing
738+
""",
739+
"""
740+
# file: a.py
741+
+ 1
742+
2 covered
743+
+ 3 missing
744+
+ 4 missing
745+
+ 5 covered
746+
""",
747+
1,
748+
3,
749+
1,
750+
id="added_code",
751+
),
752+
pytest.param(
753+
"""
754+
# file: a.py
755+
1 covered
756+
2 missing
757+
3 covered
758+
4 missing
759+
""",
760+
"""
761+
# file: a.py
762+
+ 1 missing
763+
""",
764+
1,
765+
1,
766+
2,
767+
id="removed_code",
768+
),
769+
],
770+
)
771+
def test_sort_order(
772+
make_coverage_and_diff,
773+
make_coverage,
774+
previous_code,
775+
current_code_and_diff,
776+
expected_new_missing,
777+
expected_added,
778+
expected_new_covered,
779+
):
780+
previous_cov = make_coverage(previous_code)
781+
cov, diff_cov = make_coverage_and_diff(current_code_and_diff)
782+
path = pathlib.Path("a.py")
783+
file_info = template.FileInfo(
784+
path=path,
785+
coverage=cov.files[path],
786+
diff=diff_cov.files[path],
787+
previous=previous_cov.files[path],
788+
)
789+
new_missing, added, new_covered = template.sort_order(file_info=file_info)
790+
assert new_missing == expected_new_missing
791+
assert added == expected_added
792+
assert new_covered == expected_new_covered
793+
794+
795+
def test_sort_order__none(make_coverage):
796+
cov = make_coverage(
797+
"""
798+
# file: a.py
799+
1 covered
800+
"""
801+
)
802+
file_info = template.FileInfo(
803+
path=pathlib.Path("a.py"),
804+
coverage=cov.files[pathlib.Path("a.py")],
805+
diff=None,
806+
previous=None,
807+
)
808+
new_missing, added, new_covered = template.sort_order(file_info=file_info)
809+
assert new_missing == 0
810+
assert added == 0
811+
assert new_covered == 1
812+
813+
706814
def test_get_readme_markdown():
707815
result = template.get_readme_markdown(
708816
is_public=True,

0 commit comments

Comments
 (0)