Skip to content

Commit

Permalink
fix: do not propagate build-base to snap.yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Schvezov <sergio.schvezov@canonical.com>
  • Loading branch information
sergiusens committed Feb 14, 2024
1 parent cc5c6fa commit 547b05d
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 7 deletions.
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",
]
1 change: 0 additions & 1 deletion snapcraft/meta/snap_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ class SnapMetadata(SnapcraftMetadata):
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 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"),
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
74 changes: 74 additions & 0 deletions tests/unit/services/test_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- 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 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="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,
)

0 comments on commit 547b05d

Please sign in to comment.