def test_remove_tree_mode(self): test_dir = create_empty_directory("tree_mode") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) self.trash.remove_mode = const.REMOVE_TREE_MODE removed = self.trash.remove([file_path, dir_path, tree_path]) for num in xrange(0, 3): self.assertFalse(os.path.exists(removed[num].initial_path)) self.assertTrue(os.path.exists(removed[num].path_in_trash))
def test_clean_remove_all_policy(self): self.trash.clean_policy = "remove_all" test_dir = create_empty_directory("clean") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) self.trash.remove([file_path, dir_path, tree_path]) self.assertNotEqual(len(os.listdir(self.files)), 0) self.assertNotEqual(len(os.listdir(self.info)), 0) self.trash.clean(clean_policy="remove_all") self.assertEqual(len(os.listdir(self.files)), 0) self.assertEqual(len(os.listdir(self.info)), 0)
def test_remove_directory_mode(self): test_dir = create_empty_directory("dir_mode") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) self.trash.remove_mode = const.REMOVE_EMPTY_DIRECTORY_MODE removed = self.trash.remove([file_path, dir_path, tree_path]) for num in xrange(0, 2): self.assertFalse(os.path.exists(removed[num].initial_path)) self.assertTrue(os.path.exists(removed[num].path_in_trash)) self.assertTrue(os.path.exists(tree_path)) self.assertFalse(os.path.exists(removed[2].path_in_trash)) with self.assertRaises(errors.ModeError) as info: for error in removed[2].errors: raise error ex = info.exception self.assertEqual(ex.errno, errno.ENOTEMPTY)
def test_permanent_remove(self): directory = create_empty_directory("permanent") file_path, dir_path, tree_path = ( create_file_dir_tree_in_directory(directory)) self.trash.remove_mode = "tree" removed = self.trash.remove([file_path, dir_path, tree_path]) for rm in removed: print self.info print os.path.basename(rm.initial_path) + const.INFO_FILE_EXPANSION info_path = os.path.join( self.info, os.path.basename(rm.initial_path) + const.INFO_FILE_EXPANSION) permanent_remove(rm, self.trash_loc) self.assertFalse(os.path.exists(rm.initial_path)) self.assertFalse(os.path.exists(rm.path_in_trash)) self.assertFalse(os.path.exists(info_path))
def test_view(self): test_dir = create_empty_directory("view") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) result = [] self.assertEqual(len(self.trash.view()), 0) self.trash.remove_mode = const.REMOVE_TREE_MODE for path in os.listdir(self.info): info_object = TrashInfo("", self.trash_location) info_object.read_info_with_config(os.path.join(seld.info, path), self._trashinfo_config) result.append(info_object) for res_obj, view_obj in zip(result, self.trash.view()): self.assertDictEqual(res_obj.__dict__, view_obj.__dict__)
def test_info_creating(self): test_dir = create_empty_directory("info_creating") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) test_paths = [] trash_loc, files, info = names_for_trash_files_info_in_dir(test_dir) for path in (file_path, dir_path, tree_path): test_paths.append( os.path.join( info, "{basename}.{extension}".format( basename=os.path.basename(path), extension=const.INFO_FILE_EXPANSION))) trash = Trash(trash_loc, remove_mode=const.REMOVE_TREE_MODE) for path in test_paths: self.assertFalse(os.path.exists(path)) trash.remove([file_path, dir_path, tree_path]) for path in test_paths: self.assertTrue(os.path.exists(path))
def test_dry_run_mode(self): test_dir = create_empty_directory("dry_run") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) trash_loc, files, info = names_for_trash_files_info_in_dir(test_dir) trash = Trash(trash_loc, remove_mode=const.REMOVE_TREE_MODE, dry_run=True) removed = trash.remove([file_path, dir_path, tree_path]) for num in xrange(0, 3): self.assertTrue(os.path.exists(removed[num].initial_path)) self.assertFalse(os.path.exists(removed[num].path_in_trash)) trash.dry_run = False removed = trash.remove([file_path, dir_path, tree_path]) for num in xrange(0, 3): self.assertFalse(os.path.exists(removed[num].initial_path)) self.assertTrue(os.path.exists(removed[num].path_in_trash))
def test_dry_run_restore(self): test_dir = create_empty_directory("dry_run") file_path, dir_path, tree_path = create_file_dir_tree_in_directory( test_dir) info_paths = [] for path in (file_path, dir_path, tree_path): info_paths.append( os.path.join( self.info, os.path.basename(path) + const.INFO_FILE_EXPANSION)) self.trash.remove_mode = const.REMOVE_TREE_MODE removed = self.trash.remove([file_path, dir_path, tree_path]) for num in xrange(0, 3): self.trash.dry_run = True res = self.trash.restore( [os.path.basename(removed[num].path_in_trash)]) self.assertTrue(os.path.exists(removed[num].path_in_trash)) self.assertTrue(os.path.exists(info_paths[num])) self.assertFalse(os.path.exists(removed[num].initial_path)) self.assertListEqual(removed[num].errors, []) self.trash.dry_run = False res = self.trash.restore( [os.path.basename(removed[num].path_in_trash)]) self.assertFalse(os.path.exists(removed[num].path_in_trash)) self.assertFalse(os.path.exists(info_paths[num])) self.assertTrue(os.path.exists(removed[num].initial_path))