Skip to content

Commit

Permalink
cli: catch craft-provider errors
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <[email protected]>
  • Loading branch information
mr-cal authored and sergiusens committed Jul 26, 2023
1 parent dbe76f2 commit 5110612
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion snapcraft/cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion tests/unit/cli/test_exit.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,6 +21,7 @@
import craft_store.errors
import pytest
from craft_cli import CraftError
from craft_providers import ProviderError

from snapcraft import cli

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5110612

Please sign in to comment.