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 22, 2024
1 parent e16472f commit 5463ba7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 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,6 +254,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)
16 changes: 16 additions & 0 deletions tests/unit/commands/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import pytest

import snapcraft.commands.core22.lifecycle as core22_lifecycle
from snapcraft.application import APP_METADATA
from snapcraft.commands import lifecycle


@pytest.mark.parametrize(
Expand Down Expand Up @@ -88,3 +90,17 @@ def test_core22_pack_command_with_directory(mocker):
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,
)

0 comments on commit 5463ba7

Please sign in to comment.