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_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_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_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_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_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))