forked from python-semantic-release/python-semantic-release
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gh-actions-output): fixed trailing newline to match GITHUB_OUTPUT…
… 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
1 parent
8a47db1
commit 2c7b6ec
Showing
2 changed files
with
48 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 44 additions & 33 deletions
77
tests/unit/semantic_release/cli/test_github_actions_output.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |