Skip to content

Commit

Permalink
feat(lifecycle): add deprecated snap command
Browse files Browse the repository at this point in the history
  • Loading branch information
syu-w committed Feb 16, 2024
1 parent c908f82 commit c978fe1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 25 deletions.
2 changes: 2 additions & 0 deletions snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -230,6 +231,7 @@ def main() -> int:
craft_app_commands.lifecycle.StageCommand,
craft_app_commands.lifecycle.PrimeCommand,
craft_app_commands.lifecycle.PackCommand,
snapcraft.commands.lifecycle.SnapCommand,
unimplemented.RemoteBuild,
unimplemented.Snap, # Hidden (legacy compatibility)
unimplemented.Plugins,
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


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)
62 changes: 37 additions & 25 deletions tests/unit/commands/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,22 @@

import pytest

from snapcraft.commands.core22.lifecycle import (
BuildCommand,
CleanCommand,
PackCommand,
PrimeCommand,
PullCommand,
SnapCommand,
StageCommand,
TryCommand,
)
import snapcraft.commands
import snapcraft.commands.core22.lifecycle as core22_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 +46,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 @@ -69,14 +61,34 @@ def test_pack_command(mocker, cmd_name, cmd_class):
]


def test_snap_command(tmp_path, emitter, mocker, app_metadata, mock_services):
"""Test that the snap command is falling back to the pack command."""
mock_services.package.pack.return_value = []
parsed_args = argparse.Namespace(parts=[], output=tmp_path)
cmd = snapcraft.commands.lifecycle.SnapCommand(
{"app": app_metadata, "services": mock_services}
)
cmd.run(parsed_args=parsed_args)
mock_services.package.pack.assert_called_once_with(
mock_services.lifecycle.prime_dir,
tmp_path,
)
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,
)
emitter.assert_progress("Packing...")


@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 +102,10 @@ 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)

0 comments on commit c978fe1

Please sign in to comment.