Skip to content

Commit

Permalink
Render all menu name instances for Linux (#201)
Browse files Browse the repository at this point in the history
* Render remaining instances of menu name for Linux

* Add test

* Do not remove shortcuts in placeholder test

* Add news item

* Explicitly render name everywhere
  • Loading branch information
marcoesters committed May 9, 2024
1 parent 0aa4be1 commit fc5d064
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
14 changes: 8 additions & 6 deletions menuinst/platforms/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,29 @@ def _write_directory_entry(self) -> str:
#

def _remove_this_menu(self):
log.debug("Editing %s to remove %s config", self.menu_config_location, self.name)
log.debug(
"Editing %s to remove %s config", self.menu_config_location, self.render(self.name)
)
tree = ElementTree.parse(self.menu_config_location)
root = tree.getroot()
for elt in root.findall("Menu"):
if elt.find("Name").text == self.name:
if elt.find("Name").text == self.render(self.name):
root.remove(elt)
self._write_menu_file(tree)

def _has_this_menu(self) -> bool:
root = ElementTree.parse(self.menu_config_location).getroot()
return any(e.text == self.name for e in root.findall("Menu/Name"))
return any(e.text == self.render(self.name) for e in root.findall("Menu/Name"))

def _add_this_menu(self):
log.debug("Editing %s to add %s config", self.menu_config_location, self.name)
log.debug("Editing %s to add %s config", self.menu_config_location, self.render(self.name))
tree = ElementTree.parse(self.menu_config_location)
root = tree.getroot()
menu_elt = add_xml_child(root, "Menu")
add_xml_child(menu_elt, "Name", self.name)
add_xml_child(menu_elt, "Name", self.render(self.name))
add_xml_child(menu_elt, "Directory", f"{self.render(self.name, slug=True)}.directory")
inc_elt = add_xml_child(menu_elt, "Include")
add_xml_child(inc_elt, "Category", self.name)
add_xml_child(inc_elt, "Category", self.render(self.name))
self._write_menu_file(tree)

def _is_valid_menu_file(self) -> bool:
Expand Down
19 changes: 19 additions & 0 deletions news/201-linux-render-all-menu-name-placeholders
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Render all menu name instances for Linux. (#201)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
41 changes: 33 additions & 8 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
from tempfile import NamedTemporaryFile, mkdtemp
from time import sleep, time
from typing import Iterable, Tuple
from xml.etree import ElementTree

import pytest
from conftest import DATA, PLATFORM

from menuinst.api import install, remove
from menuinst.platforms.osx import _lsregister
from menuinst.utils import DEFAULT_PREFIX, logged_run
from menuinst.utils import DEFAULT_PREFIX, logged_run, slugify


def _poll_for_file_contents(path, timeout=10):
Expand Down Expand Up @@ -144,19 +145,43 @@ def test_install_prefix(delete_files):
check_output_from_shortcut(delete_files, "sys-prefix.json", expected_output=sys.prefix)


@pytest.mark.skipif(PLATFORM != "win", reason="Windows only")
@pytest.mark.skipif(PLATFORM == "osx", reason="No menu names on MacOS")
def test_placeholders_in_menu_name(delete_files):
_, paths, tmp_base_path, _ = check_output_from_shortcut(
delete_files,
"sys-prefix.json",
expected_output=sys.prefix,
remove_after=False,
)
for path in paths:
if path.suffix == ".lnk" and "Start Menu" in path.parts:
assert path.parent.name == f"Sys.Prefix {Path(tmp_base_path).name}"
break
else:
raise AssertionError("Didn't find Start Menu")
if PLATFORM == "win":
for path in paths:
if path.suffix == ".lnk" and "Start Menu" in path.parts:
assert path.parent.name == f"Sys.Prefix {Path(tmp_base_path).name}"
break
else:
raise AssertionError("Didn't find Start Menu")
elif PLATFORM == "linux":
config_directory = Path(os.environ.get("XDG_CONFIG_HOME", "~/.config")).expanduser()
desktop_directory = (
Path(os.environ.get("XDG_DATA_HOME", "~/.local/share")).expanduser()
/ "desktop-directories"
)
menu_config_location = config_directory / "menus" / "applications.menu"
rendered_name = f"Sys.Prefix {Path(tmp_base_path).name}"
slugified_name = slugify(rendered_name)

entry_file = desktop_directory / f"{slugified_name}.directory"
assert entry_file.exists()
entry = entry_file.read_text().splitlines()
assert f"Name={rendered_name}" in entry

tree = ElementTree.parse(menu_config_location)
root = tree.getroot()
assert rendered_name in [elt.text for elt in root.findall("Menu/Name")]
assert f"{slugified_name}.directory" in [
elt.text for elt in root.findall("Menu/Directory")
]
assert rendered_name in [elt.text for elt in root.findall("Menu/Include/Category")]


def test_precommands(delete_files):
Expand Down

0 comments on commit fc5d064

Please sign in to comment.