Skip to content

Commit

Permalink
fix(charm-libs): better error message on bad version type
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Dec 11, 2024
1 parent 0215ac0 commit 900d8e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 900d8e5

Please sign in to comment.