diff --git a/craft_application/services/project.py b/craft_application/services/project.py index 8104166f0..e39f7c262 100644 --- a/craft_application/services/project.py +++ b/craft_application/services/project.py @@ -256,7 +256,7 @@ def render_for( if not getattr(project_model, field, None): missing_fields.add(field) if missing_fields: - missing = ", ".join(sorted(missing_fields)) + missing = ", ".join(repr(field) for field in sorted(missing_fields)) raise errors.CraftValidationError( f"'adopt-info' not set and required fields are missing: {missing}" ) diff --git a/tests/unit/services/test_project.py b/tests/unit/services/test_project.py index 7dabf4542..a6075033e 100644 --- a/tests/unit/services/test_project.py +++ b/tests/unit/services/test_project.py @@ -13,6 +13,7 @@ # with this program. If not, see . """Unit tests for the ProjectService.""" +import dataclasses import pathlib import textwrap from typing import cast @@ -390,3 +391,23 @@ def test_get_already_rendered(project_service: ProjectService): rendered = project_service.render_once() assert project_service.get() == rendered + + +def test_mandatory_adoptable_fields( + app_metadata, project_service: ProjectService, fake_project_file: pathlib.Path +): + """Verify if mandatory adoptable fields are defined if not using adopt-info.""" + project_service._app = dataclasses.replace( + app_metadata, mandatory_adoptable_fields=["version", "license"] + ) + + project_yaml = fake_project_file.read_text() + fake_project_file.write_text(project_yaml.replace("license:", "# licence:")) + + with pytest.raises(errors.CraftValidationError) as exc_info: + _ = project_service.render_once() + + assert ( + str(exc_info.value) + == "'adopt-info' not set and required fields are missing: 'license'" + ) diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 35022890e..8c15a5aeb 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -16,7 +16,6 @@ """Unit tests for craft-application app classes.""" import argparse -import dataclasses import importlib import importlib.metadata import logging @@ -1142,25 +1141,6 @@ def test_register_plugins_default(mocker, app_metadata, fake_services): assert reg.call_count == 0 -def test_mandatory_adoptable_fields( - tmp_path, app_metadata, fake_services, fake_project_file -): - """Verify if mandatory adoptable fields are defined if not using adopt-info.""" - app_metadata = dataclasses.replace( - app_metadata, mandatory_adoptable_fields=["license"] - ) - - app = application.Application(app_metadata, fake_services) - - with pytest.raises(errors.CraftValidationError) as exc_info: - _ = app.get_project(build_for=get_host_architecture()) - - assert ( - str(exc_info.value) - == "Required field 'license' is not set and 'adopt-info' not used." - ) - - @pytest.fixture def grammar_project_mini(tmp_path): """A project that builds on amd64 to riscv64 and s390x."""