Skip to content

Commit

Permalink
Merge pull request #85 from py-cov-action/environment-files-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Oct 17, 2022
2 parents 1656a4d + 9b81a4b commit 50a4569
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 20 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ jobs:

- name: Install Poetry
run: |
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
echo "$HOME/.poetry/bin" >> $GITHUB_PATH
pipx install poetry --python=python3.10
- name: Poetry caches
uses: actions/cache@v2
Expand All @@ -45,7 +44,7 @@ jobs:
VERBOSE: "true"

- name: Store Pull Request comment to be posted
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
with:
name: python-coverage-comment-action
Expand Down
8 changes: 5 additions & 3 deletions coverage_comment/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,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 @@ -161,10 +161,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 @@ -33,6 +33,7 @@ class Config:
)
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 @@ -67,6 +68,10 @@ def clean_badge_filename(cls, value: str) -> pathlib.Path:
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 @@ -338,3 +338,11 @@ def _(filename, content):
return zip_bytes

return _


@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 @@ -264,7 +264,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"
18 changes: 10 additions & 8 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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 @@ -110,7 +110,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 @@ -130,12 +130,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
):
# There is an existing badge in this test, allowing to test the coverage evolution
session.register(
Expand Down Expand Up @@ -167,7 +168,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 @@ -177,8 +178,9 @@ 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__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",
"BADGE_FILENAME": "bar",
Expand All @@ -44,6 +45,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,
BADGE_FILENAME=pathlib.Path("bar"),
Expand All @@ -64,6 +66,7 @@ def config():
"GITHUB_TOKEN": "foo",
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_REF": "master",
"GITHUB_OUTPUT": "",
"GITHUB_EVENT_NAME": "pull",
"GITHUB_PR_RUN_ID": 123,
"BADGE_FILENAME": pathlib.Path("bar"),
Expand Down

0 comments on commit 50a4569

Please sign in to comment.