Repository: py-cov-action/python-coverage-comment-action
Pull request #85 (2022-10-16 16:38:46)

Use environment files instead of deprecated `set-output` for `v2`
Same as #84 for `v2`

Edit: Also updating poetry to install via `pipx` instead of the deprecated installation method.


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index cdfde43..f9e0bd2 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -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
@@ -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
diff --git a/coverage_comment/github.py b/coverage_comment/github.py
index 035091d..14022e3 100644
--- a/coverage_comment/github.py
+++ b/coverage_comment/github.py
@@ -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")
diff --git a/coverage_comment/main.py b/coverage_comment/main.py
index 6043e99..8af6965 100644
--- a/coverage_comment/main.py
+++ b/coverage_comment/main.py
@@ -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
diff --git a/coverage_comment/settings.py b/coverage_comment/settings.py
index f725c94..89e90fc 100644
--- a/coverage_comment/settings.py
+++ b/coverage_comment/settings.py
@@ -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
@@ -67,6 +68,10 @@ class Config:
     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"


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index d84ae2a..f7e251e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -338,3 +338,11 @@ def zip_bytes():
         return zip_bytes
 
     return _
+
+
+@pytest.fixture
+def output_file(tmp_path):
+    file = tmp_path / "temp_output.txt"
+    file.touch()
+
+    return file
diff --git a/tests/integration/test_github.py b/tests/integration/test_github.py
index 7c95137..33a0843 100644
--- a/tests/integration/test_github.py
+++ b/tests/integration/test_github.py
@@ -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"
diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py
index fb53693..da1f8db 100644
--- a/tests/integration/test_main.py
+++ b/tests/integration/test_main.py
@@ -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(
@@ -110,7 +110,7 @@ def test_action__pull_request__store_comment(
     )
 
     result = main.action(
-        config=pull_request_config(),
+        config=pull_request_config(GITHUB_OUTPUT=output_file),
         github_session=session,
         http_session=session,
         git=None,
@@ -130,12 +130,13 @@ def test_action__pull_request__store_comment(
         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(
@@ -167,7 +168,7 @@ def test_action__pull_request__post_comment(
     )
 
     result = main.action(
-        config=pull_request_config(),
+        config=pull_request_config(GITHUB_OUTPUT=output_file),
         github_session=session,
         http_session=session,
         git=None,
@@ -177,8 +178,9 @@ def test_action__pull_request__post_comment(
     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(
diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py
index 9fccfe8..69d1e57 100644
--- a/tests/unit/test_settings.py
+++ b/tests/unit/test_settings.py
@@ -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",
@@ -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"),
@@ -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"),