class TestTrashedFileRestoreIntegration(unittest.TestCase): def setUp(self): self.temp_dir = MyPath.make_temp_dir() trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, TrashDirectory(), contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, trashed_files=trashed_files, mount_points=os_mount_points, fs=restore.FileSystem()) def test_restore(self): trashed_file = TrashedFile(self.temp_dir / 'parent/path', None, self.temp_dir / 'info_file', self.temp_dir / 'orig') make_empty_file(self.temp_dir / 'orig') make_empty_file(self.temp_dir / 'info_file') self.cmd.restore(trashed_file) assert os.path.exists(self.temp_dir / 'parent/path') assert not os.path.exists(self.temp_dir / 'info_file') assert not os.path.exists(self.temp_dir / 'orig') def test_restore_over_existing_file(self): trashed_file = TrashedFile(self.temp_dir / 'path', None, None, None) make_empty_file(self.temp_dir / 'path') self.assertRaises(IOError, lambda: self.cmd.restore(trashed_file)) def tearDown(self): self.temp_dir.clean_up()
class TestTrashedFileRestoreIntegration: def setUp(self): remove_file_if_exists('parent/path') remove_dir_if_exists('parent') self.cmd = RestoreCmd(None, None, None, None, None) def test_restore(self): trashed_file = TrashedFile('parent/path', None, 'info_file', 'orig') open('orig', 'w').close() open('info_file', 'w').close() self.cmd.restore(trashed_file) assert_true(os.path.exists('parent/path')) assert_true(not os.path.exists('info_file')) def test_restore_over_existing_file(self): trashed_file = TrashedFile('path', None, None, None) open('path', 'w').close() assert_raises(IOError, lambda: self.cmd.restore(trashed_file)) def tearDown(self): remove_file_if_exists('path') remove_file_if_exists('parent/path') remove_dir_if_exists('parent') def test_restore_create_needed_directories(self): require_empty_dir('sandbox') write_file('sandbox/TrashDir/files/bar') instance = TrashedFile('sandbox/foo/bar', 'deletion_date', 'info_file', 'sandbox/TrashDir/files/bar') self.cmd.restore(instance) assert os.path.exists("sandbox/foo/bar")
class TestListingInRestoreCmd: def setUp(self): self.cmd = RestoreCmd(None, None, None, None, None) self.cmd.curdir = lambda: "dir" self.cmd.handle_trashed_files = self.capture_trashed_files def test_with_no_args_and_files_in_trashcan(self): def some_files(): yield FakeTrashedFile('<date>', 'dir/location') yield FakeTrashedFile('<date>', 'dir/location') yield FakeTrashedFile('<date>', 'anotherdir/location') self.cmd.all_trashed_files = some_files self.cmd.run(['trash-restore']) assert_equals(['dir/location', 'dir/location'], self.original_locations) def test_with_no_args_and_files_in_trashcan(self): def some_files(): yield FakeTrashedFile('<date>', 'dir/location') yield FakeTrashedFile('<date>', 'dir/location') yield FakeTrashedFile('<date>', 'specific/path') self.cmd.all_trashed_files = some_files self.cmd.run(['trash-restore', 'specific/path']) assert_equals(['specific/path'], self.original_locations) def capture_trashed_files(self, arg): self.original_locations = [] for trashed_file in arg: self.original_locations.append(trashed_file.original_location)
def setUp(self): self.stdout = StringIO() self.stderr = StringIO() self.cmd = RestoreCmd(stdout=self.stdout, stderr=self.stderr, environ={}, exit=self.capture_exit_status, input=lambda x: self.user_reply, version=None)
def setUp(self): trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, None, contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, curdir=lambda: "dir", trashed_files=trashed_files, mount_points=os_mount_points) self.cmd.handle_trashed_files = self.capture_trashed_files self.trashed_files = Mock(spec=['all_trashed_files']) self.cmd.trashed_files = self.trashed_files
def setUp(self): self.temp_dir = MyPath.make_temp_dir() trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, TrashDirectory(), contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, trashed_files=trashed_files, mount_points=os_mount_points, fs=restore.FileSystem())
def setUp(self): remove_file_if_exists('parent/path') remove_dir_if_exists('parent') trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, TrashDirectory(), contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, trashed_files=trashed_files, mount_points=os_mount_points)
def setUp(self): self.stdout = StringIO() self.stderr = StringIO() trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, TrashDirectory(), contents_of) self.cmd = RestoreCmd(stdout=self.stdout, stderr=self.stderr, exit = self.capture_exit_status, input =lambda x: self.user_reply, version=None, trashed_files=trashed_files, mount_points=os_mount_points)
class TestTrashedFileRestoreIntegration(unittest.TestCase): def setUp(self): remove_file_if_exists('parent/path') remove_dir_if_exists('parent') trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, TrashDirectory(), contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, trashed_files=trashed_files, mount_points=os_mount_points, fs=restore.FileSystem()) def test_restore(self): trashed_file = TrashedFile('parent/path', None, 'info_file', 'orig') open('orig','w').close() open('info_file','w').close() self.cmd.restore(trashed_file) assert os.path.exists('parent/path') assert not os.path.exists('info_file') def test_restore_over_existing_file(self): trashed_file = TrashedFile('path',None,None,None) open('path','w').close() self.assertRaises(IOError, lambda:self.cmd.restore(trashed_file)) def tearDown(self): remove_file_if_exists('path') remove_file_if_exists('parent/path') remove_dir_if_exists('parent') def test_restore_create_needed_directories(self): require_empty_dir('sandbox') make_file('sandbox/TrashDir/files/bar') instance = TrashedFile('sandbox/foo/bar', 'deletion_date', 'info_file', 'sandbox/TrashDir/files/bar') self.cmd.restore(instance) assert os.path.exists("sandbox/foo/bar")
def __call__(self, from_dir='/', with_user_typing=''): RestoreCmd(stdout=self.out, stderr=self.err, environ=self.environ, exit=[].append, input=lambda msg: with_user_typing, curdir=lambda: from_dir).run([])
class TestTrashedFileRestore: def setUp(self): self.remove_file_if_exists('parent/path') self.remove_dir_if_exists('parent') self.cmd = RestoreCmd(None, None, None, None, None) def test_restore(self): trashed_file = TrashedFile('parent/path', None, 'info_file', 'orig') open('orig','w').close() open('info_file','w').close() self.cmd.restore(trashed_file) assert_true(os.path.exists('parent/path')) assert_true(not os.path.exists('info_file')) def test_restore_over_existing_file(self): trashed_file = TrashedFile('path',None,None,None) open('path','w').close() assert_raises(IOError, lambda: self.cmd.restore(trashed_file)) def tearDown(self): self.remove_file_if_exists('path') self.remove_file_if_exists('parent/path') self.remove_dir_if_exists('parent') def remove_file_if_exists(self, path): if os.path.lexists(path): os.unlink(path) def remove_dir_if_exists(self, dir): if os.path.exists(dir): os.rmdir(dir) def test_restore_create_needed_directories(self): require_empty_dir('sandbox') write_file('sandbox/TrashDir/files/bar') instance = TrashedFile('sandbox/foo/bar', 'deletion_date', 'info_file', 'sandbox/TrashDir/files/bar') self.cmd.restore(instance) assert os.path.exists("sandbox/foo/bar")
class TestListingInRestoreCmd: def setUp(self): trash_directories = make_trash_directories() trashed_files = TrashedFiles(trash_directories, None, contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, curdir=lambda: "dir", trashed_files=trashed_files, mount_points=os_mount_points) self.cmd.handle_trashed_files = self.capture_trashed_files self.trashed_files = Mock(spec=['all_trashed_files']) self.cmd.trashed_files = self.trashed_files def test_with_no_args_and_files_in_trashcan(self): self.trashed_files.all_trashed_files.return_value = [ FakeTrashedFile('<date>', 'dir/location'), FakeTrashedFile('<date>', 'dir/location'), FakeTrashedFile('<date>', 'anotherdir/location') ] self.cmd.run(['trash-restore']) assert_equal([ 'dir/location' , 'dir/location' ] ,self.original_locations) def test_with_no_args_and_files_in_trashcan_2(self): self.trashed_files.all_trashed_files.return_value = [ FakeTrashedFile('<date>', 'dir/location'), FakeTrashedFile('<date>', 'dir/location'), FakeTrashedFile('<date>', 'specific/path'), ] self.cmd.run(['trash-restore', 'specific/path']) assert_equal([ 'specific/path' ] ,self.original_locations) def capture_trashed_files(self,arg): self.original_locations = [] for trashed_file in arg: self.original_locations.append(trashed_file.original_location)
def test_something(self): cmd = RestoreCmd(None, None, {}, None, None) cmd.contents_of = lambda path: 'Path=name\nDeletionDate=2001-01-01T10:10:10' path_to_trashinfo = 'info/info_path.trashinfo' trash_dir = Mock([]) trash_dir.volume = '/volume' trash_dir.all_info_files = Mock([], return_value=[path_to_trashinfo]) cmd.all_trash_directories2 = lambda: [trash_dir] cmd.curdir = lambda: '/volume' trashed_files = cmd.all_trashed_files_in_dir() trashed_file = trashed_files[0] assert_equals('/volume/name' , trashed_file.path) assert_equals(datetime.datetime(2001, 1, 1, 10, 10, 10), trashed_file.deletion_date) assert_equals('info/info_path.trashinfo' , trashed_file.info_file) assert_equals('files/info_path' , trashed_file.actual_path)
def run_restore(self, with_user_typing=''): RestoreCmd(stdout=self.out, stderr=self.err, environ={ 'XDG_DATA_HOME': self.XDG_DATA_HOME }, exit=[].append, input=lambda msg: with_user_typing, curdir=lambda: self.current_dir).run([])
def setUp(self): self.stdout = StringIO() self.stderr = StringIO() self.cmd = RestoreCmd(stdout=self.stdout, stderr=self.stderr, environ={}, exit = self.capture_exit_status, input =lambda x: self.user_reply, version = None)
def test_list_all_directories(self): cmd = RestoreCmd(None, None, None, None, None) self.volume_of = Mock() self.getuid = lambda:123 self.mount_points = ['/', '/mnt'] self.environ = {'HOME': '~'} trash_dirs = TrashDirectories( volume_of = self.volume_of, getuid = self.getuid, environ = self.environ) result = cmd.all_trash_directories(trash_dirs, self.mount_points) paths = map(lambda td: td.path, result) assert_equals( ['~/.local/share/Trash', '/.Trash/123', '/.Trash-123', '/mnt/.Trash/123', '/mnt/.Trash-123'] , paths)
def restore(): from trashcli.restore import RestoreCmd try: # Python 2 input23 = raw_input except: # Python 3 input23 = input RestoreCmd(stdout=sys.stdout, stderr=sys.stderr, environ=os.environ, exit=sys.exit, input=input23).run(sys.argv)
def test_something(self): cmd = RestoreCmd(None, None, {}, None, None) require_empty_dir('info') open('info/info_path.trashinfo', 'w').write('Path=name\nDeletionDate=2001-01-01T10:10:10') path_to_trashinfo = 'info/info_path.trashinfo' trash_dir = Mock([]) trash_dir.volume = '/volume' trash_dir.all_info_files = Mock([], return_value=[path_to_trashinfo]) cmd.all_trash_directories2 = lambda: [trash_dir] cmd.curdir = lambda: '/volume' trashed_files = list(cmd.all_trashed_files()) trashed_file = trashed_files[0] assert_equals('/volume/name', trashed_file.original_location) assert_equals(datetime.datetime(2001, 1, 1, 10, 10, 10), trashed_file.deletion_date) assert_equals('info/info_path.trashinfo', trashed_file.info_file) assert_equals('files/info_path', trashed_file.original_file)
def run_restore(self, with_user_typing=''): environ = {'XDG_DATA_HOME': self.XDG_DATA_HOME} trash_directories = TrashDirectories(volume_of, os.getuid, environ) trash_directories2 = TrashDirectories2(volume_of, trash_directories) trashed_files = TrashedFiles(trash_directories2, TrashDirectory(), contents_of) RestoreCmd(stdout=self.out, stderr=self.err, exit=[].append, input=lambda msg: with_user_typing, curdir=lambda: self.current_dir, trashed_files=trashed_files, mount_points=os_mount_points).run([])
class TestListingInRestoreCmd(unittest.TestCase): def setUp(self): trash_directories = make_trash_directories() self.logger = Mock(spec=[]) trashed_files = TrashedFiles(self.logger, trash_directories, None, contents_of) self.cmd = RestoreCmd(None, None, exit=None, input=None, curdir=lambda: "dir", trashed_files=trashed_files, mount_points=os_mount_points, fs=restore.FileSystem()) self.cmd.handle_trashed_files = self.capture_trashed_files self.trashed_files = Mock(spec=['all_trashed_files']) self.cmd.trashed_files = self.trashed_files def test_with_no_args_and_files_in_trashcan(self): self.trashed_files.all_trashed_files.return_value = [ FakeTrashedFile('<date>', 'dir/location'), FakeTrashedFile('<date>', 'dir/location'), FakeTrashedFile('<date>', 'anotherdir/location') ] self.cmd.run(['trash-restore']) assert [ 'dir/location' , 'dir/location' ] ==self.original_locations def test_with_no_args_and_files_in_trashcan_2(self): self.trashed_files.all_trashed_files.return_value = [ FakeTrashedFile('<date>', '/dir/location'), FakeTrashedFile('<date>', '/dir/location'), FakeTrashedFile('<date>', '/specific/path'), ] self.cmd.run(['trash-restore', '/specific/path']) assert self.original_locations == ['/specific/path'] def test_with_with_path_prefix_bug(self): self.trashed_files.all_trashed_files.return_value = [ FakeTrashedFile('<date>', '/prefix'), FakeTrashedFile('<date>', '/prefix-with-other'), ] self.cmd.run(['trash-restore', '/prefix']) assert self.original_locations == ['/prefix'] def capture_trashed_files(self,arg): self.original_locations = [] for trashed_file in arg: self.original_locations.append(trashed_file.original_location)
def setUp(self): self.cmd = RestoreCmd(None, None, None, None, None) self.cmd.curdir = lambda: "dir" self.cmd.handle_trashed_files = self.capture_trashed_files
class TestTrashRestoreCmd: def setUp(self): self.stdout = StringIO() self.stderr = StringIO() self.cmd = RestoreCmd(stdout=self.stdout, stderr=self.stderr, environ={}, exit=self.capture_exit_status, input=lambda x: self.user_reply, version=None) def capture_exit_status(self, exit_status): self.exit_status = exit_status def test_should_print_version(self): self.cmd.version = '1.2.3' self.cmd.run(['trash-restore', '--version']) assert_equals('trash-restore 1.2.3\n', self.stdout.getvalue()) def test_with_no_args_and_no_files_in_trashcan(self): self.cmd.curdir = lambda: "cwd" self.cmd.run(['trash-restore']) assert_equals("No files trashed from current dir ('cwd')\n", self.stdout.getvalue()) def test_until_the_restore_intgration(self): from trashcli.fs import remove_file from trashcli.fs import contents_of self.user_reply = '0' open('orig_file', 'w').write('original') open('info_file', 'w').write('') remove_file('parent/path') remove_file('parent') trashed_file = TrashedFile('parent/path', None, 'info_file', 'orig_file') self.cmd.restore_asking_the_user([trashed_file]) assert_equals('', self.stdout.getvalue()) assert_equals('', self.stderr.getvalue()) assert_true(not os.path.exists('info_file')) assert_true(not os.path.exists('orig_file')) assert_true(os.path.exists('parent/path')) assert_equals('original', contents_of('parent/path')) def test_until_the_restore_unit(self): trashed_file = TrashedFile('parent/path', None, 'info_file', 'orig_file') fs = Mock() self.cmd.fs = fs self.cmd.path_exists = lambda _: False self.user_reply = '0' self.cmd.restore_asking_the_user([trashed_file]) assert_equals('', self.stdout.getvalue()) assert_equals('', self.stderr.getvalue()) assert_equals([ call.mkdirs('parent'), call.move('orig_file', 'parent/path'), call.remove_file('info_file') ], fs.mock_calls) def test_when_user_reply_with_empty_string(self): self.user_reply = '' self.cmd.restore_asking_the_user([]) assert_equals('Exiting\n', self.stdout.getvalue()) def test_when_user_reply_with_not_number(self): self.user_reply = 'non numeric' self.cmd.restore_asking_the_user([]) assert_equals('Invalid entry\n', self.stderr.getvalue()) assert_equals('', self.stdout.getvalue()) assert_equals(1, self.exit_status) def test_when_user_reply_with_an_out_of_range_number(self): self.user_reply = '100' self.cmd.restore_asking_the_user([]) assert_equals('Invalid entry\n', self.stderr.getvalue()) assert_equals('', self.stdout.getvalue()) assert_equals(1, self.exit_status)
def setUp(self): remove_file_if_exists('parent/path') remove_dir_if_exists('parent') self.cmd = RestoreCmd(None, None, None, None, None)
class TestTrashRestoreCmd(unittest.TestCase): def setUp(self): self.stdout = StringIO() self.stderr = StringIO() trash_directories = make_trash_directories() self.logger = Mock(spec=[]) trashed_files = TrashedFiles(self.logger, trash_directories, TrashDirectory(), contents_of) self.fs = Mock(spec=restore.FileSystem) self.cmd = RestoreCmd(stdout=self.stdout, stderr=self.stderr, exit = self.capture_exit_status, input =lambda x: self.user_reply, version=None, trashed_files=trashed_files, mount_points=os_mount_points, fs=self.fs) def capture_exit_status(self, exit_status): self.exit_status = exit_status def test_should_print_version(self): self.cmd.version = '1.2.3' self.cmd.run(['trash-restore', '--version']) assert 'trash-restore 1.2.3\n' == self.stdout.getvalue() def test_with_no_args_and_no_files_in_trashcan(self): self.cmd.curdir = lambda: "cwd" self.cmd.run(['trash-restore']) assert ("No files trashed from current dir ('cwd')\n" == self.stdout.getvalue()) def test_until_the_restore_unit(self): self.fs.path_exists.return_value = False trashed_file = TrashedFile( 'parent/path', None, 'info_file', 'orig_file') self.user_reply = '0' self.cmd.restore_asking_the_user([trashed_file]) assert '' == self.stdout.getvalue() assert '' == self.stderr.getvalue() assert [call.path_exists('parent/path'), call.mkdirs('parent'), call.move('orig_file', 'parent/path'), call.remove_file('info_file')] == self.fs.mock_calls def test_when_user_reply_with_empty_string(self): self.user_reply = '' self.cmd.restore_asking_the_user([]) assert 'Exiting\n' == self.stdout.getvalue() def test_when_user_reply_with_not_number(self): self.user_reply = 'non numeric' self.cmd.restore_asking_the_user([]) assert 'Invalid entry: not an index: non numeric\n' == \ self.stderr.getvalue() assert '' == self.stdout.getvalue() assert 1 == self.exit_status
def setUp(self): self.remove_file_if_exists('parent/path') self.remove_dir_if_exists('parent') self.cmd = RestoreCmd(None, None, None, None, None)
class TestTrashRestoreCmd: def setUp(self): self.stdout = StringIO() self.stderr = StringIO() self.cmd = RestoreCmd(stdout=self.stdout, stderr=self.stderr, environ={}, exit = self.capture_exit_status, input =lambda x: self.user_reply, version = None) def capture_exit_status(self, exit_status): self.exit_status = exit_status def test_should_print_version(self): self.cmd.version = '1.2.3' self.cmd.run(['trash-restore', '--version']) assert_equals('trash-restore 1.2.3\n', self.stdout.getvalue()) def test_with_no_args_and_no_files_in_trashcan(self): self.cmd.curdir = lambda: "cwd" self.cmd.run(['trash-restore']) assert_equals("No files trashed from current dir ('cwd')\n", self.stdout.getvalue()) def test_until_the_restore(self): from trashcli.fs import remove_file from trashcli.fs import contents_of self.user_reply = '0' file('orig_file', 'w').write('original') file('info_file', 'w').write('') remove_file('parent/path') remove_file('parent') trashed_file = TrashedFile( 'parent/path', None, 'info_file', 'orig_file') self.cmd.restore_asking_the_user([trashed_file]) assert_equals('', self.stdout.getvalue()) assert_equals('', self.stderr.getvalue()) assert_true(not os.path.exists('info_file')) assert_true(not os.path.exists('orig_file')) assert_true(os.path.exists('parent/path')) assert_equals('original', contents_of('parent/path')) def test_until_the_restore_unit(self): from mock import Mock, call trashed_file = TrashedFile( 'parent/path', None, 'info_file', 'orig_file') fs = Mock() self.cmd.fs = fs self.cmd.path_exists = lambda _: False self.user_reply = '0' self.cmd.restore_asking_the_user([trashed_file]) assert_equals('', self.stdout.getvalue()) assert_equals('', self.stderr.getvalue()) assert_equals([ call.mkdirs('parent') , call.move('orig_file', 'parent/path') , call.remove_file('info_file') ], fs.mock_calls) def test_with_no_args_and_files_in_trashcan(self): class thing(object): pass def some_files(append, _): t = thing() t.deletion_date = None t.path = None append(t) self.cmd.curdir = lambda: "cwd" self.cmd.for_all_trashed_file_in_dir = some_files self.cmd.input = lambda _ : "" self.cmd.run(['trash-restore']) assert_equals(" 0 None None\n" "Exiting\n", self.stdout.getvalue()) def test_when_user_reply_with_empty_string(self): self.user_reply = '' self.cmd.restore_asking_the_user([]) assert_equals('Exiting\n', self.stdout.getvalue()) def test_when_user_reply_with_empty_string(self): self.user_reply = 'non numeric' self.cmd.restore_asking_the_user([]) assert_equals('Invalid entry\n', self.stderr.getvalue()) assert_equals('', self.stdout.getvalue()) assert_equals(1, self.exit_status) def test_when_user_reply_with_out_of_range_number(self): self.user_reply = '100' self.cmd.restore_asking_the_user([]) assert_equals('Invalid entry\n', self.stderr.getvalue()) assert_equals('', self.stdout.getvalue()) assert_equals(1, self.exit_status)