Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(charm-libs): better error message on bad version type #1955

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion charmcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class CharmLib(models.CraftBaseModel):
version: str = pydantic.Field(
title="Version filter for the charm. Either an API version or a specific [api].[patch].",
pattern=r"[0-9]+(\.[0-9]+)?",
coerce_numbers_to_str=False,
)

@pydantic.field_validator("lib", mode="before")
Expand Down Expand Up @@ -129,7 +130,7 @@ def _validate_api_version(cls, value: str) -> str:
@pydantic.field_validator("version", mode="before")
def _validate_patch_version(cls, value: str) -> str:
"""Validate the optional patch version, providing a useful error message."""
api, separator, patch = value.partition(".")
api, separator, patch = str(value).partition(".")
if not separator:
return value
try:
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ def test_invalid_patch_version(version: str):
project.CharmLib(lib="charm_name.lib_name", version=version)


@pytest.mark.parametrize("version", [1, 1.1])
def test_bad_version_type(version: str):
with pytest.raises(
pydantic.ValidationError,
match="1 validation error for CharmLib\nversion\n Input should be a valid string",
):
project.CharmLib(lib="charm_name.lib_name", version=version)


@pytest.mark.parametrize(
("run_on", "expected"),
[
Expand Down
Loading