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

feat(lifecycle): add deprecated snap command #4589

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from craft_providers import bases
from overrides import override

import snapcraft.commands
from snapcraft import cli, errors, models, services
from snapcraft.commands import unimplemented
from snapcraft.models import Architecture
Expand Down Expand Up @@ -253,8 +254,8 @@ def main() -> int:
craft_app_commands.lifecycle.StageCommand,
craft_app_commands.lifecycle.PrimeCommand,
craft_app_commands.lifecycle.PackCommand,
snapcraft.commands.lifecycle.SnapCommand, # Hidden (legacy compatibility)
unimplemented.RemoteBuild,
unimplemented.Snap, # Hidden (legacy compatibility)
unimplemented.Plugins,
unimplemented.ListPlugins,
unimplemented.Try,
Expand Down
2 changes: 2 additions & 0 deletions snapcraft/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"""Snapcraft commands."""

from . import core22, legacy
from .lifecycle import SnapCommand

__all__ = [
"core22",
"legacy",
"SnapCommand",
]
48 changes: 48 additions & 0 deletions snapcraft/commands/lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- 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/>.

"""Snapcraft lifecycle commands."""

import argparse
from typing import Any

import craft_application.commands
from craft_cli import emit
from typing_extensions import override

# pylint: disable=too-many-ancestors
tigarmo marked this conversation as resolved.
Show resolved Hide resolved


class SnapCommand(craft_application.commands.lifecycle.PackCommand):
"""Deprecated legacy command to pack the final snap payload."""

name = "snap"
hidden = True

@override
def _run(
self,
parsed_args: argparse.Namespace,
step_name: str | None = None,
**kwargs: Any,
) -> None:
emit.progress(
"Warning: the 'snap' command is deprecated and will be removed "
"in a future release of Snapcraft. Please use 'pack' instead.",
permanent=True,
)

super()._run(parsed_args, step_name, **kwargs)
42 changes: 0 additions & 42 deletions snapcraft/commands/unimplemented.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,48 +180,6 @@ class Validate(
pass


class Build(
UnimplementedMixin, commands.core22.BuildCommand
): # noqa: D101 (missing docstring)
pass


class Clean(
UnimplementedMixin, commands.core22.CleanCommand
): # noqa: D101 (missing docstring)
pass


class Pack(
UnimplementedMixin, commands.core22.PackCommand
): # noqa: D101 (missing docstring)
pass


class Prime(
UnimplementedMixin, commands.core22.PrimeCommand
): # noqa: D101 (missing docstring)
pass


class Pull(
UnimplementedMixin, commands.core22.PullCommand
): # noqa: D101 (missing docstring)
pass


class Snap(
UnimplementedMixin, commands.core22.SnapCommand
): # noqa: D101 (missing docstring)
pass


class Stage(
UnimplementedMixin, commands.core22.StageCommand
): # noqa: D101 (missing docstring)
pass


class Try(
UnimplementedMixin, commands.core22.TryCommand
): # noqa: D101 (missing docstring)
Expand Down
57 changes: 32 additions & 25 deletions tests/unit/commands/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,23 @@

import pytest

from snapcraft.commands.core22.lifecycle import (
BuildCommand,
CleanCommand,
PackCommand,
PrimeCommand,
PullCommand,
SnapCommand,
StageCommand,
TryCommand,
)
import snapcraft.commands.core22.lifecycle as core22_lifecycle
from snapcraft.application import APP_METADATA
from snapcraft.commands import lifecycle


@pytest.mark.parametrize(
"cmd_name,cmd_class",
[
("pull", PullCommand),
("build", BuildCommand),
("stage", StageCommand),
("prime", PrimeCommand),
("clean", CleanCommand),
("try", TryCommand),
("pull", core22_lifecycle.PullCommand),
("build", core22_lifecycle.BuildCommand),
("stage", core22_lifecycle.StageCommand),
("prime", core22_lifecycle.PrimeCommand),
("clean", core22_lifecycle.CleanCommand),
("try", core22_lifecycle.TryCommand),
],
)
def test_lifecycle_command(cmd_name, cmd_class, mocker):
def test_core22_lifecycle_command(cmd_name, cmd_class, mocker):
lifecycle_run_mock = mocker.patch("snapcraft.parts.lifecycle.run")
cmd = cmd_class(None)
cmd.run(argparse.Namespace(parts=["part1", "part2"]))
Expand All @@ -54,11 +47,11 @@ def test_lifecycle_command(cmd_name, cmd_class, mocker):
@pytest.mark.parametrize(
"cmd_name,cmd_class",
[
("pack", PackCommand),
("snap", SnapCommand),
("pack", core22_lifecycle.PackCommand),
("snap", core22_lifecycle.SnapCommand),
],
)
def test_pack_command(mocker, cmd_name, cmd_class):
def test_core22_pack_command(mocker, cmd_name, cmd_class):
lifecycle_run_mock = mocker.patch("snapcraft.parts.lifecycle.run")
cmd = cmd_class(None)
cmd.run(argparse.Namespace(directory=None, output=None, compression=None))
Expand All @@ -72,11 +65,11 @@ def test_pack_command(mocker, cmd_name, cmd_class):
@pytest.mark.parametrize(
"cmd_name,cmd_class",
[
("pack", PackCommand),
("snap", SnapCommand),
("pack", core22_lifecycle.PackCommand),
("snap", core22_lifecycle.SnapCommand),
],
)
def test_pack_command_with_output(mocker, cmd_name, cmd_class):
def test_core22_pack_command_with_output(mocker, cmd_name, cmd_class):
lifecycle_run_mock = mocker.patch("snapcraft.parts.lifecycle.run")
pack_mock = mocker.patch("snapcraft.pack.pack_snap")
cmd = cmd_class(None)
Expand All @@ -90,10 +83,24 @@ def test_pack_command_with_output(mocker, cmd_name, cmd_class):
assert pack_mock.mock_calls == []


def test_pack_command_with_directory(mocker):
def test_core22_pack_command_with_directory(mocker):
lifecycle_run_mock = mocker.patch("snapcraft.parts.lifecycle.run")
pack_mock = mocker.patch("snapcraft.pack.pack_snap")
cmd = PackCommand(None)
cmd = core22_lifecycle.PackCommand(None)
cmd.run(argparse.Namespace(directory=".", output=None, compression=None))
assert lifecycle_run_mock.mock_calls == []
assert pack_mock.mock_calls[0] == call(".", output=None)


def test_snap_command_fallback(tmp_path, emitter, mocker, fake_services):
"""Test that the snap command is falling back to the pack command."""
parsed_args = argparse.Namespace(parts=[], output=tmp_path)
mock_pack = mocker.patch("craft_application.commands.lifecycle.PackCommand._run")
cmd = lifecycle.SnapCommand({"app": APP_METADATA, "services": fake_services})
cmd.run(parsed_args=parsed_args)
mock_pack.assert_called_once()
emitter.assert_progress(
"Warning: the 'snap' command is deprecated and will be removed "
"in a future release of Snapcraft. Please use 'pack' instead.",
permanent=True,
)
11 changes: 11 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,14 @@ def package_service(default_project, default_factory):


# pylint: enable=import-outside-toplevel
syu-w marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture()
def fake_services(default_factory, lifecycle_service, package_service):
lifecycle_service.setup()
default_factory.lifecycle = lifecycle_service

package_service.setup()
default_factory.package = package_service

return default_factory