Skip to content

Commit

Permalink
Fix clean logic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msj committed Aug 21, 2023
1 parent b19059d commit 487d55c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pupa/cli/commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def add_args(self):
self.add_argument(
"--yes",
action="store_true",
help="delete objects without getting user confirmation",
help="assumes an answer of 'yes' to all interactive prompts",
default=False,
)

def get_stale_objects(self, window):
Expand Down Expand Up @@ -96,10 +97,14 @@ def handle(self, args, other):
stale_objects = list(self.get_stale_objects(args.window))
num_stale_objects = len(stale_objects)

if args.noinput and args.yes:
self.remove_stale_objects(args.window)
sys.exit()

if args.noinput:
# Fail-safe to avoid deleting a large amount of objects
# without explicit confimation
if num_stale_objects > 10 and not args.yes:
if num_stale_objects > 10:
print(
f"This command would delete {num_stale_objects} objects: "
f"\n{stale_objects}"
Expand Down
4 changes: 2 additions & 2 deletions pupa/tests/clean/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_clean_command(subparsers):

# Call clean command
Command(subparsers).handle(
argparse.Namespace(noinput=True, report=False, window=7), []
argparse.Namespace(noinput=True, report=False, window=7, yes=False), []
)

expected_stale_objects = {stale_person, stale_membership}
Expand Down Expand Up @@ -117,5 +117,5 @@ def test_clean_command_failsafe(subparsers):
with pytest.raises(SystemExit):
# Should trigger failsafe exist when deleting more than 10 objects
Command(subparsers).handle(
argparse.Namespace(noinput=True, report=False, window=7), []
argparse.Namespace(noinput=True, report=False, window=7, yes=False), []
)

0 comments on commit 487d55c

Please sign in to comment.