From 0815ce5d48aeea309eb1ba29e4e00e3f18215222 Mon Sep 17 00:00:00 2001 From: Callahan Kovacs Date: Wed, 26 Jul 2023 14:59:27 -0500 Subject: [PATCH] cli: catch craft-provider errors Signed-off-by: Callahan Kovacs --- snapcraft/cli.py | 6 +++++- tests/unit/cli/test_exit.py | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/snapcraft/cli.py b/snapcraft/cli.py index 0f1a17e405..94d22caf6a 100644 --- a/snapcraft/cli.py +++ b/snapcraft/cli.py @@ -1,6 +1,6 @@ # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # -# Copyright 2022 Canonical Ltd. +# Copyright 2022-2023 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 @@ -26,6 +26,7 @@ import craft_cli import craft_store from craft_cli import ArgumentParsingError, EmitterMode, ProvideHelpException, emit +from craft_providers import ProviderError import snapcraft import snapcraft_legacy @@ -290,6 +291,9 @@ def run(): # noqa: C901 except craft_store.errors.CraftStoreError as err: _emit_error(craft_cli.errors.CraftError(f"craft-store error: {err}")) retcode = 1 + except ProviderError as err: + _emit_error(craft_cli.errors.CraftError(f"craft-providers error: {err}")) + retcode = 1 except errors.LinterError as err: emit.error(craft_cli.errors.CraftError(f"linter error: {err}")) retcode = err.exit_code diff --git a/tests/unit/cli/test_exit.py b/tests/unit/cli/test_exit.py index 7264a79485..e2a853834d 100644 --- a/tests/unit/cli/test_exit.py +++ b/tests/unit/cli/test_exit.py @@ -1,6 +1,6 @@ # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # -# Copyright 2022 Canonical Ltd. +# Copyright 2022-2023 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 @@ -21,6 +21,7 @@ import craft_store.errors import pytest from craft_cli import CraftError +from craft_providers import ProviderError from snapcraft import cli @@ -50,6 +51,29 @@ def test_no_keyring_error(capsys, mocker): ) +def test_craft_providers_error(capsys, mocker): + """Catch craft-providers errors.""" + mocker.patch.object(sys, "argv", ["cmd", "pull"]) + mocker.patch.object(sys.stdin, "isatty", return_value=True) + mocker.patch( + "snapcraft.commands.lifecycle.PullCommand.run", + side_effect=ProviderError( + brief="test brief", + details="test details", + resolution="test resolution", + ), + ) + + cli.run() + + stderr = capsys.readouterr().err.splitlines() + + # Simple verification that our expected message is being printed + assert stderr[0].startswith("craft-providers error: test brief") + assert stderr[1].startswith("test details") + assert stderr[2].startswith("test resolution") + + @pytest.mark.parametrize("is_managed,report_errors", [(True, False), (False, True)]) def test_emit_error(emitter, mocker, is_managed, report_errors): mocker.patch("snapcraft.utils.is_managed_mode", return_value=is_managed)