Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: catch craft-provider errors #4419

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -291,6 +292,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