Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only delete Menu when empty #218

Merged
merged 7 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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
11 changes: 8 additions & 3 deletions menuinst/platforms/win.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ 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.
try:
menu_location = Path(self.start_menu_location)
if menu_location.exists():
next(menu_location.iterdir())
except StopIteration:
log.debug("Removing %s", self.start_menu_location)
shutil.rmtree(self.start_menu_location, ignore_errors=True)
marcoesters marked this conversation as resolved.
Show resolved Hide resolved
return (self.start_menu_location,)

@property
Expand Down Expand Up @@ -344,7 +350,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
Loading