Skip to content

Commit

Permalink
fixup! projects: warn on "global" assign of plugs and slots
Browse files Browse the repository at this point in the history
  • Loading branch information
tigarmo committed Apr 18, 2023
1 parent acd2eb0 commit 1c9214f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
39 changes: 23 additions & 16 deletions snapcraft/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,6 @@ class ContentPlug(ProjectModel):

MANDATORY_ADOPTABLE_FIELDS = ("version", "summary", "description")

GLOBAL_PLUGS_SLOTS_WARNING = (
"Implicit {lowercase} assignment in {culprits}. "
"{uppercase}s should be assigned to the app that they apply, "
"and not implicitly assigned via the global '{lowercase}s:' "
"stanza which is intended for configuration only."
"\n(Reference: https://snapcraft.io/docs/snapcraft-interfaces)"
)


class Project(ProjectModel):
"""Snapcraft project definition.
Expand Down Expand Up @@ -456,10 +448,7 @@ def _validate_plugs(cls, plugs):
empty_plugs.append(plug_name)

if empty_plugs:
culprits = utils.humanize_list(empty_plugs, "and")
message = GLOBAL_PLUGS_SLOTS_WARNING.format(
lowercase="plug", uppercase="Plug", culprits=culprits
)
message = _format_global_keyword_warning("plug", empty_plugs)
emit.message(message)

return plugs
Expand All @@ -474,10 +463,7 @@ def _validate_slots(cls, slots):
empty_slots.append(slot_name)

if empty_slots:
culprits = utils.humanize_list(empty_slots, "and")
message = GLOBAL_PLUGS_SLOTS_WARNING.format(
lowercase="slot", uppercase="Slot", culprits=culprits
)
message = _format_global_keyword_warning("slot", empty_slots)
emit.message(message)

return slots
Expand Down Expand Up @@ -862,3 +848,24 @@ def _printable_field_location_split(location: str) -> Tuple[str, str]:
return field_name, repr(".".join(loc_split))

return field_name, "top-level"


def _format_global_keyword_warning(keyword: str, empty_entries: List[str]) -> str:
"""Create a warning message about global assignment in the ``keyword`` field.
:param keyword:
The top-level keyword that contains empty entries (currently either
"plug" or "slot").
:param empty_entries:
The entries inside the ``keyword`` dict that are empty.
:return:
A properly-formatted warning message.
"""
culprits = utils.humanize_list(empty_entries, "and")
return (
f"Warning: implicit {keyword.lower()} assignment in {culprits}. "
f"{keyword.capitalize()}s should be assigned to the app to which they apply, "
f"and not implicitly assigned via the global '{keyword.lower()}s:' "
"stanza which is intended for configuration only."
"\n(Reference: https://snapcraft.io/docs/snapcraft-interfaces)"
)
4 changes: 2 additions & 2 deletions tests/spread/core22/plugs-warn/expected_output.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Implicit plug assignment in 'desktop' and 'desktop-legacy'. Plugs should be assigned to the app that they apply, and not implicitly assigned via the global 'plugs:' stanza which is intended for configuration only.
Warning: implicit plug assignment in 'desktop' and 'desktop-legacy'. Plugs should be assigned to the app to which they apply, and not implicitly assigned via the global 'plugs:' stanza which is intended for configuration only.
(Reference: https://snapcraft.io/docs/snapcraft-interfaces)
Implicit slot assignment in 'network' and 'opengl'. Slots should be assigned to the app that they apply, and not implicitly assigned via the global 'slots:' stanza which is intended for configuration only.
Warning: implicit slot assignment in 'network' and 'opengl'. Slots should be assigned to the app to which they apply, and not implicitly assigned via the global 'slots:' stanza which is intended for configuration only.
(Reference: https://snapcraft.io/docs/snapcraft-interfaces)
Initializing parts lifecycle
8 changes: 4 additions & 4 deletions tests/unit/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ def test_project_global_plugs_warning(self, project_yaml_data, emitter):
data = project_yaml_data(plugs={"desktop": None, "desktop-legacy": None})
Project.unmarshal(data)
expected_message = (
"Implicit plug assignment in 'desktop' and 'desktop-legacy'. "
"Plugs should be assigned to the app that they apply, and not "
"Warning: implicit plug assignment in 'desktop' and 'desktop-legacy'. "
"Plugs should be assigned to the app to which they apply, and not "
"implicitly assigned via the global 'plugs:' stanza "
"which is intended for configuration only."
"\n(Reference: https://snapcraft.io/docs/snapcraft-interfaces)"
Expand All @@ -579,8 +579,8 @@ def test_project_global_slots_warning(self, project_yaml_data, emitter):
data = project_yaml_data(slots={"home": None, "removable-media": None})
Project.unmarshal(data)
expected_message = (
"Implicit slot assignment in 'home' and 'removable-media'. "
"Slots should be assigned to the app that they apply, and not "
"Warning: implicit slot assignment in 'home' and 'removable-media'. "
"Slots should be assigned to the app to which they apply, and not "
"implicitly assigned via the global 'slots:' stanza "
"which is intended for configuration only."
"\n(Reference: https://snapcraft.io/docs/snapcraft-interfaces)"
Expand Down

0 comments on commit 1c9214f

Please sign in to comment.