Skip to content

Commit

Permalink
test(unit-release-notes): refactor template testing
Browse files Browse the repository at this point in the history
Drop the test related release notes template as that is one more thing to
maintain and use the one provided to the user.
  • Loading branch information
codejedi365 committed Feb 10, 2024
1 parent e626208 commit 5256026
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 40 deletions.

This file was deleted.

118 changes: 86 additions & 32 deletions tests/unit/semantic_release/changelog/test_release_notes.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,107 @@
# NOTE: use backport with newer API
from __future__ import annotations

from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
from git import Commit, Repo
from git.objects import Object

# NOTE: use backport with newer API to support 3.7
from importlib_resources import files

import semantic_release
from semantic_release.changelog.context import make_changelog_context
from semantic_release.changelog.release_history import ReleaseHistory
from semantic_release.changelog.release_history import Release, ReleaseHistory
from semantic_release.changelog.template import environment
from semantic_release.hvcs import Github
from semantic_release.version import Version, VersionTranslator
from semantic_release.commit_parser import ParsedCommit
from semantic_release.enums import LevelBump
from semantic_release.hvcs import Gitea, Github, Gitlab
from semantic_release.version import Version

from tests.const import COMMIT_MESSAGE
from tests.const import TODAY_DATE_STR

default_release_notes_template = (
files("tests")
.joinpath("unit/semantic_release/changelog/test_release_notes.md.j2")
.read_text(encoding="utf-8")
)
if TYPE_CHECKING:
from git import Actor

today_as_str = datetime.now().strftime("%Y-%m-%d")
from semantic_release.hvcs import HvcsBase


def _cm_rstripped(version: str) -> str:
return COMMIT_MESSAGE.format(version=version).rstrip()
@pytest.fixture
def artificial_release_history(commit_author: Actor):
version = Version.parse("1.1.0-alpha.3")
commit_subject = "fix(cli): fix a problem"

fix_commit = Commit(
Repo("."),
Object.NULL_HEX_SHA[:20].encode("utf-8"),
message=commit_subject,
)

EXPECTED_CONTENT = f"""\
# v1.1.0-alpha.3 ({today_as_str})
## Fix
* fix(feature): add some missing text
## Unknown
* {_cm_rstripped("1.1.0-alpha.3")}
"""
fix_commit_parsed = ParsedCommit(
bump=LevelBump.PATCH,
type="fix",
scope="cli",
descriptions=[commit_subject],
breaking_descriptions=[],
commit=fix_commit,
)

return ReleaseHistory(
unreleased={},
released={
version: Release(
tagger=commit_author,
committer=commit_author,
tagged_date=datetime.utcnow(),
elements={
"fix": [fix_commit_parsed],
}
)
}
)


def test_default_changelog_template(
repo_with_git_flow_and_release_channels_angular_commits, default_angular_parser
@pytest.fixture
def release_notes_template() -> str:
"""Retrieve the semantic-release default release notes template."""
version_notes_template = files(semantic_release.__name__).joinpath(
Path("data", "templates", "release_notes.md.j2")
)
return version_notes_template.read_text(encoding="utf-8")


@pytest.mark.parametrize("hvcs_client", [Github, Gitlab, Gitea])
def test_default_release_notes_template(
example_git_https_url: str,
hvcs_client: type[HvcsBase],
release_notes_template: str,
artificial_release_history: ReleaseHistory,
):
version = Version.parse("1.1.0-alpha.3")
repo = repo_with_git_flow_and_release_channels_angular_commits
"""
Unit test goal: just make sure it renders the release notes template without error.
Scenarios are better suited for all the variations (commit types).
"""
version_str = "1.1.0-alpha.3"
version = Version.parse(version_str)
commit_obj = artificial_release_history.released[version]["elements"]["fix"][0]
commit_url = hvcs_client(example_git_https_url).commit_hash_url(commit_obj.commit.hexsha)
commit_description = str.join('\n', commit_obj.descriptions)
expected_content = str.join("\n", [
f"# v{version_str} ({TODAY_DATE_STR})",
"## Fix",
f"* {commit_description} ([`{commit_obj.commit.hexsha[:7]}`]({commit_url}))",
"",
])
env = environment(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)
rh = ReleaseHistory.from_git_history(
repo=repo, translator=VersionTranslator(), commit_parser=default_angular_parser
)
context = make_changelog_context(
hvcs_client=Github(remote_url=repo.remote().url), release_history=rh
hvcs_client=hvcs_client(remote_url=example_git_https_url),
release_history=artificial_release_history,
)
context.bind_to_environment(env)
release = rh.released[version]
actual_content = env.from_string(default_release_notes_template).render(
version=version, release=release
actual_content = env.from_string(release_notes_template).render(
version=version, release=context.history.released[version]
)
assert actual_content == EXPECTED_CONTENT
assert expected_content == actual_content

0 comments on commit 5256026

Please sign in to comment.