Skip to content

Commit

Permalink
test: move adopt-info test to project service
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Feb 21, 2025
1 parent 2ef6fd9 commit 6195408
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion craft_application/services/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/services/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the ProjectService."""

import dataclasses
import pathlib
import textwrap
from typing import cast
Expand Down Expand Up @@ -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'"
)
20 changes: 0 additions & 20 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""Unit tests for craft-application app classes."""

import argparse
import dataclasses
import importlib
import importlib.metadata
import logging
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 6195408

Please sign in to comment.