From cb694a7d26a9d95215380db11859599b8162d62a Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 10 Sep 2023 13:40:42 +0200 Subject: [PATCH] Fix associated tests & add missing coverage --- coverage_comment/main.py | 11 ++- coverage_comment/template_files/comment.md.j2 | 4 +- pyproject.toml | 1 + tests/integration/test_main.py | 68 ++++++++++++++++++- tests/unit/test_template.py | 6 +- 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/coverage_comment/main.py b/coverage_comment/main.py index 41efbf07..8d55ea29 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -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, @@ -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: diff --git a/coverage_comment/template_files/comment.md.j2 b/coverage_comment/template_files/comment.md.j2 index 1151d57c..860911e4 100644 --- a/coverage_comment/template_files/comment.md.j2 +++ b/coverage_comment/template_files/comment.md.j2 @@ -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 -%} diff --git a/pyproject.toml b/pyproject.toml index a757c43e..85139034 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index f25f438f..123d569a 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -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, @@ -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 ( @@ -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 ): diff --git a/tests/unit/test_template.py b/tests/unit/test_template.py index 78a298b6..52bda8db 100644 --- a/tests/unit/test_template.py +++ b/tests/unit/test_template.py @@ -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%`.