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

style: improve error message for invalid architectures #98

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion craft_platforms/_architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def parse_base_and_architecture(

:returns: A tuple of the DistroBase and the architecture. The architecture is either
a DebianArchitecture or 'all'.

:raises ValueError: If the architecture or base is invalid.
mr-cal marked this conversation as resolved.
Show resolved Hide resolved
"""
if ":" in arch:
base_str, _, arch_str = arch.partition(":")
Expand All @@ -98,4 +100,7 @@ def parse_base_and_architecture(
base = None
arch_str = arch

return base, DebianArchitecture(arch_str) if arch_str != "all" else "all"
try:
return base, DebianArchitecture(arch_str) if arch_str != "all" else "all"
except ValueError:
raise ValueError(f"{arch_str!r} is not a valid Debian architecture.") from None
2 changes: 2 additions & 0 deletions craft_platforms/_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def parse_base_and_name(platform_name: str) -> Tuple[Optional[_distro.DistroBase
:param platform_name: The name of the platform.

:returns: A tuple of the DistroBase and the platform name.

:raises ValueError: If the base is invalid.
"""
if ":" in platform_name:
base_str, _, name = platform_name.partition(":")
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
*********

X.Y.Z (2025-MM-DD)
mr-cal marked this conversation as resolved.
Show resolved Hide resolved
------------------

- Improve presentation of invalid architecture error messages.

0.5.0 (2024-12-18)
------------------

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/charm/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def test_build_plans_bad_base(base, build_base, platforms, error_msg, error_res)
),
pytest.param(
{"my machine": {"build-on": ["my machine"], "build-for": ["amd64"]}},
"'my machine' is not a valid DebianArchitecture",
"'my machine' is not a valid Debian architecture",
id="invalid-architecture-name",
),
pytest.param(
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/test_architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,21 @@ def test_debian_architecture_from_host(monkeypatch, machine):
("[email protected]:all", (DistroBase("ubuntu", "24.04"), "all")),
],
)
def test_get_base_and_architecture(given, expected):
def test_parse_base_and_architecture(given, expected):
assert parse_base_and_architecture(given) == expected


def test_parse_base_and_architecture_invalid_arch():
expected = "'unknown' is not a valid Debian architecture."

with pytest.raises(ValueError, match=expected):
parse_base_and_architecture("unknown")


def test_parse_base_and_architecture_invalid_base():
expected = (
"Invalid base string 'unknown'. Format should be '<distribution>@<series>'"
)

with pytest.raises(ValueError, match=expected):
parse_base_and_architecture("unknown:riscv64")
11 changes: 10 additions & 1 deletion tests/unit/test_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,14 @@ def test_build_plans_bad_architecture(platforms, error_msg):
),
],
)
def test_get_base_and_name(given, expected):
def test_parse_base_and_name(given, expected):
assert craft_platforms.parse_base_and_name(given) == expected


def test_parse_base_and_name_invalid_base():
expected = (
"Invalid base string 'unknown'. Format should be '<distribution>@<series>'"
)

with pytest.raises(ValueError, match=expected):
craft_platforms.parse_base_and_name("unknown:my-platform")