Skip to content

Commit 50a4569

Browse files
authored
Merge pull request #85 from py-cov-action/environment-files-v2
2 parents 1656a4d + 9b81a4b commit 50a4569

File tree

8 files changed

+41
-20
lines changed

8 files changed

+41
-20
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ jobs:
2121

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

4746
- name: Store Pull Request comment to be posted
48-
uses: actions/upload-artifact@v2
47+
uses: actions/upload-artifact@v3
4948
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
5049
with:
5150
name: python-coverage-comment-action

coverage_comment/github.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def post_comment(
137137
raise CannotPostComment from exc
138138

139139

140-
def set_output(**kwargs: bool) -> None:
141-
for key, value in kwargs.items():
142-
print(f"::set-output name={key}::{json.dumps(value)}")
140+
def set_output(github_output: pathlib.Path, **kwargs: bool) -> None:
141+
if github_output:
142+
with github_output.open("a") as f:
143+
for key, value in kwargs.items():
144+
f.write(f"{key}={json.dumps(value)}\n")

coverage_comment/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ def generate_comment(
161161
filename=config.COMMENT_FILENAME,
162162
content=comment,
163163
)
164-
github.set_output(COMMENT_FILE_WRITTEN=True)
164+
github.set_output(github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=True)
165165
log.debug("Comment stored locally on disk")
166166
else:
167-
github.set_output(COMMENT_FILE_WRITTEN=False)
167+
github.set_output(
168+
github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=False
169+
)
168170
log.debug("Comment not generated")
169171

170172
return 0

coverage_comment/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Config:
3333
)
3434
COMMENT_ARTIFACT_NAME: str = "python-coverage-comment-action"
3535
COMMENT_FILENAME: pathlib.Path = pathlib.Path("python-coverage-comment-action.txt")
36+
GITHUB_OUTPUT: pathlib.Path | None = None
3637
MINIMUM_GREEN: float = 100.0
3738
MINIMUM_ORANGE: float = 70.0
3839
MERGE_COVERAGE_FILES: bool = False
@@ -67,6 +68,10 @@ def clean_badge_filename(cls, value: str) -> pathlib.Path:
6768
def clean_comment_filename(cls, value: str) -> pathlib.Path:
6869
return path_below(value)
6970

71+
@classmethod
72+
def clean_github_output(cls, value: str) -> pathlib.Path:
73+
return pathlib.Path(value)
74+
7075
@property
7176
def GITHUB_PR_NUMBER(self) -> int | None:
7277
# "refs/pull/2/merge"

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,11 @@ def _(filename, content):
338338
return zip_bytes
339339

340340
return _
341+
342+
343+
@pytest.fixture
344+
def output_file(tmp_path):
345+
file = tmp_path / "temp_output.txt"
346+
file.touch()
347+
348+
return file

tests/integration/test_github.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_post_comment__update_error(gh, session):
264264
)
265265

266266

267-
def test_set_output(capsys):
268-
github.set_output(foo=True)
269-
captured = capsys.readouterr()
270-
assert captured.out.strip() == "::set-output name=foo::true"
267+
def test_set_output(output_file):
268+
github.set_output(github_output=output_file, foo=True)
269+
270+
assert output_file.read_text() == "foo=true\n"

tests/integration/test_main.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def integration_env(integration_dir, write_file, run_coverage):
8282

8383

8484
def test_action__pull_request__store_comment(
85-
pull_request_config, session, in_integration_env, capsys
85+
pull_request_config, session, in_integration_env, output_file
8686
):
8787
# No existing badge in this test
8888
session.register(
@@ -110,7 +110,7 @@ def checker(payload):
110110
)
111111

112112
result = main.action(
113-
config=pull_request_config(),
113+
config=pull_request_config(GITHUB_OUTPUT=output_file),
114114
github_session=session,
115115
http_session=session,
116116
git=None,
@@ -130,12 +130,13 @@ def checker(payload):
130130
in comment
131131
)
132132

133-
expected_stdout = "::set-output name=COMMENT_FILE_WRITTEN::true"
134-
assert capsys.readouterr().out.strip() == expected_stdout
133+
expected_output = "COMMENT_FILE_WRITTEN=true\n"
134+
135+
assert output_file.read_text() == expected_output
135136

136137

137138
def test_action__pull_request__post_comment(
138-
pull_request_config, session, in_integration_env, capsys
139+
pull_request_config, session, in_integration_env, output_file
139140
):
140141
# There is an existing badge in this test, allowing to test the coverage evolution
141142
session.register(
@@ -167,7 +168,7 @@ def checker(payload):
167168
)
168169

169170
result = main.action(
170-
config=pull_request_config(),
171+
config=pull_request_config(GITHUB_OUTPUT=output_file),
171172
github_session=session,
172173
http_session=session,
173174
git=None,
@@ -177,8 +178,9 @@ def checker(payload):
177178
assert not pathlib.Path("python-coverage-comment-action.txt").exists()
178179
assert "The coverage rate went from `30%` to `86%` :arrow_up:" in comment
179180

180-
expected_stdout = "::set-output name=COMMENT_FILE_WRITTEN::false"
181-
assert capsys.readouterr().out.strip() == expected_stdout
181+
expected_output = "COMMENT_FILE_WRITTEN=false\n"
182+
183+
assert output_file.read_text() == expected_output
182184

183185

184186
def test_action__pull_request__post_comment__no_marker(

tests/unit/test_settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test_config__from_environ__ok():
2828
"GITHUB_TOKEN": "foo",
2929
"GITHUB_REPOSITORY": "owner/repo",
3030
"GITHUB_REF": "master",
31+
"GITHUB_OUTPUT": "foo.txt",
3132
"GITHUB_EVENT_NAME": "pull",
3233
"GITHUB_PR_RUN_ID": "123",
3334
"BADGE_FILENAME": "bar",
@@ -44,6 +45,7 @@ def test_config__from_environ__ok():
4445
GITHUB_TOKEN="foo",
4546
GITHUB_REPOSITORY="owner/repo",
4647
GITHUB_REF="master",
48+
GITHUB_OUTPUT=pathlib.Path("foo.txt"),
4749
GITHUB_EVENT_NAME="pull",
4850
GITHUB_PR_RUN_ID=123,
4951
BADGE_FILENAME=pathlib.Path("bar"),
@@ -64,6 +66,7 @@ def config():
6466
"GITHUB_TOKEN": "foo",
6567
"GITHUB_REPOSITORY": "owner/repo",
6668
"GITHUB_REF": "master",
69+
"GITHUB_OUTPUT": "",
6770
"GITHUB_EVENT_NAME": "pull",
6871
"GITHUB_PR_RUN_ID": 123,
6972
"BADGE_FILENAME": pathlib.Path("bar"),

0 commit comments

Comments
 (0)