Skip to content

Commit

Permalink
fix(gh-actions-output): fixed trailing newline to match GITHUB_OUTPUT…
Browse files Browse the repository at this point in the history
… format (python-semantic-release#885)

* test(gh-actions-output): fix unit tests to manage proper whitespace

  tests were adjusted for clarity and to replicate error detailed in python-semantic-release#884.

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

  Resolves: python-semantic-release#884
  • Loading branch information
codejedi365 authored Apr 6, 2024
1 parent 8a47db1 commit 2c7b6ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
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()

0 comments on commit 2c7b6ec

Please sign in to comment.