Skip to content

Commit

Permalink
fix(project): improve error message for unsupported architectures
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <[email protected]>
  • Loading branch information
mr-cal committed Oct 4, 2023
1 parent 061b4f0 commit b592abb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
15 changes: 15 additions & 0 deletions snapcraft/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
convert_architecture_deb_to_platform,
get_effective_base,
get_host_architecture,
get_supported_architectures,
is_architecture_supported,
)


Expand Down Expand Up @@ -119,6 +121,19 @@ def _validate_architectures(architectures):
)
unique_build_fors.add(architecture)

# validate architectures are supported
if len(architectures):
for element in architectures:
for arch in element.build_for + element.build_on:
if arch != "all" and not is_architecture_supported(arch):
supported_archs = utils.humanize_list(
get_supported_architectures(), "and"
)
raise ValueError(
f"Architecture {arch!r} is not supported. Supported "
f"architectures are {supported_archs}."
)

return architectures


Expand Down
18 changes: 18 additions & 0 deletions snapcraft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ def get_host_architecture():
)


def is_architecture_supported(architecture: str) -> bool:
"""Check if an debian-syntax architecture is supported.
:param architecture: architecture to check
:returns: True if the architecture is supported by snapcraft.
"""
return architecture in list(_ARCH_TRANSLATIONS_DEB_TO_PLATFORM)


def get_supported_architectures() -> List[str]:
"""Get a list of architectures supported by snapcraft.
:returns: A list of architectures.
"""
return list(_ARCH_TRANSLATIONS_DEB_TO_PLATFORM.keys())


def convert_architecture_deb_to_platform(architecture: str) -> str:
"""Convert an architecture from deb/snap syntax to platform syntax.
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,24 @@ def test_architecture_multiple_build_for_same_architecture_implicit(
error.value
)

@pytest.mark.parametrize(
"architectures",
[
"unknown",
{"build-on": ["unknown"]},
{"build-on": ["unknown"], "build-for": ["amd64"]},
{"build-on": ["amd64"], "build-for": ["unknown"]},
],
)
def test_architecture_unsupported(self, architectures, project_yaml_data):
"""Raise an error for unsupported architectures."""
data = project_yaml_data(architectures=[architectures])

with pytest.raises(errors.ProjectValidationError) as error:
Project.unmarshal(data)

assert "Architecture 'unknown' is not supported." in str(error.value)

def test_project_get_build_on(self, project_yaml_data):
"""Test `get_build_on()` returns the build-on string."""
data = project_yaml_data(
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,38 @@ def test_process_version_git(mocker):
assert utils.process_version("git") == "1.2.3-dirty"


###########################
# Supported architectures #
###########################


@pytest.mark.parametrize("arch", utils.get_supported_architectures())
def test_is_architecture_supported(arch):
"""Supported architectures should return true."""
assert utils.is_architecture_supported(arch)


def test_is_architecture_not_supported():
"""Unsupported architectures should return false."""
assert not utils.is_architecture_supported("unknown")


def get_supported_architectures():
"""Validate list of supported architectures."""
supported_archs = utils.get_supported_architectures()

assert supported_archs == [
"arm64",
"armhf",
"i386",
"powerpc",
"ppc64el",
"amd64",
"s390x",
"riscv64",
]


#########################
# Convert Architectures #
#########################
Expand Down

0 comments on commit b592abb

Please sign in to comment.