Skip to content

Guarded rm#382

Open
abstract-official wants to merge 12 commits into
andreafrancia:masterfrom
abstract-official:guarded-rm
Open

Guarded rm#382
abstract-official wants to merge 12 commits into
andreafrancia:masterfrom
abstract-official:guarded-rm

Conversation

@abstract-official
Copy link
Copy Markdown

@abstract-official abstract-official commented Apr 17, 2026

Overview

This pull request guards trash-rm by showing files / dirs to be removed, and ask for confirmation before removing. It also fix the issue found in development that trash-empty will display “No trash directories to empty.” and start a new line, waiting for user input, as if waiting for user to answer to y/N. Now, both trash-empty and trash-rm exits after displaying a message indicating nothing is to be done.

When implementing this feature, several modules were separated from the 'empty' subpackage, forming a 'guard' subpackage, and reused in 'rm'.

Next steps

  • Create unit tests for these new codes and test against them.
  • Implement a flag that will skip comfirmation (already no confirmation when non-interactive, as it is with trash-empty)
  • Fix types in the guard module (and also in rm and the like if possible; my pyright actually complains a lot about the codebase)
  • Rename internal variables in guard package so they are more generic and not trash-empty-related.
  • (Suggestion, despite being somewhat off-topic): Add a trailing / for trashed directories' name. This hints the user that it contains files in it, not listed by trash-cli.

Copilot AI review requested due to automatic review settings April 17, 2026 16:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an interactive “guard” confirmation flow reused by both trash-empty and trash-rm, and prevents the “no items to delete” case from incorrectly blocking on user input.

Changes:

  • Introduce a reusable trashcli.guard confirmation layer (Guard, User, reply parsing, TTY detection).
  • Update trash-empty to skip prompting when there’s nothing to empty (prints message and exits).
  • Update trash-rm to display matched items and ask for confirmation before deleting.

Reviewed changes

Copilot reviewed 9 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
trashcli/rm/rm_cmd.py Collects matching trashed entries, prompts user via Guard, and deletes on confirmation
trashcli/rm/prepare_output_message.py Formats the confirmation prompt for trash-rm
trashcli/guard/user.py Generalizes user confirmation method (confirm)
trashcli/guard/parse_reply.py Adds shared reply parsing (y/default no)
trashcli/guard/is_input_interactive.py Adds shared stdin-TTY detection
trashcli/guard/guard.py Reuses guard logic with generalized User.confirm()
trashcli/empty/empty_cmd.py Switches to shared is_input_interactive
trashcli/empty/empty_action.py Avoids prompting when there are no trash dirs
tests/test_empty/components/test_user.py Updates import + method name to User.confirm()
tests/test_empty/components/test_parse_reply.py Updates import to shared parse_reply
tests/test_empty/components/test_guard.py Updates mock spec + method name to confirm
Comments suppressed due to low confidence (1)

trashcli/guard/guard.py:19

  • The guard logic is now reused beyond trash-empty (e.g., trash-rm passes tuples of (original_location, info_file)), but this module still hard-codes TrashDir types and trash_dirs naming in UserIntention and method parameters. This mismatch makes the API misleading and can confuse future refactors/type checking. Consider making Guard generic (e.g., Iterable[T] -> UserIntention[T]) or renaming the parameter/field to something like items_to_confirm and loosening the type from TrashDir.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread trashcli/rm/rm_cmd.py
Comment thread trashcli/rm/rm_cmd.py
Comment on lines +79 to +83
if files_to_remove: # skip asking the user if there is nothing to delete; avoids being stuck
user = User(prepare_output_message, RealInput(), parse_reply)
guard = Guard(user)
if guard.ask_the_user(is_input_interactive(), files_to_remove).ok_to_empty:
for file in files_to_remove:
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change hard-codes interactive detection (is_input_interactive()) and uses RealInput() directly inside run(). That makes behavior depend on whether stdin is a TTY (tests or callers running in a TTY can hang waiting for input), and it’s difficult to unit test the yes/no paths without patching global stdin. Consider injecting an Input implementation (like HardCodedInput) and/or parsing -i/-f flags similarly to trash-empty, so tests and non-interactive callers can reliably control confirmation behavior.

Copilot uses AI. Check for mistakes.
Comment thread trashcli/rm/rm_cmd.py
Comment on lines +79 to +86
if files_to_remove: # skip asking the user if there is nothing to delete; avoids being stuck
user = User(prepare_output_message, RealInput(), parse_reply)
guard = Guard(user)
if guard.ask_the_user(is_input_interactive(), files_to_remove).ok_to_empty:
for file in files_to_remove:
trashcan.delete_trash_info_and_backup_copy(file[1])
else:
print('No files / directories to be removed in {}'.format(path))
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New guarded-deletion behavior is introduced here, but there are no tests exercising the interactive confirmation branches (user answers yes vs no), nor the “no matches” path. Since tests/test_rm/ already has command-level and integration tests for RmCmd, it would be good to extend them to cover the new prompt flow and ensure it doesn’t regress or hang (especially when stdin is a TTY).

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +76
else:
print('No trash directories to empty.')
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch prints directly to stdout via print(), bypassing the Console abstraction that EmptyCmd wires up (and which is used elsewhere for output). This makes it harder to redirect/capture output consistently (e.g., when EmptyCmd is used programmatically with a custom out). Prefer emitting this message through console.out (or add a dedicated Console.print_info() method) instead of using print().

Copilot uses AI. Check for mistakes.
@abstract-official
Copy link
Copy Markdown
Author

Will close #383. (I created this issue in case the maintainer(s) only check(s) for new issues.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants