Skip to content

Commit

Permalink
fix(cli-generate-config): ensure configuration types are always toml …
Browse files Browse the repository at this point in the history
…parsable (#785)
  • Loading branch information
codejedi365 committed Jan 3, 2024
1 parent 253c99e commit 758e649
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
4 changes: 3 additions & 1 deletion semantic_release/cli/commands/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def generate_config(fmt: str = "toml", is_pyproject_toml: bool = False) -> None:
semantic-release generate-config -f toml >> pyproject.toml
"""
config = RawConfig().model_dump(exclude_none=True)
# due to possible IntEnum values (which are not supported by tomlkit.dumps, see sdispater/tomlkit#237),
# we must ensure the transformation of the model to a dict uses json serializable values
config = RawConfig().model_dump(mode="json", exclude_none=True)

config_dct = {"semantic_release": config}
if is_pyproject_toml and fmt == "toml":
Expand Down
59 changes: 38 additions & 21 deletions tests/command_line/test_generate_config.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
from __future__ import annotations

import json
from typing import TYPE_CHECKING

import pytest
import tomlkit

from semantic_release.cli.commands.generate_config import generate_config
from semantic_release.cli.config import RawConfig

if TYPE_CHECKING:
from typing import Any

from tests.command_line.conftest import CliRunner


@pytest.fixture
def raw_config_dict() -> dict[str, Any]:
return RawConfig().model_dump(mode="json", exclude_none=True)


@pytest.mark.parametrize("args", [(), ("--format", "toml"), ("--format", "TOML")])
def test_generate_config_toml(cli_runner, args):
def test_generate_config_toml(
cli_runner: CliRunner, args: tuple[str], raw_config_dict: dict[str, Any]
):
expected_config_as_str = tomlkit.dumps(
{"semantic_release": raw_config_dict}
).strip()

result = cli_runner.invoke(generate_config, args)

assert result.exit_code == 0
assert (
result.output.strip()
== tomlkit.dumps(
{"semantic_release": RawConfig().model_dump(exclude_none=True)}
).strip()
)
assert expected_config_as_str == result.output.strip()


@pytest.mark.parametrize("args", [("--format", "json"), ("--format", "JSON")])
def test_generate_config_json(cli_runner, args):
def test_generate_config_json(
cli_runner: CliRunner, args: tuple[str], raw_config_dict: dict[str, Any]
):
expected_config_as_str = json.dumps(
{"semantic_release": raw_config_dict}, indent=4
).strip()

result = cli_runner.invoke(generate_config, args)

assert result.exit_code == 0
assert (
result.output.strip()
== json.dumps(
{"semantic_release": RawConfig().model_dump(exclude_none=True)}, indent=4
).strip()
)
assert expected_config_as_str == result.output.strip()


def test_generate_config_pyproject_toml(cli_runner):
def test_generate_config_pyproject_toml(
cli_runner: CliRunner, raw_config_dict: dict[str, Any]
):
expected_config_as_str = tomlkit.dumps(
{"tool": {"semantic_release": raw_config_dict}}
).strip()

result = cli_runner.invoke(generate_config, ["--format", "toml", "--pyproject"])

assert result.exit_code == 0
assert (
result.output.strip()
== tomlkit.dumps(
{"tool": {"semantic_release": RawConfig().model_dump(exclude_none=True)}}
).strip()
)
assert expected_config_as_str == result.output.strip()
2 changes: 1 addition & 1 deletion tests/unit/semantic_release/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_invalid_hvcs_type(remote_config: dict[str, Any]):
def test_default_toml_config_valid(example_project):
default_config_file = example_project / "default.toml"
default_config_file.write_text(
tomlkit.dumps(RawConfig().model_dump(exclude_none=True))
tomlkit.dumps(RawConfig().model_dump(mode='json', exclude_none=True))
)

written = default_config_file.read_text(encoding="utf-8")
Expand Down

0 comments on commit 758e649

Please sign in to comment.