Skip to content

Commit

Permalink
Merge pull request #212 from py-cov-action/annotations-on-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Jul 27, 2023
2 parents 4295553 + ccfad01 commit fdf2f2d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 121 deletions.
12 changes: 8 additions & 4 deletions coverage_comment/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
MISSING_LINES_GROUP_TITLE = "Annotations of lines with missing coverage"


def create_pr_annotations(annotation_type: str, coverage: coverage_module.Coverage):
def create_pr_annotations(
annotation_type: str, diff_coverage: coverage_module.DiffCoverage
):
github.send_workflow_command(
command="group", command_value=MISSING_LINES_GROUP_TITLE
)

for filename, file_coverage in coverage.files.items():
for missing_line in file_coverage.missing_lines:
for file_path, file_diff_coverage in diff_coverage.files.items():
for missing_line in file_diff_coverage.violation_lines:
github.create_missing_coverage_annotation(
annotation_type=annotation_type, file=filename, line=missing_line
annotation_type=annotation_type,
file=file_path,
line=missing_line,
)

github.send_workflow_command(command="endgroup", command_value="")
14 changes: 7 additions & 7 deletions coverage_comment/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CoverageInfo:

@dataclasses.dataclass
class FileCoverage:
path: str
path: pathlib.Path
executed_lines: list[int]
missing_lines: list[int]
excluded_lines: list[int]
Expand All @@ -42,12 +42,12 @@ class FileCoverage:
class Coverage:
meta: CoverageMetadata
info: CoverageInfo
files: dict[str, FileCoverage]
files: dict[pathlib.Path, FileCoverage]


@dataclasses.dataclass
class FileDiffCoverage:
path: str
path: pathlib.Path
percent_covered: decimal.Decimal
violation_lines: list[int]

Expand Down Expand Up @@ -146,8 +146,8 @@ def extract_info(data) -> Coverage:
show_contexts=data["meta"]["show_contexts"],
),
files={
path: FileCoverage(
path=path,
pathlib.Path(path): FileCoverage(
path=pathlib.Path(path),
excluded_lines=file_data["excluded_lines"],
executed_lines=file_data["executed_lines"],
missing_lines=file_data["missing_lines"],
Expand Down Expand Up @@ -235,8 +235,8 @@ def extract_diff_info(data) -> DiffCoverage:
),
num_changed_lines=data["num_changed_lines"],
files={
path: FileDiffCoverage(
path=path,
pathlib.Path(path): FileDiffCoverage(
path=pathlib.Path(path),
percent_covered=decimal.Decimal(str(file_data["percent_covered"]))
/ decimal.Decimal("100"),
violation_lines=file_data["violation_lines"],
Expand Down
8 changes: 6 additions & 2 deletions coverage_comment/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ def send_workflow_command(command: str, command_value: str, **kwargs: str) -> No
)


def create_missing_coverage_annotation(annotation_type: str, file: str, line: int):
def create_missing_coverage_annotation(
annotation_type: str, file: pathlib.Path, line: int
):
send_workflow_command(
command=annotation_type,
command_value=MISSING_COVERAGE_MESSAGE,
file=file,
# This will produce \ paths when running on windows.
# GHA doc is unclear whether this is right or not.
file=str(file),
line=str(line),
)

Expand Down
12 changes: 7 additions & 5 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ def action(

if event_name in {"pull_request", "push"}:
coverage = coverage_module.get_coverage_info(merge=config.MERGE_COVERAGE_FILES)

if event_name == "pull_request":
diff_coverage = coverage_module.get_diff_coverage_info(
base_ref=config.GITHUB_BASE_REF
)
if config.ANNOTATE_MISSING_LINES:
annotations.create_pr_annotations(
annotation_type=config.ANNOTATION_TYPE, coverage=coverage
annotation_type=config.ANNOTATION_TYPE, diff_coverage=diff_coverage
)

return generate_comment(
config=config,
coverage=coverage,
diff_coverage=diff_coverage,
github_session=github_session,
)
else:
Expand All @@ -105,15 +109,13 @@ def action(
def generate_comment(
config: settings.Config,
coverage: coverage_module.Coverage,
diff_coverage: coverage_module.DiffCoverage,
github_session: httpx.Client,
) -> int:
log.info("Generating comment for PR")

gh = github_client.GitHub(session=github_session)

diff_coverage = coverage_module.get_diff_coverage_info(
base_ref=config.GITHUB_BASE_REF
)
previous_coverage_data_file = storage.get_datafile_contents(
github=gh,
repository=config.GITHUB_REPOSITORY,
Expand Down
85 changes: 24 additions & 61 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def coverage_obj():
missing_branches=1,
),
files={
"codebase/code.py": coverage_module.FileCoverage(
path="codebase/code.py",
pathlib.Path("codebase/code.py"): coverage_module.FileCoverage(
path=pathlib.Path("codebase/code.py"),
executed_lines=[1, 2, 5, 6, 9],
missing_lines=[7, 9],
excluded_lines=[],
Expand Down Expand Up @@ -195,8 +195,8 @@ def coverage_obj_no_branch():
missing_branches=None,
),
files={
"codebase/code.py": coverage_module.FileCoverage(
path="codebase/code.py",
pathlib.Path("codebase/code.py"): coverage_module.FileCoverage(
path=pathlib.Path("codebase/code.py"),
executed_lines=[1, 2, 5, 6, 9],
missing_lines=[7],
excluded_lines=[],
Expand All @@ -217,77 +217,40 @@ def coverage_obj_no_branch():


@pytest.fixture
def coverage_obj_many_missing_lines():
return coverage_module.Coverage(
meta=coverage_module.CoverageMetadata(
version="1.2.3",
timestamp=datetime.datetime(2000, 1, 1),
branch_coverage=True,
show_contexts=False,
),
info=coverage_module.CoverageInfo(
covered_lines=7,
num_statements=10,
percent_covered=decimal.Decimal("0.8"),
missing_lines=12,
excluded_lines=0,
num_branches=2,
num_partial_branches=1,
covered_branches=1,
missing_branches=1,
),
def diff_coverage_obj():
return coverage_module.DiffCoverage(
total_num_lines=5,
total_num_violations=1,
total_percent_covered=decimal.Decimal("0.8"),
num_changed_lines=39,
files={
"codebase/main.py": coverage_module.FileCoverage(
path="codebase/main.py",
executed_lines=[1, 2, 5, 6, 9],
missing_lines=[3, 7, 13, 21, 123],
excluded_lines=[],
info=coverage_module.CoverageInfo(
covered_lines=5,
num_statements=10,
percent_covered=decimal.Decimal("0.5"),
missing_lines=5,
excluded_lines=0,
num_branches=2,
num_partial_branches=1,
covered_branches=1,
missing_branches=1,
),
),
"codebase/caller.py": coverage_module.FileCoverage(
path="codebase/caller.py",
executed_lines=[1, 2, 5],
missing_lines=[13, 21, 212],
excluded_lines=[],
info=coverage_module.CoverageInfo(
covered_lines=3,
num_statements=6,
percent_covered=decimal.Decimal("0.5"),
missing_lines=3,
excluded_lines=0,
num_branches=2,
num_partial_branches=1,
covered_branches=1,
missing_branches=1,
),
),
pathlib.Path("codebase/code.py"): coverage_module.FileDiffCoverage(
path=pathlib.Path("codebase/code.py"),
percent_covered=decimal.Decimal("0.8"),
violation_lines=[7, 9],
)
},
)


@pytest.fixture
def diff_coverage_obj():
def diff_coverage_obj_many_missing_lines():
return coverage_module.DiffCoverage(
total_num_lines=5,
total_num_violations=1,
total_percent_covered=decimal.Decimal("0.8"),
num_changed_lines=39,
files={
"codebase/code.py": coverage_module.FileDiffCoverage(
path="codebase/code.py",
pathlib.Path("codebase/code.py"): coverage_module.FileDiffCoverage(
path=pathlib.Path("codebase/code.py"),
percent_covered=decimal.Decimal("0.8"),
violation_lines=[7, 9],
)
),
pathlib.Path("codebase/main.py"): coverage_module.FileDiffCoverage(
path=pathlib.Path("codebase/code.py"),
percent_covered=decimal.Decimal("0.8"),
violation_lines=[1, 2, 8, 17],
),
},
)

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/test_github.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pathlib

import pytest

from coverage_comment import github
Expand Down Expand Up @@ -315,7 +317,7 @@ def test_send_workflow_command(capsys):

def test_create_missing_coverage_annotation(capsys):
github.create_missing_coverage_annotation(
annotation_type="warning", file="test.py", line=42
annotation_type="warning", file=pathlib.Path("test.py"), line=42
)
output = capsys.readouterr()
assert (
Expand All @@ -326,7 +328,7 @@ def test_create_missing_coverage_annotation(capsys):

def test_create_missing_coverage_annotation__annotation_type(capsys):
github.create_missing_coverage_annotation(
annotation_type="error", file="test.py", line=42
annotation_type="error", file=pathlib.Path("test.py"), line=42
)
output = capsys.readouterr()
assert (
Expand Down
Loading

0 comments on commit fdf2f2d

Please sign in to comment.