def test_check_parent_read_rights(self): parent_directory = create_empty_directory("parent_dir") read_wright_execute_all = 0777 no_read_wright_execute_all = 0333 test_paths = [] empty_dir = create_dir_in_dir("empty", parent_directory) test_paths.append(empty_dir) file_in_dir = create_file_in_dir("file", parent_directory) test_paths.append(file_in_dir) tree_dir = create_dir_in_dir("tree", parent_directory) file_in_tree = create_file_in_dir("file", tree_dir) test_paths.append(tree_dir) os.chmod(tree_dir, read_wright_execute_all) for path in test_paths: os.chmod(parent_directory, read_wright_execute_all) self.assertTrue(check_parent_read_rights(path)) os.chmod(parent_directory, no_read_wright_execute_all) self.assertFalse(check_parent_read_rights(path)) os.chmod(parent_directory, read_wright_execute_all) self.assertTrue(check_parent_read_rights(file_in_tree)) os.chmod(parent_directory, no_read_wright_execute_all) self.assertTrue(check_parent_read_rights(file_in_tree))
def test_remove_list(self): test_dir = create_empty_directory("list") files = [] directories = [] trees = [] for num in xrange(0, 3): files.append(create_file_in_dir("file{0}".format(num), test_dir)) directories.append( create_dir_in_dir("dir{0}".format(num), test_dir)) trees.append(create_tree_in_dir("tree{0}".format(num), test_dir)) self.trash.remove_mode = const.REMOVE_FILE_MODE removed_files = self.trash.remove(files) self.trash.remove_mode = const.REMOVE_EMPTY_DIRECTORY_MODE removed_directories = self.trash.remove(directories) self.trash.remove_mode = const.REMOVE_TREE_MODE removed_trees = self.trash.remove(trees) file_list = [removed_files, removed_directories, removed_trees] for lt in file_list: for num in xrange(0, 3): self.assertFalse(os.path.exists(lt[num].initial_path)) self.assertTrue(os.path.exists(lt[num].path_in_trash))
def test_create_if_not_exist(self): test_path = create_empty_directory("create") if os.path.exists(test_path): shutil.rmtree(test_path) self.assertFalse(os.path.exists(test_path)) create_not_exist_file(test_path) self.assertTrue(os.path.exists(test_path))
def test_make_trash_if_not_exist(self): trash = create_empty_directory("trash_loc") make_trash_if_not_exist(trash) self.assertTrue(os.path.exists(trash)) self.assertTrue( os.path.exists(os.path.join(trash, const.TRASH_FILES_DIRECTORY))) self.assertTrue( os.path.exists(os.path.join(trash, const.TRASH_INFO_DIRECTORY)))
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_confirm_yes_and_replace(self, yes): dir_path = create_empty_directory("yes") file_to_replace = create_file("file_replace") new_file = os.path.abspath(create_file_in_dir("file_replace", dir_path)) replaced = self.trash.remove([file_to_replace]) self.assertEqual(len(os.listdir(self.files)), 1) self.trash.conflict_policy = "confirm_and_replace" removed = self.trash.remove([new_file]) self.assertEqual(len(os.listdir(self.files)), 1) self.assertTrue(os.path.exists(removed[0].path_in_trash)) in_trash = self.trash.view() self.assertEqual(in_trash[0].initial_path, new_file)
def test_all_tree_elem_match_regex(self): regex_dir = create_empty_directory("regex_tree_dir") all_tree_elem_match_regex_template = r"\d*tree.*" all_tree_elem_match_regex = create_dir_in_dir("121212treeadadas", regex_dir) create_file_in_dir("436757treejhj", all_tree_elem_match_regex) create_file_in_dir("566887treekjhkj", all_tree_elem_match_regex) dir_in_all_tree_elem_match_regex = create_dir_in_dir( "767868treeoj9", all_tree_elem_match_regex) create_file_in_dir("12treeadad", dir_in_all_tree_elem_match_regex) self.trash.remove_mode = const.REMOVE_TREE_MODE res = self.trash.remove([all_tree_elem_match_regex], regex=all_tree_elem_match_regex_template) self.assertEqual(len(res), 1) self.assertEqual(res[0].initial_path, os.path.abspath(all_tree_elem_match_regex))
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_confirm_no_and_replace(self, no): dir_path = create_empty_directory("no") file_to_replace = os.path.abspath(create_file("file_no_repl")) new_file = os.path.abspath(create_file_in_dir("file_no_repl", dir_path)) replaced = self.trash.remove([file_to_replace]) self.assertEqual(len(os.listdir(self.files)), 1) self.trash.conflict_policy = "confirm_and_replace" rm = self.trash.remove([new_file]) self.assertTrue(os.path.exists(rm[0].initial_path)) with self.assertRaises(errors.SysError) as info: raise rm[0].errors[0] ex = info.exception self.assertIn('Already exists', str(ex)) in_trash = self.trash.view() self.assertEqual(in_trash[0].initial_path, file_to_replace)
def test_file_regex(self): regex_dir = create_empty_directory("regex") file_template = "file.*" file_names = [ "file.py", "file$", "file$$", "file1231321", r"\file1", "abracadabra", "lalal" ] file_re = get_regex_matcher(file_template) regex_paths = [] file_paths = [] for name in file_names: path = create_file_in_dir(name, regex_dir) file_paths.append(path) if file_re(name): regex_paths.append(os.path.abspath(path)) removed = self.trash.remove(file_paths, regex=file_template) for rm in removed: self.assertIn(rm.initial_path, regex_paths)
def test_parent_read_rights(self): parent_directory = create_empty_directory("parent_dir") r_file = create_file_in_dir("file_parent_right", parent_directory) no_r_file = create_file_in_dir("file_no_par_right", parent_directory) read_wright_execute_all = 0777 no_read_wright_execute_all = 0333 os.chmod(parent_directory, read_wright_execute_all) rm = self.trash.remove([r_file]) self.assertFalse(os.path.exists(rm[0].initial_path)) self.assertTrue(os.path.exists(rm[0].path_in_trash)) os.chmod(parent_directory, no_read_wright_execute_all) rm = self.trash.remove([no_r_file]) with self.assertRaises(errors.AccessError) as info: for error in rm[0].errors: raise error ex = info.exception self.assertEqual(ex.errno, errno.EACCES)
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_dir_regex(self): regex_dir = create_empty_directory("regex_dir") dir_template = ".+dir123" dir_names = [ "dir123", "ajdhadir123", "asjdhsjdir123skjfhkds", r"$$$$$dir123", "$$dir123", "abr" ] dir_re = get_regex_matcher(dir_template) regex_paths = [] dir_paths = [] for name in dir_names: path = create_dir_in_dir(name, regex_dir) dir_paths.append(path) if dir_re(name): regex_paths.append(os.path.abspath(path)) self.trash.remove_mode = const.REMOVE_EMPTY_DIRECTORY_MODE removed = self.trash.remove(dir_paths, regex=dir_template) for rm in removed: self.assertIn(rm.initial_path, regex_paths)
def test_check_directory_access(self): access_directory = create_empty_directory("acces_dir") read_wright_execute_all = 0777 read_wright_no_execute_all = 0666 read_no_wright_execute_all = 0555 read_no_wright_no_execute_all = 0444 no_read_wright_execute_all = 0333 os.chmod(access_directory, read_wright_execute_all) self.assertTrue(check_directory_access(access_directory)) os.chmod(access_directory, read_wright_no_execute_all) self.assertFalse(check_directory_access(access_directory)) os.chmod(access_directory, read_no_wright_execute_all) self.assertFalse(check_directory_access(access_directory)) os.chmod(access_directory, read_no_wright_no_execute_all) self.assertFalse(check_directory_access(access_directory)) os.chmod(access_directory, no_read_wright_execute_all) self.assertTrue(check_directory_access(access_directory))
def test_tree_match_rehex_partially(self): regex_dir = create_empty_directory("regex_tree_part") paths = [] regex_paths = [] tree_template = r"4+part.*" matcher = get_regex_matcher(tree_template) tree = create_dir_in_dir("444part098765", regex_dir) file1_in_tree = create_file_in_dir("444partads", tree) file2_in_tree = create_file_in_dir("partkasjd", tree) dir_in_tree = create_dir_in_dir("hello", tree) file_in_dir_tree = create_file_in_dir("4444parthello", dir_in_tree) paths = [ tree, file1_in_tree, file2_in_tree, dir_in_tree, file_in_dir_tree ] for path in paths: if matcher(path): regex_paths.append(os.path.abspath(path)) self.trash.remove_mode = const.REMOVE_TREE_MODE result = self.trash.remove([tree], regex=tree_template) for res in result: self.assertIn(res.initial_path, regex_paths)
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))
def test_check_path_is_not_tree(self): empty_directory = create_empty_directory("empty_dir") self.assertTrue(check_path_is_not_tree(empty_directory)) self.assertTrue(check_path_is_not_tree(__file__)) self.assertFalse(check_path_is_not_tree(os.path.dirname(__file__)))