Skip to content

Commit

Permalink
Only delete Menu when empty (#218)
Browse files Browse the repository at this point in the history
* Only remove Windows start menu directory if empty

* Add install/remove test

* Remove redundant warning for terminal profile

* Only remove Linux directory entry when menu is empty

* Add news item

* Check if menu location exists before iterating

* Update menuinst/platforms/win.py

Co-authored-by: jaimergp <jaimergp@users.noreply.github.com>

---------

Co-authored-by: jaimergp <jaimergp@users.noreply.github.com>
  • Loading branch information
marcoesters and jaimergp authored Jun 6, 2024
1 parent 26deb4d commit 9e53a88
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion menuinst/platforms/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def create(self) -> Tuple[os.PathLike]:
return (path,)

def remove(self) -> Tuple[os.PathLike]:
unlink(self.directory_entry_location, missing_ok=True)
for fn in os.listdir(self.desktop_entries_location):
if fn.startswith(f"{self.render(self.name, slug=True)}_"):
# found one shortcut, so don't remove the name from menu
return (self.directory_entry_location,)
unlink(self.directory_entry_location, missing_ok=True)
self._remove_this_menu()
return (self.directory_entry_location,)

Expand Down
13 changes: 10 additions & 3 deletions menuinst/platforms/win.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ def create(self) -> Tuple[os.PathLike]:
return (self.start_menu_location,)

def remove(self) -> Tuple[os.PathLike]:
log.debug("Removing %s", self.start_menu_location)
shutil.rmtree(self.start_menu_location, ignore_errors=True)
# Only remove if the Start Menu directory is empty in case applications share a folder.
menu_location = Path(self.start_menu_location)
if menu_location.exists():
try:
# Check directory contents. If empty, it will raise StopIteration
# and only in that case we delete the directory.
next(menu_location.iterdir())
except StopIteration:
log.debug("Removing %s", self.start_menu_location)
shutil.rmtree(self.start_menu_location, ignore_errors=True)
return (self.start_menu_location,)

@property
Expand Down Expand Up @@ -344,7 +352,6 @@ def _add_remove_windows_terminal_profile(self, location: Path, remove: bool = Fa

if remove:
if index < 0:
log.warning(f"Could not find terminal profile for {name}.")
return
del settings["profiles"]["list"][index]
else:
Expand Down
19 changes: 19 additions & 0 deletions news/218-only-delete-empty-menus
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Only delete menus when they do not contain any items. (#218)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
24 changes: 24 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ def test_install_prefix(delete_files):
check_output_from_shortcut(delete_files, "sys-prefix.json", expected_output=sys.prefix)


def test_install_remove(tmp_path, delete_files):
metadata = DATA / "jsons" / "sys-prefix.json"
(tmp_path / ".nonadmin").touch()
paths = set(install(metadata, target_prefix=tmp_path, base_prefix=tmp_path, _mode="user"))
delete_files.extend(paths)
files_found = set(filter(lambda x: x.exists(), paths))
assert files_found == paths
if PLATFORM != "osx":
metadata_2 = json.loads(metadata.read_text())
metadata_2["menu_items"][0]["name"] = "Sys.Prefix.2"
paths_2 = set(
install(metadata_2, target_prefix=tmp_path, base_prefix=tmp_path, _mode="user")
)
delete_files.extend(paths_2)
files_found = set(filter(lambda x: x.exists(), paths_2.union(paths)))
assert files_found == paths_2.union(paths)
remove(metadata_2, target_prefix=tmp_path, base_prefix=tmp_path, _mode="user")
files_found = set(filter(lambda x: x.exists(), paths_2.union(paths)))
assert files_found == paths
remove(metadata, target_prefix=tmp_path, base_prefix=tmp_path, _mode="user")
files_found = set(filter(lambda x: x.exists(), paths))
assert files_found == set()


def test_overwrite_existing_shortcuts(delete_files, caplog):
"""Test that overwriting shortcuts works without errors by running installation twice."""
check_output_from_shortcut(
Expand Down

0 comments on commit 9e53a88

Please sign in to comment.