Skip to content

Commit 6d40a87

Browse files
authored
Merge pull request #343 from py-cov-action/template-fixes-round-2
2 parents 94ec7e6 + e886962 commit 6d40a87

File tree

5 files changed

+140
-18
lines changed

5 files changed

+140
-18
lines changed

coverage_comment/template.py

Lines changed: 21 additions & 10 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
*,
@@ -209,7 +201,7 @@ def select_files(
209201
diff=diff_coverage_file,
210202
previous=previous_coverage_file,
211203
)
212-
has_diff = bool(diff_coverage_file)
204+
has_diff = bool(diff_coverage_file and diff_coverage_file.added_statements)
213205
has_evolution_from_previous = (
214206
previous_coverage_file.info != coverage_file.info
215207
if previous_coverage_file
@@ -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,

coverage_comment/template_files/comment.md.j2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ _This PR does not seem to contain any modification to coverable code._
244244

245245
{%- if max_files and count_files > max_files %}
246246

247-
> [!NOTE]
248-
> The report is truncated to {{ max_files }} files out of {{ count_files }}. To see the full report, please visit the workflow summary page.
247+
_The report is truncated to {{ max_files }} files out of {{ count_files }}. To see the full report, please visit the workflow summary page._
249248

250249
{% endif %}
251250

tests/conftest.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,13 @@ def _(code: str, has_branches: bool = True) -> coverage_module.Coverage:
291291
)
292292
line_number = 0
293293
# (we start at 0 because the first line will be empty for readabilty)
294-
for line in code.splitlines()[1:]:
294+
for line in code.splitlines():
295295
line = line.strip()
296+
if not line:
297+
continue
296298
if line.startswith("# file: "):
297299
current_file = pathlib.Path(line.split("# file: ")[1])
300+
line_number = 0
298301
continue
299302
assert current_file, (line, current_file, code)
300303
line_number += 1
@@ -383,8 +386,10 @@ def _(code: str) -> tuple[coverage_module.Coverage, coverage_module.DiffCoverage
383386
current_file = None
384387
# (we start at 0 because the first line will be empty for readabilty)
385388
line_number = 0
386-
for line in code.splitlines()[1:]:
389+
for line in code.splitlines():
387390
line = line.strip()
391+
if not line:
392+
continue
388393
if line.startswith("# file: "):
389394
new_code += line + "\n"
390395
current_file = pathlib.Path(line.split("# file: ")[1])

tests/unit/test_diff_grouper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def test_group_annotations_more_files(
2424
)
2525

2626
assert list(result) == [
27-
groups.Group(file=pathlib.Path("codebase/code.py"), line_start=6, line_end=8),
28-
groups.Group(
29-
file=pathlib.Path("codebase/other.py"), line_start=17, line_end=17
30-
),
27+
groups.Group(file=pathlib.Path("codebase/code.py"), line_start=5, line_end=8),
28+
groups.Group(file=pathlib.Path("codebase/other.py"), line_start=1, line_end=1),
29+
groups.Group(file=pathlib.Path("codebase/other.py"), line_start=3, line_end=5),
3130
]

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)