Skip to content

Commit

Permalink
Validate input in trash-remove
Browse files Browse the repository at this point in the history
  • Loading branch information
olajep committed Oct 9, 2013
1 parent 9078825 commit 5b8773a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions integration_tests/test_restore_trash.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ def it_should_do_nothing_when_no_file_have_been_found_in_current_dir(self):
self.when_running_restore_trash()
self.output_should_match('No files trashed from current dir.+')

@istest
def it_should_error_when_user_input_is_not_a_number(self):

self.having_a_trashed_file('/foo/bar')
self.when_running_restore_trash( from_dir='/foo',
with_user_typing = '-@notanumber')
self.error_should_be('Invalid entry\n')

@istest
def it_should_error_when_user_input_is_too_small(self):

self.having_a_trashed_file('/foo/bar')
self.when_running_restore_trash( from_dir='/foo',
with_user_typing = '-1')
self.error_should_be('Invalid entry\n')

@istest
def it_should_error_when_user_input_is_too_large(self):

self.having_a_trashed_file('/foo/bar')
self.when_running_restore_trash( from_dir='/foo',
with_user_typing = '1')
self.error_should_be('Invalid entry\n')

@istest
def it_should_show_the_file_deleted_from_the_current_dir(self):

Expand Down
7 changes: 6 additions & 1 deletion trashcli/trash.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ def run(self, args = sys.argv):
if index == "" :
self.println("Exiting")
else :
index = int(index)
try:
index = int(index)
if (index < 0 or index >= len(trashed_files)):
raise IndexError("Out of range")
trashed_files[index].restore()
except (ValueError, IndexError) as e:
self.printerr("Invalid entry")
self.exit(1)
except IOError as e:
self.printerr(e)
self.exit(1)
Expand Down

1 comment on commit 5b8773a

@andreafrancia
Copy link

Choose a reason for hiding this comment

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

Seems a good patch, in the next days I'll verify it.

Please sign in to comment.