Skip to content

Commit

Permalink
Merge pull request #84 from py-cov-action/environment-files
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Oct 17, 2022
2 parents 3d455b7 + d56b963 commit 4870826
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
8 changes: 5 additions & 3 deletions coverage_comment/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def post_comment(
raise CannotPostComment from exc


def set_output(**kwargs: bool) -> None:
for key, value in kwargs.items():
print(f"::set-output name={key}::{json.dumps(value)}")
def set_output(github_output: pathlib.Path, **kwargs: bool) -> None:
if github_output:
with github_output.open("a") as f:
for key, value in kwargs.items():
f.write(f"{key}={json.dumps(value)}\n")
6 changes: 4 additions & 2 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ def generate_comment(
filename=config.COMMENT_FILENAME,
content=comment,
)
github.set_output(COMMENT_FILE_WRITTEN=True)
github.set_output(github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=True)
log.debug("Comment stored locally on disk")
else:
github.set_output(COMMENT_FILE_WRITTEN=False)
github.set_output(
github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=False
)
log.debug("Comment not generated")

return 0
Expand Down
5 changes: 5 additions & 0 deletions coverage_comment/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Config:
COVERAGE_DATA_BRANCH: str = "python-coverage-comment-action-data"
COMMENT_ARTIFACT_NAME: str = "python-coverage-comment-action"
COMMENT_FILENAME: pathlib.Path = pathlib.Path("python-coverage-comment-action.txt")
GITHUB_OUTPUT: pathlib.Path | None = None
MINIMUM_GREEN: float = 100.0
MINIMUM_ORANGE: float = 70.0
MERGE_COVERAGE_FILES: bool = False
Expand Down Expand Up @@ -71,6 +72,10 @@ def clean_force_workflow_run(cls, value: str) -> bool:
def clean_comment_filename(cls, value: str) -> pathlib.Path:
return path_below(value)

@classmethod
def clean_github_output(cls, value: str) -> pathlib.Path:
return pathlib.Path(value)

@property
def GITHUB_PR_NUMBER(self) -> int | None:
# "refs/pull/2/merge"
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,11 @@ def _(*, exit_code=0, stdout=""):
git = Git()
yield git
assert not git.expected_calls


@pytest.fixture
def output_file(tmp_path):
file = tmp_path / "temp_output.txt"
file.touch()

return file
8 changes: 4 additions & 4 deletions tests/integration/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_post_comment__update_error(gh, session):
)


def test_set_output(capsys):
github.set_output(foo=True)
captured = capsys.readouterr()
assert captured.out.strip() == "::set-output name=foo::true"
def test_set_output(output_file):
github.set_output(github_output=output_file, foo=True)

assert output_file.read_text() == "foo=true\n"
27 changes: 15 additions & 12 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def integration_env(integration_dir, write_file, run_coverage):


def test_action__pull_request__store_comment(
pull_request_config, session, in_integration_env, capsys
pull_request_config, session, in_integration_env, output_file
):
# No existing badge in this test
session.register(
Expand Down Expand Up @@ -111,7 +111,7 @@ def checker(payload):
)(status_code=403)

result = main.action(
config=pull_request_config(),
config=pull_request_config(GITHUB_OUTPUT=output_file),
github_session=session,
http_session=session,
git=None,
Expand All @@ -131,12 +131,13 @@ def checker(payload):
in comment
)

expected_stdout = "::set-output name=COMMENT_FILE_WRITTEN::true"
assert capsys.readouterr().out.strip() == expected_stdout
expected_output = "COMMENT_FILE_WRITTEN=true\n"

assert output_file.read_text() == expected_output


def test_action__pull_request__post_comment(
pull_request_config, session, in_integration_env, capsys
pull_request_config, session, in_integration_env, output_file
):
payload = json.dumps({"coverage": 30.00})
# There is an existing badge in this test, allowing to test the coverage evolution
Expand Down Expand Up @@ -169,7 +170,7 @@ def checker(payload):
)

result = main.action(
config=pull_request_config(),
config=pull_request_config(GITHUB_OUTPUT=output_file),
github_session=session,
http_session=session,
git=None,
Expand All @@ -179,12 +180,13 @@ def checker(payload):
assert not pathlib.Path("python-coverage-comment-action.txt").exists()
assert "The coverage rate went from `30%` to `86%` :arrow_up:" in comment

expected_stdout = "::set-output name=COMMENT_FILE_WRITTEN::false"
assert capsys.readouterr().out.strip() == expected_stdout
expected_output = "COMMENT_FILE_WRITTEN=false\n"

assert output_file.read_text() == expected_output


def test_action__pull_request__force_store_comment(
pull_request_config, session, in_integration_env, capsys
pull_request_config, session, in_integration_env, output_file
):
payload = json.dumps({"coverage": 30.00})
# There is an existing badge in this test, allowing to test the coverage evolution
Expand All @@ -194,7 +196,7 @@ def test_action__pull_request__force_store_comment(
)(json={"content": base64.b64encode(payload.encode()).decode()})

result = main.action(
config=pull_request_config(FORCE_WORKFLOW_RUN=True),
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
github_session=session,
http_session=session,
git=None,
Expand All @@ -203,8 +205,9 @@ def test_action__pull_request__force_store_comment(

assert pathlib.Path("python-coverage-comment-action.txt").exists()

expected_stdout = "::set-output name=COMMENT_FILE_WRITTEN::true"
assert capsys.readouterr().out.strip() == expected_stdout
expected_output = "COMMENT_FILE_WRITTEN=true\n"

assert output_file.read_text() == expected_output


def test_action__pull_request__post_comment__no_marker(
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_config__from_environ__ok():
"GITHUB_TOKEN": "foo",
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_REF": "master",
"GITHUB_OUTPUT": "foo.txt",
"GITHUB_EVENT_NAME": "pull",
"GITHUB_PR_RUN_ID": "123",
"COMMENT_ARTIFACT_NAME": "baz",
Expand All @@ -45,6 +46,7 @@ def test_config__from_environ__ok():
GITHUB_TOKEN="foo",
GITHUB_REPOSITORY="owner/repo",
GITHUB_REF="master",
GITHUB_OUTPUT=pathlib.Path("foo.txt"),
GITHUB_EVENT_NAME="pull",
GITHUB_PR_RUN_ID=123,
COMMENT_ARTIFACT_NAME="baz",
Expand All @@ -67,6 +69,7 @@ def config():
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_REF": "master",
"GITHUB_EVENT_NAME": "pull",
"GITHUB_OUTPUT": "",
"GITHUB_PR_RUN_ID": 123,
"COMMENT_ARTIFACT_NAME": "baz",
"COMMENT_FILENAME": pathlib.Path("qux"),
Expand Down

0 comments on commit 4870826

Please sign in to comment.