Skip to content

Commit

Permalink
feat(craft-application): migrate version command
Browse files Browse the repository at this point in the history
This does not include the legacy snapcraft version
  • Loading branch information
syu-w committed Feb 26, 2024
1 parent b683207 commit f1395d5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 98 deletions.
20 changes: 8 additions & 12 deletions snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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,
],
Expand Down
20 changes: 8 additions & 12 deletions snapcraft/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,7 +125,6 @@
craft_cli.CommandGroup(
"Other",
[
commands.core22.VersionCommand,
commands.core22.LintCommand,
commands.core22.InitCommand,
],
Expand Down Expand Up @@ -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(

Check warning on line 207 in snapcraft/cli.py

View check run for this annotation

Codecov / codecov/patch

snapcraft/cli.py#L207

Added line #L207 was not covered by tests
"Options -t and --trace are deprecated, use --verbosity=debug instead."
)
emit.set_mode(EmitterMode.DEBUG)

Check warning on line 210 in snapcraft/cli.py

View check run for this annotation

Codecov / codecov/patch

snapcraft/cli.py#L210

Added line #L210 was not covered by tests

dispatcher.load_command(None)
dispatcher.run()
dispatcher.load_command(None)
dispatcher.run()
emit.ended_ok()


Expand Down
2 changes: 0 additions & 2 deletions snapcraft/commands/core22/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
)
from .upload import StoreUploadCommand
from .validation_sets import StoreEditValidationSetsCommand
from .version import VersionCommand

__all__ = [
"BuildCommand",
Expand Down Expand Up @@ -86,5 +85,4 @@
"StoreUploadCommand",
"StoreWhoAmICommand",
"TryCommand",
"VersionCommand",
]
34 changes: 0 additions & 34 deletions snapcraft/commands/core22/version.py

This file was deleted.

6 changes: 0 additions & 6 deletions snapcraft/commands/unimplemented.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 12 additions & 6 deletions tests/unit/cli/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(),
]


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__}")
26 changes: 0 additions & 26 deletions tests/unit/commands/test_version.py

This file was deleted.

0 comments on commit f1395d5

Please sign in to comment.