Skip to content

Commit

Permalink
fix(publish-lib): exit with status 1 on error (#2042)
Browse files Browse the repository at this point in the history
Fixes #1981

CRAFT-3830
  • Loading branch information
lengau authored Dec 17, 2024
1 parent 0977fbf commit f328234
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions charmcraft/application/commands/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,7 @@ def run(self, parsed_args):
analysis.append((lib_data, error_message))

# work on the analysis result, showing messages to the user if not programmatic output
return_code = 0
for lib_data, error_message in analysis:
if error_message is None:
store.create_library_revision(
Expand All @@ -1555,6 +1556,7 @@ def run(self, parsed_args):
)
else:
message = error_message
return_code = 1
if not parsed_args.format:
emit.message(message)

Expand All @@ -1577,6 +1579,8 @@ def run(self, parsed_args):
output_data.append(datum)
emit.message(cli.format_content(output_data, parsed_args.format))

return return_code


class FetchLibCommand(CharmcraftCommand):
"""Fetch one or more charm libraries."""
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/commands/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
from charmcraft import errors, store
from charmcraft.application import commands
from charmcraft.application.commands import SetResourceArchitecturesCommand
from charmcraft.application.commands.store import FetchLibs, LoginCommand
from charmcraft.application.commands import store as store_commands
from charmcraft.application.commands.store import (
FetchLibs,
LoginCommand,
PublishLibCommand,
)
from charmcraft.application.main import APP_METADATA
from charmcraft.models.project import CharmLib
from charmcraft.services import CharmcraftServiceFactory
from charmcraft.store.models import Library
from charmcraft.utils import cli
from tests import get_fake_revision

Expand Down Expand Up @@ -119,6 +126,40 @@ def test_set_resource_architectures_output_json(emitter, updates, expected):
emitter.assert_json_output(expected)


def test_publish_lib_error(monkeypatch, new_path: pathlib.Path) -> None:
mock_service_factory = mock.Mock(spec=CharmcraftServiceFactory)
mock_service_factory.project.name = "test-project"
lib_path = new_path / "lib/charms/test_project/v0/my_lib.py"
lib_path.parent.mkdir(parents=True)
lib_path.write_text("LIBAPI=0\nLIBID='blah'\nLIBPATCH=1")

mock_store = mock.Mock()
mock_store.return_value.get_libraries_tips.return_value = {
("blah", 0): Library(
charm_name="test-project",
lib_id="blah",
lib_name="my_lib",
api=0,
patch=2,
content=None,
content_hash="",
),
}
monkeypatch.setattr(store_commands, "Store", mock_store)

cmd = PublishLibCommand({"app": APP_METADATA, "services": mock_service_factory})

assert (
cmd.run(
argparse.Namespace(
library="charms.test-project.v0.my_lib",
format=False,
)
)
== 1
)


@pytest.mark.parametrize(
("updates", "expected"),
[
Expand Down

0 comments on commit f328234

Please sign in to comment.