diff --git a/.readthedocs.yml b/.readthedocs.yml index ece51a8..809adcb 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -13,7 +13,7 @@ build: tools: python: "3.10" jobs: + post_create_environment: + - pip install poetry post_install: - - pip install -U poetry - - poetry config virtualenvs.create false - - poetry install --with docs + - VIRTUAL_ENV=$READTHEDOCS_VIRTUALENV_PATH poetry install --with docs diff --git a/sphinx_github_changelog/changelog.py b/sphinx_github_changelog/changelog.py index 35e5380..b8d0be0 100644 --- a/sphinx_github_changelog/changelog.py +++ b/sphinx_github_changelog/changelog.py @@ -41,7 +41,7 @@ def compute_changelog( token: Optional[str], options: Dict[str, str] ) -> List[nodes.Node]: if not token: - return no_token(changelog_url=options["changelog-url"]) + return no_token(changelog_url=options.get("changelog-url")) owner_repo = extract_github_repo_name(url=options["github"]) releases = extract_releases(owner_repo=owner_repo, token=token) @@ -60,7 +60,7 @@ def no_token(changelog_url: Optional[str]) -> List[nodes.Node]: par += nodes.Text("Changelog was not built because ") par += nodes.literal("", "sphinx_github_changelog_token") par += nodes.Text(" parameter is missing in the documentation configuration.") - result = [nodes.warning("", par)] + result: List[nodes.Node] = [nodes.warning("", par)] if changelog_url: par2 = nodes.paragraph() @@ -100,6 +100,12 @@ def extract_pypi_package_name(url: Optional[str]) -> Optional[str]: return stripped_url[len(prefix) :] # noqa +def get_release_title(title: Optional[str], tag: str): + if not title: + return tag + return title if tag in title else f"{tag}: {title}" + + def node_for_release( release: Dict[str, Any], pypi_name: Optional[str] = None ) -> Optional[nodes.Node]: @@ -109,7 +115,7 @@ def node_for_release( tag = release["tagName"] title = release["name"] date = release["publishedAt"][:10] - title = title if tag in title else f"{tag}: {title}" + title = get_release_title(title=title, tag=tag) # Section id_section = nodes.make_id("release-" + tag) diff --git a/tests/unit/test_changelog.py b/tests/unit/test_changelog.py index b5ed70a..ef94142 100644 --- a/tests/unit/test_changelog.py +++ b/tests/unit/test_changelog.py @@ -15,14 +15,6 @@ def extract_releases(mocker, release_dict): ) -@pytest.fixture -def options(): - def _(kwargs): - return {"changelog-url": None, "github": None, "pypi": None, **kwargs} - - return _ - - def node_to_string(node): if isinstance(node, list): return canonicalize( @@ -49,16 +41,16 @@ def canonicalize(value): ) -def test_compute_changelog_no_token(options): - nodes = changelog.compute_changelog(token=None, options=options({})) +def test_compute_changelog_no_token(): + nodes = changelog.compute_changelog(token=None, options={}) assert len(nodes) == 1 assert "Changelog was not built" in node_to_string(nodes[0]) -def test_compute_changelog_token(options, extract_releases): +def test_compute_changelog_token(extract_releases): nodes = changelog.compute_changelog( - token="token", options=options({"github": "https://github.com/a/b/releases"}) + token="token", options={"github": "https://github.com/a/b/releases"} ) assert "1.0.0: A new hope" in node_to_string(nodes[0]) @@ -156,6 +148,13 @@ def test_node_for_release_title_tag(release_dict): ) +def test_node_for_release_none_title(release_dict): + release_dict["name"] = None + assert "1.0.0" in node_to_string( + changelog.node_for_release(release=release_dict, pypi_name=None) + ) + + def test_node_for_release_title_pypy(release_dict): value = node_to_string( changelog.node_for_release(release=release_dict, pypi_name="foo") @@ -239,3 +238,16 @@ def test_github_call_http_error_connection(requests_mock): changelog.github_call(url=url, token="token", query="") assert str(exc_info.value) == "Could not retrieve changelog from github: bar" + + +@pytest.mark.parametrize( + "title, tag, expected", + [ + ("Foo", "1.0.0", "1.0.0: Foo"), + ("1.0.0: Foo", "1.0.0", "1.0.0: Foo"), + ("Fix 1.0.0", "1.0.1", "1.0.1: Fix 1.0.0"), + (None, "1.0.0", "1.0.0"), + ], +) +def test_get_release_title(title, tag, expected): + assert changelog.get_release_title(title=title, tag=tag) == expected