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: do not propagate build-base to snap.yaml #4580

Merged
merged 2 commits into from
Feb 14, 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
2 changes: 1 addition & 1 deletion requirements-devel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pyparsing==3.1.1
pyproject-api==1.6.1
pyramid==2.0.2
pyRFC3339==1.1
pyright==1.1.349
pyright==1.1.350
pytest==7.4.4
pytest-cov==4.1.0
pytest-mock==3.12.0
Expand Down
11 changes: 9 additions & 2 deletions snapcraft/meta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@

"""Snap metadata definitions and helpers."""

from .extracted_metadata import ExtractedMetadata # noqa: F401
from .metadata import extract_metadata # noqa: F401
from .extracted_metadata import ExtractedMetadata
from .metadata import extract_metadata
from .snap_yaml import SnapMetadata

__all__ = [
"extract_metadata",
"ExtractedMetadata",
"SnapMetadata",
]
13 changes: 7 additions & 6 deletions snapcraft/meta/snap_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import pydantic
import yaml
from craft_application.models import BaseMetadata, constraints
from craft_application.models import BaseMetadata, SummaryStr, constraints
from craft_cli import emit
from pydantic import ValidationError, validator
from typing_extensions import override
Expand Down Expand Up @@ -229,13 +229,12 @@ class SnapMetadata(SnapcraftMetadata):
name: str
title: Optional[str]
version: str
summary: str
summary: SummaryStr
description: str
license: Optional[str]
type: Optional[str]
architectures: List[str]
base: Optional[str]
build_base: Optional[str]
assumes: Optional[List[str]]
epoch: Optional[str]
apps: Optional[Dict[str, SnapApp]]
Expand All @@ -245,7 +244,9 @@ class SnapMetadata(SnapcraftMetadata):
plugs: Optional[Dict[str, Any]]
slots: Optional[Dict[str, Any]]
hooks: Optional[Dict[str, Any]]
layout: Optional[Dict[str, Dict[str, str]]]
layout: Optional[
Dict[str, Dict[Literal["symlink", "bind", "bind-file", "type"], str]]
]
system_usernames: Optional[Dict[str, Any]]
provenance: Optional[str]
links: Optional[Links]
Expand Down Expand Up @@ -449,8 +450,8 @@ def get_metadata_from_project(
name=project.name,
title=project.title,
version=version,
summary=project.summary,
description=project.description, # type: ignore
summary=cast(SummaryStr, project.summary),
description=cast(str, project.description),
license=project.license,
type=project.type,
architectures=[arch],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description: |
architectures:
- amd64
base: core24
build-base: devel
apps:
paths-all-null:
command: usr/bin/hello
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ description: |
architectures:
- amd64
base: core24
build-base: devel
apps:
paths-one-null:
command: usr/bin/hello
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ description: Defining LD_LIBRARY_PATH or PATH should override default values.
architectures:
- amd64
base: core24
build-base: devel
apps:
paths-user-defined:
command: usr/bin/hello
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ description: |
architectures:
- amd64
base: core24
build-base: devel
apps:
staged-common-library:
command: test-cmd
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,63 @@ def launched_environment(
yield mock_instance

return FakeProvider()


@pytest.fixture()
def extra_project_params():
"""Configuration fixture for the Project used by the default services."""
return {"confinement": "devmode"}


# The factory setup from CraftApplication is imported at the fixture level.
# pylint: disable=import-outside-toplevel


@pytest.fixture()
def default_project(extra_project_params):
from craft_application.models import SummaryStr, VersionStr

from snapcraft.models.project import Project

parts = extra_project_params.pop("parts", {})

return Project(
name="default",
version=VersionStr("1.0"),
summary=SummaryStr("default project"),
description="default project",
base="core24",
build_base="devel",
parts=parts,
license="MIT",
**extra_project_params,
)


@pytest.fixture()
def default_factory(default_project):
from snapcraft.application import APP_METADATA
from snapcraft.services import SnapcraftServiceFactory

factory = SnapcraftServiceFactory(
app=APP_METADATA,
project=default_project,
)
return factory


@pytest.fixture()
def package_service(default_project, default_factory):
from snapcraft.application import APP_METADATA
from snapcraft.services import Package

return Package(
app=APP_METADATA,
project=default_project,
services=default_factory,
platform="amd64",
build_for="amd64",
)


# pylint: enable=import-outside-toplevel
64 changes: 64 additions & 0 deletions tests/unit/meta/test_snap_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,70 @@ def test_assumes(simple_project, new_dir):
)


def test_build_base_devel(simple_project, new_dir):
"""Devel base return devel grade and no build-base in snap.yaml."""
snap_yaml.write(
simple_project(build_base="devel"),
prime_dir=Path(new_dir),
arch="amd64",
)
yaml_file = Path("meta/snap.yaml")
assert yaml_file.is_file()

content = yaml_file.read_text()
assert content == textwrap.dedent(
"""\
name: mytest
version: 1.29.3
summary: Single-line elevator pitch for your amazing snap
description: test-description
architectures:
- amd64
base: core22
apps:
app1:
command: bin/mytest
confinement: strict
grade: devel
environment:
LD_LIBRARY_PATH: ${SNAP_LIBRARY_PATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
PATH: $SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH
"""
)


def test_build_base_stable(simple_project, new_dir):
"""Stable base return stable grade and no build-base in snap.yaml."""
snap_yaml.write(
simple_project(build_base="core22"),
sergiusens marked this conversation as resolved.
Show resolved Hide resolved
prime_dir=Path(new_dir),
arch="amd64",
)
yaml_file = Path("meta/snap.yaml")
assert yaml_file.is_file()

content = yaml_file.read_text()
assert content == textwrap.dedent(
"""\
name: mytest
version: 1.29.3
summary: Single-line elevator pitch for your amazing snap
description: test-description
architectures:
- amd64
base: core22
apps:
app1:
command: bin/mytest
confinement: strict
grade: stable
environment:
LD_LIBRARY_PATH: ${SNAP_LIBRARY_PATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
PATH: $SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH
"""
)


def test_links_scalars(simple_project, new_dir):
snap_yaml.write(
simple_project(
Expand Down
76 changes: 76 additions & 0 deletions tests/unit/services/test_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Tests for the Snapcraft Package service."""

from pathlib import Path

from craft_application.models import SummaryStr

from snapcraft import linters, meta, pack


def test_pack(package_service, default_factory, mocker):
mock_pack_snap = mocker.patch.object(pack, "pack_snap")
mock_linters_run = mocker.patch.object(linters, "run_linters")
mock_linters_report = mocker.patch.object(linters, "report")

package_service.pack(prime_dir=Path("prime"), dest=Path())

# Check that the regular pack.pack_snap() function was called with the correct
# parameters.
mock_pack_snap.assert_called_once_with(
Path("prime"),
name="default",
version="1.0",
compression="xz",
output=".",
target_arch="amd64",
)


def test_metadata(package_service, default_factory, new_dir):
default_factory.set_kwargs(
"lifecycle", work_dir=Path("work"), cache_dir=new_dir, build_for="amd64"
)

assert package_service.metadata == meta.SnapMetadata(
name="default",
title=None,
version="1.0",
summary=SummaryStr("default project"),
description="default project",
license="MIT",
type=None,
architectures=["amd64"],
base="core24",
assumes=None,
epoch=None,
apps=None,
confinement="devmode",
grade="devel",
environment={
"LD_LIBRARY_PATH": "${SNAP_LIBRARY_PATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
},
plugs=None,
slots=None,
hooks=None,
layout=None,
system_usernames=None,
provenance=None,
links=None,
)
Loading