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

multiple error-related fixes #601

Merged
merged 2 commits into from
Jun 21, 2024
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
1 change: 1 addition & 0 deletions rockcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ProjectClass=project.Project,
BuildPlannerClass=project.BuildPlanner,
source_ignore_patterns=["*.rock"],
docs_url="https://documentation.ubuntu.com/rockcraft/en/stable",
)


Expand Down
3 changes: 2 additions & 1 deletion rockcraft/commands/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ExpandExtensionsCommand(AppCommand, abc.ABC):
@overrides
def run(self, parsed_args: argparse.Namespace) -> None:
"""Print the project's specification with the extensions expanded."""
project = Project.unmarshal(load_project(Path("rockcraft.yaml")))
project_path = Path("rockcraft.yaml")
project = Project.from_yaml_data(load_project(project_path), project_path)

emit.message(project.to_yaml()) # pylint: disable=no-member
10 changes: 10 additions & 0 deletions rockcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ def get_build_plan(self) -> list[BuildInfo]:

return build_infos

@override
@classmethod
def model_reference_slug(cls) -> str | None:
return "/reference/rockcraft.yaml"


class Project(YamlModelMixin, BuildPlanner, BaseProject): # type: ignore[misc]
"""Rockcraft project definition."""
Expand Down Expand Up @@ -512,6 +517,11 @@ def unmarshal(cls, data: dict[str, Any]) -> Self:

return cls(**data)

@override
@classmethod
def model_reference_slug(cls) -> str | None:
return "/reference/rockcraft.yaml"


def load_project(filename: Path) -> dict[str, Any]:
"""Load and unmarshal the project YAML file.
Expand Down
34 changes: 33 additions & 1 deletion tests/unit/commands/test_expand_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import copy
import re
import textwrap
from pathlib import Path

import pytest
from craft_application import util, errors

from rockcraft import extensions
from rockcraft.commands import ExpandExtensionsCommand

from tests.unit.testing.extensions import FULL_EXTENSION_YAML, FullExtension
from tests.unit.testing.extensions import (
FULL_EXTENSION_YAML,
FullExtension,
FULL_EXTENSION_PROJECT,
)

# The project with the extension (FullExtension) expanded
EXPECTED_EXPAND_EXTENSIONS = textwrap.dedent(
Expand Down Expand Up @@ -82,3 +90,27 @@ def test_expand_extensions(setup_extensions, emitter, new_dir):
cmd.run(argparse.Namespace())

emitter.assert_message(EXPECTED_EXPAND_EXTENSIONS)


def test_expand_extensions_error(setup_extensions, new_dir):
tigarmo marked this conversation as resolved.
Show resolved Hide resolved
wrong_yaml = copy.deepcopy(FULL_EXTENSION_PROJECT)

# Misconfigure the plugin
wrong_yaml["parts"]["foo"]["plugin"] = "nonexistent"

# Misconfigure a service
wrong_yaml["services"]["my-service"]["override"] = "invalid"

project_file = Path("rockcraft.yaml")
dumped = util.dump_yaml(wrong_yaml)
project_file.write_text(dumped)

expected_message = re.escape(
"Bad rockcraft.yaml content:\n"
"- plugin not registered: 'nonexistent' (in field 'parts.foo')\n"
"- unexpected value; permitted: 'merge', 'replace' (in field 'services.my-service.override')"
)

cmd = ExpandExtensionsCommand(None)
with pytest.raises(errors.CraftValidationError, match=expected_message):
cmd.run(argparse.Namespace())
48 changes: 21 additions & 27 deletions tests/unit/testing/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Fake Extensions for use in tests."""
import textwrap
from typing import Any

from craft_application import util
from overrides import override
from rockcraft.extensions.extension import Extension

Expand Down Expand Up @@ -103,29 +103,23 @@ def get_parts_snippet(self) -> dict[str, Any]:
return {"full-extension/new-part": {"plugin": "nil", "source": None}}


FULL_EXTENSION_YAML = textwrap.dedent(
f"""
name: project-with-extensions
version: latest
base: [email protected]
summary: Project with extensions
description: Project with extensions
license: Apache-2.0
platforms:
amd64:

extensions:
- {FullExtension.NAME}

parts:
foo:
plugin: nil
stage-packages:
- old-package

services:
my-service:
command: foo
override: merge
"""
)
FULL_EXTENSION_PROJECT = {
"name": "project-with-extensions",
"version": "latest",
"base": "[email protected]",
"summary": "Project with extensions",
"description": "Project with extensions",
"license": "Apache-2.0",
"platforms": {"amd64": None},
"extensions": [FullExtension.NAME],
"parts": {"foo": {"plugin": "nil", "stage-packages": ["old-package"]}},
"services": {
"my-service": {
"command": "foo",
"override": "merge",
}
},
}


FULL_EXTENSION_YAML = util.dump_yaml(FULL_EXTENSION_PROJECT)
Loading