Skip to content

Commit

Permalink
Fix associated tests & add missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Sep 10, 2023
1 parent 4ed6e55 commit cb694a7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
11 changes: 5 additions & 6 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def process_pr(
github.add_job_summary(
content=comment, github_step_summary=config.GITHUB_STEP_SUMMARY
)

pr_number: int | None = config.GITHUB_PR_NUMBER
if pr_number is None:
# If we don't have a PR number, we're launched from a push event,
Expand All @@ -183,11 +182,11 @@ def process_pr(
)
except github.CannotDeterminePR:
pr_number = None
else:
if config.ANNOTATE_MISSING_LINES:
annotations.create_pr_annotations(
annotation_type=config.ANNOTATION_TYPE, diff_coverage=diff_coverage
)

if pr_number is not None and config.ANNOTATE_MISSING_LINES:
annotations.create_pr_annotations(
annotation_type=config.ANNOTATION_TYPE, diff_coverage=diff_coverage
)

try:
if config.FORCE_WORKFLOW_RUN or not pr_number:
Expand Down
4 changes: 2 additions & 2 deletions coverage_comment/template_files/comment.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ The coverage rate went from `{{ previous_coverage_rate | pct }}` to `{{ coverage
> [!NOTE]
> Coverage evolution disabled because this PR targets a different branch
> than the default branch, for which coverage data is not available.
{%- endblock no_comparison_info_non_default_target -%}
{% endif %}
{%- endblock no_comparison_info_non_default_target %}
{%- endif %}
{%- endblock no_comparison_info %}

{% block coverage_value_wording -%}
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ markers = [
"repo_suffix: Allows to use an additional suffix for the e2e test repo.",
"code_path: Allows to place the code in a subdirectory for the e2e test repo.",
"subproject_id: Allows to use a different subproject id for the e2e test repo.",
"add_branches: Adds branches besides 'main' and 'branch' to integration tests setup.",
]

[tool.coverage.run]
Expand Down
68 changes: 65 additions & 3 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,20 @@ def _():


@pytest.fixture
def integration_env(integration_dir, write_file, run_coverage, commit):
def integration_env(integration_dir, write_file, run_coverage, commit, request):
subprocess.check_call(["git", "init", "-b", "main"], cwd=integration_dir)
# diff coverage reads the "origin/{...}" branch so we simulate an origin remote
subprocess.check_call(["git", "remote", "add", "origin", "."], cwd=integration_dir)
write_file("A", "B")

commit()

add_branch_mark = request.node.get_closest_marker("add_branches")
for additional_branch in add_branch_mark.args if add_branch_mark else []:
subprocess.check_call(
["git", "switch", "-c", additional_branch],
cwd=integration_dir,
)

subprocess.check_call(
["git", "switch", "-c", "branch"],
cwd=integration_dir,
Expand Down Expand Up @@ -160,7 +167,7 @@ def checker(payload):
comment_file = pathlib.Path("python-coverage-comment-action.txt").read_text()
assert comment == comment_file
assert comment == summary_file.read_text()
assert "No coverage data of the default branch was found for comparison" in comment
assert "Coverage data for the default branch was not found." in comment
assert "The coverage rate is `77.77%`" in comment
assert "`75%` of new lines are covered." in comment
assert (
Expand All @@ -177,6 +184,61 @@ def checker(payload):
assert output_file.read_text() == expected_output


@pytest.mark.add_branches("foo")
def test_action__pull_request__store_comment_not_targeting_default(
pull_request_config, session, in_integration_env, output_file, summary_file, capsys
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
)
payload = json.dumps({"coverage": 30.00})

session.register(
"GET",
"/repos/py-cov-action/foobar/contents/data.json",
)(json={"content": base64.b64encode(payload.encode()).decode()})

# Who am I
session.register("GET", "/user")(json={"login": "foo"})
# Are there already comments
session.register("GET", "/repos/py-cov-action/foobar/issues/2/comments")(json=[])

comment = None

def checker(payload):
body = payload["body"]
assert "## Coverage report" in body
nonlocal comment
comment = body
return True

# Post a new comment
session.register(
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
)(status_code=403)

result = main.action(
config=pull_request_config(
GITHUB_OUTPUT=output_file,
GITHUB_STEP_SUMMARY=summary_file,
GITHUB_BASE_REF="foo",
),
github_session=session,
http_session=session,
git=None,
)
assert result == 0

# Check that no annotations were made
output = capsys.readouterr()
assert output.err.strip() == ""

comment_file = pathlib.Path("python-coverage-comment-action.txt").read_text()
assert comment == comment_file
assert comment == summary_file.read_text()
assert "Coverage evolution disabled because this PR targets" in comment


def test_action__pull_request__post_comment(
pull_request_config, session, in_integration_env, output_file, summary_file
):
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ def test_template__no_branch_no_previous(coverage_obj_no_branch, diff_coverage_o
base_template=template.read_template_file("comment.md.j2"),
)
expected = """## Coverage report
> **Note**
> No coverage data of the default branch was found for comparison. A possible reason for this is that the coverage action has not yet run after a push event and the data is therefore not yet initialized.
> [!NOTE]
> Coverage data for the default branch was not found.
> This usually happens when the action has not run on the default
> branch yet, for example right after deploying it into the workflows.
The coverage rate is `75%`.
Expand Down

0 comments on commit cb694a7

Please sign in to comment.