diff --git a/charmcraft/application/commands/store.py b/charmcraft/application/commands/store.py index cfa24f267..43f3232a6 100644 --- a/charmcraft/application/commands/store.py +++ b/charmcraft/application/commands/store.py @@ -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( @@ -1555,6 +1556,7 @@ def run(self, parsed_args): ) else: message = error_message + return_code = 1 if not parsed_args.format: emit.message(message) @@ -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.""" diff --git a/tests/unit/commands/test_store.py b/tests/unit/commands/test_store.py index 9d68d4bfd..512f5cbf7 100644 --- a/tests/unit/commands/test_store.py +++ b/tests/unit/commands/test_store.py @@ -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 @@ -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"), [