diff --git a/menuinst/platforms/linux.py b/menuinst/platforms/linux.py index df811035..3eba00bd 100644 --- a/menuinst/platforms/linux.py +++ b/menuinst/platforms/linux.py @@ -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: diff --git a/news/201-linux-render-all-menu-name-placeholders b/news/201-linux-render-all-menu-name-placeholders new file mode 100644 index 00000000..bd96bd8a --- /dev/null +++ b/news/201-linux-render-all-menu-name-placeholders @@ -0,0 +1,19 @@ +### Enhancements + +* + +### Bug fixes + +* Render all menu name instances for Linux. (#201) + +### Deprecations + +* + +### Docs + +* + +### Other + +* diff --git a/tests/test_api.py b/tests/test_api.py index b22aaa01..9689bf8d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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): @@ -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):