diff --git a/snapcraft/application.py b/snapcraft/application.py index a361c18f86..008f3b50d2 100644 --- a/snapcraft/application.py +++ b/snapcraft/application.py @@ -197,6 +197,8 @@ def _get_dispatcher(self) -> craft_cli.Dispatcher: if "core24" in (base, build_base) or build_base == "devel": # We know for sure that we're handling a core24 project self._known_core24 = True + elif any(arg in ("version", "--version", "-V") for arg in sys.argv): + pass else: raise errors.ClassicFallback() @@ -214,18 +216,12 @@ def _get_dispatcher(self) -> craft_cli.Dispatcher: craft_cli.emit.trace("pre-parsing arguments...") # Workaround for the fact that craft_cli requires a command. # https://github.com/canonical/craft-cli/issues/141 - if "--version" in sys.argv or "-V" in sys.argv: - try: - global_args = dispatcher.pre_parse_args(["pull", *sys.argv[1:]]) - except craft_cli.ArgumentParsingError: - global_args = dispatcher.pre_parse_args(sys.argv[1:]) + if any(arg in ("--version", "-V") for arg in sys.argv) and ( + "version" not in sys.argv + ): + global_args = dispatcher.pre_parse_args(["version", *sys.argv[1:]]) else: global_args = dispatcher.pre_parse_args(sys.argv[1:]) - - if global_args.get("version"): - craft_cli.emit.ended_ok() - print(f"{self.app.name} {self.app.version}") - sys.exit(0) except craft_cli.ProvideHelpException as err: print(err, file=sys.stderr) # to stderr, as argparse normally does craft_cli.emit.ended_ok() @@ -348,8 +344,8 @@ def create_app() -> Snapcraft: ) app.add_command_group( "Other", - [ - unimplemented.Version, + list(craft_app_commands.get_other_command_group().commands) + + [ unimplemented.Lint, unimplemented.Init, ], diff --git a/snapcraft/cli.py b/snapcraft/cli.py index 3945a33a92..d3f3fe0b21 100644 --- a/snapcraft/cli.py +++ b/snapcraft/cli.py @@ -30,7 +30,7 @@ import snapcraft import snapcraft_legacy -from snapcraft import __version__, errors, store, utils +from snapcraft import errors, store, utils from snapcraft.parts import plugins from snapcraft.remote import RemoteBuildError from snapcraft_legacy.cli import legacy @@ -125,7 +125,6 @@ craft_cli.CommandGroup( "Other", [ - commands.core22.VersionCommand, commands.core22.LintCommand, commands.core22.InitCommand, ], @@ -204,17 +203,14 @@ def get_dispatcher() -> craft_cli.Dispatcher: def _run_dispatcher( dispatcher: craft_cli.Dispatcher, global_args: Dict[str, Any] ) -> None: - if global_args.get("version"): - emit.message(f"snapcraft {__version__}") - else: - if global_args.get("trace"): - emit.message( - "Options -t and --trace are deprecated, use --verbosity=debug instead." - ) - emit.set_mode(EmitterMode.DEBUG) + if global_args.get("trace"): + emit.message( + "Options -t and --trace are deprecated, use --verbosity=debug instead." + ) + emit.set_mode(EmitterMode.DEBUG) - dispatcher.load_command(None) - dispatcher.run() + dispatcher.load_command(None) + dispatcher.run() emit.ended_ok() diff --git a/snapcraft/commands/core22/__init__.py b/snapcraft/commands/core22/__init__.py index 2017b92f9a..747420746c 100644 --- a/snapcraft/commands/core22/__init__.py +++ b/snapcraft/commands/core22/__init__.py @@ -52,7 +52,6 @@ ) from .upload import StoreUploadCommand from .validation_sets import StoreEditValidationSetsCommand -from .version import VersionCommand __all__ = [ "BuildCommand", @@ -86,5 +85,4 @@ "StoreUploadCommand", "StoreWhoAmICommand", "TryCommand", - "VersionCommand", ] diff --git a/snapcraft/commands/core22/version.py b/snapcraft/commands/core22/version.py deleted file mode 100644 index 74e7ac04f7..0000000000 --- a/snapcraft/commands/core22/version.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- -# -# Copyright 2022 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 version command.""" - -from craft_cli import BaseCommand, emit - -from snapcraft import __version__ - - -class VersionCommand(BaseCommand): - """Show the snapcraft version.""" - - name = "version" - help_msg = "Show the application version and exit" - overview = "Show the application version and exit" - common = True - - def run(self, parsed_args): - """Run the command.""" - emit.message(f"snapcraft {__version__}") diff --git a/snapcraft/commands/unimplemented.py b/snapcraft/commands/unimplemented.py index 44ed694269..d14a68bd68 100644 --- a/snapcraft/commands/unimplemented.py +++ b/snapcraft/commands/unimplemented.py @@ -276,12 +276,6 @@ class EditValidationSets( pass -class Version( - UnimplementedMixin, commands.core22.VersionCommand -): # noqa: D101 (missing docstring) - pass - - class RemoteBuild( UnimplementedMixin, commands.core22.RemoteBuildCommand ): # noqa: D101 (missing docstring) diff --git a/tests/unit/cli/test_version.py b/tests/unit/cli/test_version.py index ccb58ee945..0a23c0ca49 100644 --- a/tests/unit/cli/test_version.py +++ b/tests/unit/cli/test_version.py @@ -18,25 +18,31 @@ import sys from unittest.mock import call -from snapcraft import __version__, cli +from snapcraft import __version__, application def test_version_command(mocker): mocker.patch.object(sys, "argv", ["cmd", "version"]) + app = application.create_app() mock_version_cmd = mocker.patch( - "snapcraft.commands.core22.version.VersionCommand.run" + "craft_application.commands.other.VersionCommand.run" ) - cli.run() - assert mock_version_cmd.mock_calls == [call(argparse.Namespace())] + app.run() + assert mock_version_cmd.mock_calls == [ + call(argparse.Namespace()), + call().__bool__(), # pylint: disable=unnecessary-dunder-call + ] def test_version_argument(mocker, emitter): mocker.patch.object(sys, "argv", ["cmd", "--version"]) - cli.run() + app = application.create_app() + app.run() emitter.assert_message(f"snapcraft {__version__}") def test_version_argument_with_command(mocker, emitter): mocker.patch.object(sys, "argv", ["cmd", "--version", "version"]) - cli.run() + app = application.create_app() + app.run() emitter.assert_message(f"snapcraft {__version__}") diff --git a/tests/unit/commands/test_version.py b/tests/unit/commands/test_version.py deleted file mode 100644 index d99d5cc9f1..0000000000 --- a/tests/unit/commands/test_version.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- -# -# Copyright 2022 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/>. - -from argparse import Namespace - -from snapcraft import __version__ -from snapcraft.commands.core22.version import VersionCommand - - -def test_version_command(emitter): - cmd = VersionCommand(None) - cmd.run(Namespace()) - emitter.assert_message(f"snapcraft {__version__}")