diff --git a/snapcraft/application.py b/snapcraft/application.py index 21699afd36..75b96e65d6 100644 --- a/snapcraft/application.py +++ b/snapcraft/application.py @@ -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", @@ -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.""" diff --git a/snapcraft/services/lifecycle.py b/snapcraft/services/lifecycle.py index 715e761029..83b64875e5 100644 --- a/snapcraft/services/lifecycle.py +++ b/snapcraft/services/lifecycle.py @@ -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): @@ -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. diff --git a/tests/unit/services/test_lifecycle.py b/tests/unit/services/test_lifecycle.py index 96e33f8ccd..8df1dcd764 100644 --- a/tests/unit/services/test_lifecycle.py +++ b/tests/unit/services/test_lifecycle.py @@ -19,6 +19,7 @@ import platform import shutil import sys +from pathlib import Path from unittest import mock import pytest @@ -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) diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 376612c3be..9706ba1665 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -17,7 +17,6 @@ import json import os import sys -from pathlib import Path from textwrap import dedent import craft_cli @@ -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)