Skip to content

Commit

Permalink
fix: fix report of validation errors in expand-extensions
Browse files Browse the repository at this point in the history
Fixes #589
  • Loading branch information
tigarmo committed Jun 21, 2024
1 parent 9412ed5 commit 7718957
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
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
36 changes: 35 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,29 @@ 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):
wrong_yaml = copy.deepcopy(FULL_EXTENSION_PROJECT)
# Set a wrong base
# wrong_yaml["base"] = "ubuntu@invalid"

# Mix slices and debs
wrong_yaml["parts"]["foo"]["stage-packages"].append("some-chisel_slice")

# 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"
"- cannot mix packages and slices in stage-packages (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)

0 comments on commit 7718957

Please sign in to comment.