Skip to content

Commit 4870826

Browse files
authored
Merge pull request #84 from py-cov-action/environment-files
2 parents 3d455b7 + d56b963 commit 4870826

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

coverage_comment/github.py

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

155155

156-
def set_output(**kwargs: bool) -> None:
157-
for key, value in kwargs.items():
158-
print(f"::set-output name={key}::{json.dumps(value)}")
156+
def set_output(github_output: pathlib.Path, **kwargs: bool) -> None:
157+
if github_output:
158+
with github_output.open("a") as f:
159+
for key, value in kwargs.items():
160+
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
@@ -162,10 +162,12 @@ def generate_comment(
162162
filename=config.COMMENT_FILENAME,
163163
content=comment,
164164
)
165-
github.set_output(COMMENT_FILE_WRITTEN=True)
165+
github.set_output(github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=True)
166166
log.debug("Comment stored locally on disk")
167167
else:
168-
github.set_output(COMMENT_FILE_WRITTEN=False)
168+
github.set_output(
169+
github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=False
170+
)
169171
log.debug("Comment not generated")
170172

171173
return 0

coverage_comment/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Config:
3535
COVERAGE_DATA_BRANCH: str = "python-coverage-comment-action-data"
3636
COMMENT_ARTIFACT_NAME: str = "python-coverage-comment-action"
3737
COMMENT_FILENAME: pathlib.Path = pathlib.Path("python-coverage-comment-action.txt")
38+
GITHUB_OUTPUT: pathlib.Path | None = None
3839
MINIMUM_GREEN: float = 100.0
3940
MINIMUM_ORANGE: float = 70.0
4041
MERGE_COVERAGE_FILES: bool = False
@@ -71,6 +72,10 @@ def clean_force_workflow_run(cls, value: str) -> bool:
7172
def clean_comment_filename(cls, value: str) -> pathlib.Path:
7273
return path_below(value)
7374

75+
@classmethod
76+
def clean_github_output(cls, value: str) -> pathlib.Path:
77+
return pathlib.Path(value)
78+
7479
@property
7580
def GITHUB_PR_NUMBER(self) -> int | None:
7681
# "refs/pull/2/merge"

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,11 @@ def _(*, exit_code=0, stdout=""):
385385
git = Git()
386386
yield git
387387
assert not git.expected_calls
388+
389+
390+
@pytest.fixture
391+
def output_file(tmp_path):
392+
file = tmp_path / "temp_output.txt"
393+
file.touch()
394+
395+
return file

tests/integration/test_github.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_post_comment__update_error(gh, session):
288288
)
289289

290290

291-
def test_set_output(capsys):
292-
github.set_output(foo=True)
293-
captured = capsys.readouterr()
294-
assert captured.out.strip() == "::set-output name=foo::true"
291+
def test_set_output(output_file):
292+
github.set_output(github_output=output_file, foo=True)
293+
294+
assert output_file.read_text() == "foo=true\n"

tests/integration/test_main.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def integration_env(integration_dir, write_file, run_coverage):
8383

8484

8585
def test_action__pull_request__store_comment(
86-
pull_request_config, session, in_integration_env, capsys
86+
pull_request_config, session, in_integration_env, output_file
8787
):
8888
# No existing badge in this test
8989
session.register(
@@ -111,7 +111,7 @@ def checker(payload):
111111
)(status_code=403)
112112

113113
result = main.action(
114-
config=pull_request_config(),
114+
config=pull_request_config(GITHUB_OUTPUT=output_file),
115115
github_session=session,
116116
http_session=session,
117117
git=None,
@@ -131,12 +131,13 @@ def checker(payload):
131131
in comment
132132
)
133133

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

137138

138139
def test_action__pull_request__post_comment(
139-
pull_request_config, session, in_integration_env, capsys
140+
pull_request_config, session, in_integration_env, output_file
140141
):
141142
payload = json.dumps({"coverage": 30.00})
142143
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -169,7 +170,7 @@ def checker(payload):
169170
)
170171

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

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

185187

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

196198
result = main.action(
197-
config=pull_request_config(FORCE_WORKFLOW_RUN=True),
199+
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
198200
github_session=session,
199201
http_session=session,
200202
git=None,
@@ -203,8 +205,9 @@ def test_action__pull_request__force_store_comment(
203205

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

206-
expected_stdout = "::set-output name=COMMENT_FILE_WRITTEN::true"
207-
assert capsys.readouterr().out.strip() == expected_stdout
208+
expected_output = "COMMENT_FILE_WRITTEN=true\n"
209+
210+
assert output_file.read_text() == expected_output
208211

209212

210213
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
"COMMENT_ARTIFACT_NAME": "baz",
@@ -45,6 +46,7 @@ def test_config__from_environ__ok():
4546
GITHUB_TOKEN="foo",
4647
GITHUB_REPOSITORY="owner/repo",
4748
GITHUB_REF="master",
49+
GITHUB_OUTPUT=pathlib.Path("foo.txt"),
4850
GITHUB_EVENT_NAME="pull",
4951
GITHUB_PR_RUN_ID=123,
5052
COMMENT_ARTIFACT_NAME="baz",
@@ -67,6 +69,7 @@ def config():
6769
"GITHUB_REPOSITORY": "owner/repo",
6870
"GITHUB_REF": "master",
6971
"GITHUB_EVENT_NAME": "pull",
72+
"GITHUB_OUTPUT": "",
7073
"GITHUB_PR_RUN_ID": 123,
7174
"COMMENT_ARTIFACT_NAME": "baz",
7275
"COMMENT_FILENAME": pathlib.Path("qux"),

0 commit comments

Comments
 (0)