Skip to content

Commit

Permalink
chore(project): implement _providers_base()
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <callahan.kovacs@canonical.com>
  • Loading branch information
mr-cal committed May 8, 2024
1 parent 739063a commit 080fe01
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
17 changes: 17 additions & 0 deletions snapcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from craft_grammar.models import GrammarSingleEntryDictList, GrammarStr, GrammarStrList
from craft_providers import bases
from pydantic import PrivateAttr, constr
from typing_extensions import override

from snapcraft import utils
from snapcraft.const import SUPPORTED_ARCHS, SnapArch
Expand Down Expand Up @@ -602,6 +603,22 @@ class Project(models.Project):
provenance: Optional[str]
components: Optional[Dict[ProjectName, Component]]

@override
@classmethod
def _providers_base(cls, base: str) -> bases.BaseAlias | None:
"""Get a BaseAlias from snapcraft's base.
:param base: The application-specific base name.
:returns: The BaseAlias for the base.
:raises CraftValidationError: If the project's base cannot be determined.
"""
if base == "bare":
return None

try:
return SNAPCRAFT_BASE_TO_PROVIDER_BASE[base]
except KeyError as err:
raise CraftValidationError(f"Unknown base {base!r}") from err

@pydantic.validator("plugs")
@classmethod
def _validate_plugs(cls, plugs):
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/models/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from craft_providers.bases import BaseName

import snapcraft.models
from snapcraft import const, errors
from snapcraft import const, errors, providers
from snapcraft.models import (
MANDATORY_ADOPTABLE_FIELDS,
Architecture,
Expand Down Expand Up @@ -585,6 +585,21 @@ def test_project_build_base_devel_grade_stable_error(self, project_yaml_data):
with pytest.raises(errors.ProjectValidationError, match=error):
Project.unmarshal(project_yaml_data(build_base="devel", grade="stable"))

@pytest.mark.parametrize(
("base", "expected_base"),
[("bare", None), *providers.SNAPCRAFT_BASE_TO_PROVIDER_BASE.items()],
)
def test_provider_base(self, base, expected_base, project_yaml_data):
providers_base = Project._providers_base(base)

assert providers_base == expected_base

def test_provider_base_error(self, project_yaml_data):
with pytest.raises(CraftValidationError) as raised:
Project._providers_base("unknown")

assert "Unknown base 'unknown'" in str(raised.value)

def test_project_global_plugs_warning(self, project_yaml_data, emitter):
data = project_yaml_data(plugs={"desktop": None, "desktop-legacy": None})
Project.unmarshal(data)
Expand Down

0 comments on commit 080fe01

Please sign in to comment.