Skip to content

Commit

Permalink
Install new function and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Sep 17, 2023
1 parent f419af0 commit 4b58f87
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
7 changes: 5 additions & 2 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def action(
config=config,
gh=gh,
repo_info=repo_info,
git=git,
)

else:
Expand All @@ -107,6 +108,7 @@ def process_pr(
config: settings.Config,
gh: github_client.GitHub,
repo_info: github.RepositoryInfo,
git: subprocess.Git,
) -> int:
log.info("Generating comment for PR")

Expand All @@ -115,10 +117,11 @@ def process_pr(
coverage_path=config.COVERAGE_PATH,
)
base_ref = config.GITHUB_BASE_REF or repo_info.default_branch

added_lines = coverage_module.get_added_lines(git=git, base_ref=base_ref)
diff_coverage = coverage_module.get_diff_coverage_info(
base_ref=base_ref, coverage_path=config.COVERAGE_PATH
coverage=coverage, added_lines=added_lines
)

# It only really makes sense to display a comparison with the previous
# coverage if the PR target is the branch in which the coverage data is
# stored, e.g. the default branch.
Expand Down
78 changes: 60 additions & 18 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def _(*variables):
return _


DIFF_STDOUT = """diff --git a/foo.py b/foo.py
index 6c08c94..b65c612 100644
--- a/foo.py
+++ b/foo.py
@@ -6,0 +7,6 @@ if os.environ.get("B"):
+
+if os.environ.get("C"):
+ 2
+
+if os.environ.get("D"):
+ 3
"""


@pytest.fixture
def commit(integration_dir):
def _():
Expand Down Expand Up @@ -120,7 +134,13 @@ def test_action__invalid_event_name(session, push_config, in_integration_env, ge


def test_action__pull_request__store_comment(
pull_request_config, session, in_integration_env, output_file, summary_file, capsys
pull_request_config,
session,
in_integration_env,
output_file,
summary_file,
capsys,
git,
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand Down Expand Up @@ -150,13 +170,15 @@ def checker(payload):
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
)(status_code=403)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

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

Expand Down Expand Up @@ -186,7 +208,13 @@ def checker(payload):

@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
pull_request_config,
session,
in_integration_env,
output_file,
summary_file,
capsys,
git,
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand Down Expand Up @@ -217,6 +245,8 @@ def checker(payload):
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
)(status_code=403)

git.register("git diff --unified=0 origin/foo -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(
GITHUB_OUTPUT=output_file,
Expand All @@ -225,7 +255,7 @@ def checker(payload):
),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 0

Expand All @@ -240,7 +270,7 @@ def checker(payload):


def test_action__pull_request__post_comment(
pull_request_config, session, in_integration_env, output_file, summary_file
pull_request_config, session, in_integration_env, output_file, summary_file, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand All @@ -258,6 +288,8 @@ def test_action__pull_request__post_comment(
# Are there already comments
session.register("GET", "/repos/py-cov-action/foobar/issues/2/comments")(json=[])

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

comment = None

def checker(payload):
Expand All @@ -282,7 +314,7 @@ def checker(payload):
),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 0

Expand All @@ -296,11 +328,12 @@ def checker(payload):


def test_action__push__non_default_branch(
push_config, session, in_integration_env, output_file, summary_file
push_config, session, in_integration_env, output_file, summary_file, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
)
git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

payload = json.dumps({"coverage": 30.00})
# There is an existing badge in this test, allowing to test the coverage evolution
Expand Down Expand Up @@ -350,7 +383,7 @@ def checker(payload):
),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 0

Expand All @@ -364,11 +397,12 @@ def checker(payload):


def test_action__push__non_default_branch__no_pr(
push_config, session, in_integration_env, output_file, summary_file
push_config, session, in_integration_env, output_file, summary_file, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
)
git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

payload = json.dumps({"coverage": 30.00})
# There is an existing badge in this test, allowing to test the coverage evolution
Expand Down Expand Up @@ -406,7 +440,7 @@ def test_action__push__non_default_branch__no_pr(
),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 0

Expand All @@ -418,7 +452,7 @@ def test_action__push__non_default_branch__no_pr(


def test_action__pull_request__force_store_comment(
pull_request_config, session, in_integration_env, output_file
pull_request_config, session, in_integration_env, output_file, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand All @@ -431,11 +465,13 @@ def test_action__pull_request__force_store_comment(
"/repos/py-cov-action/foobar/contents/data.json",
)(json={"content": base64.b64encode(payload.encode()).decode()})

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 0

Expand All @@ -447,7 +483,7 @@ def test_action__pull_request__force_store_comment(


def test_action__pull_request__post_comment__no_marker(
pull_request_config, session, in_integration_env, get_logs
pull_request_config, session, in_integration_env, get_logs, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand All @@ -459,18 +495,20 @@ def test_action__pull_request__post_comment__no_marker(
"/repos/py-cov-action/foobar/contents/data.json",
)(status_code=404)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(COMMENT_TEMPLATE="""foo"""),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 1
assert get_logs("ERROR", "Marker not found")


def test_action__pull_request__annotations(
pull_request_config, session, in_integration_env, capsys
pull_request_config, session, in_integration_env, capsys, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand All @@ -481,6 +519,8 @@ def test_action__pull_request__annotations(
"/repos/py-cov-action/foobar/contents/data.json",
)(status_code=404)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

# Who am I
session.register("GET", "/user")(json={"login": "foo"})
# Are there already comments
Expand All @@ -496,7 +536,7 @@ def test_action__pull_request__annotations(
config=pull_request_config(ANNOTATE_MISSING_LINES=True),
github_session=session,
http_session=session,
git=None,
git=git,
)
expected = """::group::Annotations of lines with missing coverage
::warning file=foo.py,line=12::This line has no coverage
Expand All @@ -508,7 +548,7 @@ def test_action__pull_request__annotations(


def test_action__pull_request__post_comment__template_error(
pull_request_config, session, in_integration_env, get_logs
pull_request_config, session, in_integration_env, get_logs, git
):
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
Expand All @@ -520,11 +560,13 @@ def test_action__pull_request__post_comment__template_error(
"/repos/py-cov-action/foobar/contents/data.json",
)(status_code=404)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(COMMENT_TEMPLATE="""{%"""),
github_session=session,
http_session=session,
git=None,
git=git,
)
assert result == 1
assert get_logs("ERROR", "There was a rendering error")
Expand Down

0 comments on commit 4b58f87

Please sign in to comment.