Skip to content

fix(gh-actions-output): fixed trailing newline to match GITHUB_OUTPUT format #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion semantic_release/cli/github_actions_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def to_output_text(self) -> str:
"version": str(self.version),
"tag": self.tag,
}
return "\n".join(f"{key}={value!s}" for key, value in outputs.items())

return str.join("", [
f"{key}={value!s}\n" for key, value in outputs.items()
])

def write_if_possible(self, filename: str | None = None) -> None:
output_file = filename or os.getenv(self.OUTPUT_ENV_VAR)
Expand Down
77 changes: 44 additions & 33 deletions tests/unit/semantic_release/cli/test_github_actions_output.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,77 @@
from __future__ import annotations

from textwrap import dedent
from typing import TYPE_CHECKING

import pytest

from semantic_release import Version
from semantic_release.cli.github_actions_output import VersionGitHubActionsOutput

from tests.util import actions_output_to_dict

if TYPE_CHECKING:
from pathlib import Path


@pytest.mark.parametrize("released", (True, False))
def test_version_github_actions_output_format(released):
version = Version.parse("1.2.3")
output = VersionGitHubActionsOutput()

output.version = version
output.released = released

text = output.to_output_text()
# fmt: off
assert (
text == f"released={str(released).lower()}\n"
f"version={version!s}\n"
f"tag={version.as_tag()}"
def test_version_github_actions_output_format(released: bool):
version_str = "1.2.3"
expected_output = dedent(
f"""\
released={'true' if released else 'false'}
version={version_str}
tag=v{version_str}
"""
)
output = VersionGitHubActionsOutput(
released=released,
version=Version.parse(version_str),
)
# fmt: on

# Evaluate (expected -> actual)
assert expected_output == output.to_output_text()

def test_version_github_actions_output_fails_if_missing_output():
version = Version.parse("1.2.3")
output = VersionGitHubActionsOutput()

output.version = version
def test_version_github_actions_output_fails_if_missing_output():
output = VersionGitHubActionsOutput(
version=Version.parse("1.2.3"),
)

# Execute with expected failure
with pytest.raises(ValueError, match="required outputs were not set"):
output.to_output_text()


def test_version_github_actions_output_writes_to_github_output_if_available(
monkeypatch, tmp_path
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
mock_output_file = tmp_path / "action.out"
version = Version.parse("1.2.3")
output = VersionGitHubActionsOutput()

output.version = version
output.released = True

version_str = "1.2.3"
monkeypatch.setenv("GITHUB_OUTPUT", str(mock_output_file.resolve()))
output = VersionGitHubActionsOutput(
version=Version.parse(version_str),
released=True,
)

output.write_if_possible()

action_outputs = actions_output_to_dict(
mock_output_file.read_text(encoding="utf-8")
)

assert action_outputs["version"] == str(version)
assert action_outputs["released"] == "true"
# Evaluate (expected -> actual)
assert version_str == action_outputs["version"]
assert str(True).lower() == action_outputs["released"]


def test_version_github_actions_output_no_error_if_not_in_gha(monkeypatch):
version = Version.parse("1.2.3")
output = VersionGitHubActionsOutput()

output.version = version
output.released = True
def test_version_github_actions_output_no_error_if_not_in_gha(
monkeypatch: pytest.MonkeyPatch,
):
output = VersionGitHubActionsOutput(
version=Version.parse("1.2.3"),
released=True,
)

monkeypatch.delenv("GITHUB_OUTPUT", raising=False)
output.write_if_possible()