Skip to content

Commit

Permalink
review: move keys method to LifecycleService
Browse files Browse the repository at this point in the history
  • Loading branch information
tigarmo committed Apr 24, 2024
1 parent dbd750a commit 639bcc3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
14 changes: 1 addition & 13 deletions snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from .legacy_cli import _LIB_NAMES, _ORIGINAL_LIB_NAME_LOG_LEVEL
from .parts import plugins
from .parts.yaml_utils import extract_parse_info, get_snap_project
from .parts.yaml_utils import extract_parse_info

APP_METADATA = AppMetadata(
name="snapcraft",
Expand Down Expand Up @@ -230,18 +230,6 @@ def _create_dispatcher(self) -> craft_cli.Dispatcher:
default_command=craft_app_commands.lifecycle.PackCommand,
)

@override
def _get_local_keys_path(self) -> pathlib.Path | None:
if self._snapcraft_yaml_path is None:
return None

snap_project = get_snap_project()
keys_dir = snap_project.assets_dir / "keys"
if keys_dir.is_dir():
return keys_dir

return None


def create_app() -> Snapcraft:
"""Create a Snapcraft application with the proper commands."""
Expand Down
10 changes: 10 additions & 0 deletions snapcraft/services/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from overrides import overrides

from snapcraft import __version__, errors, models, os_release, parts, utils
from snapcraft.parts.yaml_utils import get_snap_project


class Lifecycle(LifecycleService):
Expand Down Expand Up @@ -158,6 +159,15 @@ def generate_manifest(self) -> models.Manifest:
primed_stage_packages=sorted(primed_stage_packages),
)

@overrides
def _get_local_keys_path(self) -> Path | None:
snap_project = get_snap_project()
keys_dir = snap_project.assets_dir / "keys"
if keys_dir.is_dir():
return keys_dir

return None


def get_prime_dirs_from_project(project_info: ProjectInfo) -> dict[str | None, Path]:
"""Get a mapping of component names to prime directories from a ProjectInfo.
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/services/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import platform
import shutil
import sys
from pathlib import Path
from unittest import mock

import pytest
Expand Down Expand Up @@ -198,3 +199,37 @@ def test_lifecycle_prime_dirs(lifecycle_service):
lifecycle_service.setup()

assert lifecycle_service.prime_dirs == {None: lifecycle_service._work_dir / "prime"}


@pytest.mark.parametrize(
("project_location", "assets_dir", "expected_keys_path"),
[
# These locations come from yaml_utils._SNAP_PROJECT_FILES
# snapcraft.yaml locations where the keys are expected to be in snap/keys
("snapcraft.yaml", "snap", "snap/keys"),
("snap/snapcraft.yaml", "snap", "snap/keys"),
(".snapcraft.yaml", "snap", "snap/keys"),
# snapcraft.yaml location where the keys are expected to be somewhere else
("build-aux/snap/snapcraft.yaml", "build-aux/snap", "build-aux/snap/keys"),
],
)
def test_local_keys_path(
new_dir, lifecycle_service, project_location, assets_dir, expected_keys_path
):
"""Check that _get_local_keys_path() is correct given the location of snapcraft.yaml."""
snap_dir = new_dir / Path(project_location).parent
snap_dir.mkdir(exist_ok=True, parents=True)

# The project file itself doesn't really matter, but must exist
project_yaml = snap_dir / "snapcraft.yaml"
project_yaml.touch()

# app = application.create_app()
keys_dir = Path(assets_dir) / "keys"

# If the keys dir doesn't exist the method should return None
assert not keys_dir.is_dir()
assert lifecycle_service._get_local_keys_path() is None

keys_dir.mkdir(exist_ok=True, parents=True)
assert lifecycle_service._get_local_keys_path() == Path(expected_keys_path)
33 changes: 0 additions & 33 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import json
import os
import sys
from pathlib import Path
from textwrap import dedent

import craft_cli
Expand Down Expand Up @@ -468,35 +467,3 @@ def test_run_envvar_invalid(snapcraft_yaml, base, monkeypatch):
"'SNAPCRAFT_REMOTE_BUILD_STRATEGY'. Valid values are 'disable-fallback' and "
"'force-fallback'"
)


@pytest.mark.parametrize(
("project_location", "assets_dir", "expected_keys_path"),
[
# These locations come from yaml_utils._SNAP_PROJECT_FILES
# snapcraft.yaml locations where the keys are expected to be in snap/keys
("snapcraft.yaml", "snap", "snap/keys"),
("snap/snapcraft.yaml", "snap", "snap/keys"),
(".snapcraft.yaml", "snap", "snap/keys"),
# snapcraft.yaml location where the keys are expected to be somewhere else
("build-aux/snap/snapcraft.yaml", "build-aux/snap", "build-aux/snap/keys"),
],
)
def test_local_keys_path(new_dir, project_location, assets_dir, expected_keys_path):
"""Check that _get_local_keys_path() is correct given the location of snapcraft.yaml."""
snap_dir = new_dir / Path(project_location).parent
snap_dir.mkdir(exist_ok=True, parents=True)

# The project itself doesn't really matter, but must exist
project_yaml = snap_dir / "snapcraft.yaml"
project_yaml.write_text(PARSE_INFO_PROJECT)

app = application.create_app()
keys_dir = Path(assets_dir) / "keys"

# If the keys dir doesn't exist the method should return None
assert not keys_dir.is_dir()
assert app._get_local_keys_path() is None

keys_dir.mkdir(exist_ok=True, parents=True)
assert app._get_local_keys_path() == Path(expected_keys_path)

0 comments on commit 639bcc3

Please sign in to comment.