Skip to content
Open
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
10 changes: 6 additions & 4 deletions snapcraft/parts/desktop_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ def _parse_and_reformat_section_exec(self, section: str):
def _parse_and_reformat_section(
self, *, section: str, icon_path: str | None = None
):
if "Exec" not in self._parser[section]:
raise errors.DesktopFileError(self._filename, "missing 'Exec' key")

self._parse_and_reformat_section_exec(section)
# Exec is not required: desktop entries that are D-Bus- or
# systemd-activated (often with NoDisplay=true) are valid without it,
# per the Desktop Entry specification and desktop-file-validate. Only
# reformat the Exec line when it is present.
if "Exec" in self._parser[section]:
self._parse_and_reformat_section_exec(section)

if "Icon" in self._parser[section]:
icon = self._parser[section]["Icon"]
Expand Down
24 changes: 21 additions & 3 deletions tests/unit/parts/test_desktop_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,34 @@ def test_no_desktop_section(new_dir):


def test_missing_exec_entry(new_dir):
"""A desktop entry without an Exec key is written through unchanged.

Exec is not required for D-Bus- or systemd-activated entries (regression
test for https://github.com/canonical/snapcraft/issues/5799).
"""
with open("foo.desktop", "w") as desktop_file:
print("[Desktop Entry]", file=desktop_file)
print("Icon=foo", file=desktop_file)
print("Type=Application", file=desktop_file)
print("Name=Foo", file=desktop_file)
print("NoDisplay=true", file=desktop_file)

d = DesktopFile(
snap_name="foo",
app_name="foo",
filename="foo.desktop",
prime_dir=new_dir,
)
d.write(gui_dir=new_dir)

with pytest.raises(errors.DesktopFileError):
d.write(gui_dir=new_dir)
written = new_dir / "foo.desktop"
assert written.exists()
with written.open() as desktop_file:
assert desktop_file.read() == dedent(
"""\
[Desktop Entry]
Type=Application
Name=Foo
NoDisplay=true

"""
)