def test_create_trash_info_file(trash, tmpdir): for i in range(2): path = create_tmpfile(tmpdir, "new_file") trash_manager.delete([path]) infodir = os.path.join(trash, "info") assert os.path.exists(os.path.join(infodir, "new_file.trashinfo")) assert os.path.exists(os.path.join(infodir, "new_file.2.trashinfo"))
def test_do_not_override_existing_file_in_trash(trash, tmpdir): for i in range(2): path = create_tmpfile(tmpdir, "new_file") trash_manager.delete([path]) trashdir = os.path.join(trash, "files") assert os.path.exists(os.path.join(trashdir, "new_file")) assert os.path.exists(os.path.join(trashdir, "new_file.2"))
def delete(paths: List[str]) -> None: """Move one or more images to the trash directory. **syntax:** ``:delete path [path ...]`` positional arguments: * ``paths``: The path(s) to the images to delete. .. note:: This only deletes images, not any other path(s). """ _last_deleted.clear() images = [path for path in paths if files.is_image(path)] if not images: raise api.commands.CommandError("No images to delete") failed_images = [] for filename in images: try: trash_filename = trash_manager.delete(filename) _last_deleted.append(os.path.basename(trash_filename)) except OSError: failed_images.append(filename) n_fail = len(failed_images) n_success = len(images) - n_fail fail_msg = f"Failed to delete {', '.join(failed_images)}" if n_fail > 0 else "" succ_msg = f"Deleted {n_success} images" if n_success > 0 else "" if fail_msg: log.warning(f"{succ_msg + ' but ' if succ_msg else ''}{fail_msg}") elif succ_msg: log.info(succ_msg)
def test_fail_undelete_non_existing_original_directory(tmp_path): directory = tmp_path / "directory" directory.mkdir() original_filename = create_tmpfile(directory, "file") trash_filename = trash_manager.delete(original_filename) directory.rmdir() with pytest.raises(FileNotFoundError, match="Original directory"): trash_manager.undelete(pathlib.Path(trash_filename).name)
def test_undelete_symlink(tmp_path): path = tmp_path / "file" path.touch() path_to_link = tmp_path / "link" path_to_link.symlink_to("file") trash_filename = trash_manager.delete(str(path_to_link)) trash_manager.undelete(trash_filename) assert path_to_link.exists() assert path_to_link.is_symlink() assert path_to_link.resolve() == path
def deleted_file(tmp_path): original_filename = create_tmpfile(tmp_path, "file") trash_filename = trash_manager.delete(original_filename) info_filename = trash_manager._get_info_filename(original_filename) paths = collections.namedtuple("DeletedFile", ("original", "trash", "info")) return paths( pathlib.Path(original_filename), pathlib.Path(trash_filename), pathlib.Path(info_filename), )
def delete(paths: List[str]) -> None: """Move one or more images to the trash directory. **syntax:** ``:delete path [path ...]`` positional arguments: * ``paths``: The path(s) to the images to delete. .. note:: This only deletes images, not any other path(s). """ _last_deleted.clear() images = [path for path in paths if files.is_image(path)] if not images: raise api.commands.CommandError("No images to delete") for filename in images: trash_filename = trash_manager.delete(filename) _last_deleted.append(os.path.basename(trash_filename)) n_images = len(images) if n_images > 1: log.info("Deleted %d images", n_images)
def delete(paths: List[str], ask: bool = False) -> None: """Move one or more images to the trash directory. **syntax:** ``:delete path [path ...]`` positional arguments: * ``paths``: The path(s) to the images to delete. optional arguments: * ``--ask``: Prompt for confirmation before deleting the images. .. note:: This only deletes images, not any other path(s). """ if ask and not api.prompt.ask_question( title="delete", body=f"delete {quotedjoin(paths)}?" ): return _last_deleted.clear() images = [path for path in paths if files.is_image(path)] if not images: raise api.commands.CommandError("No images to delete") failed_images = [] for filename in images: try: trash_filename = trash_manager.delete(filename) _last_deleted.append(os.path.basename(trash_filename)) except OSError: failed_images.append(filename) n_fail = len(failed_images) n_success = len(images) - n_fail fail_msg = f"Failed to delete {', '.join(failed_images)}" if n_fail > 0 else "" succ_msg = f"Deleted {n_success} images" if n_success > 0 else "" if fail_msg: log.warning(f"{succ_msg + ' but ' if succ_msg else ''}{fail_msg}") elif succ_msg: log.info(succ_msg)
def test_do_not_overwrite_trash_file(deleted_file): deleted_file.original.touch() trash_manager.delete(deleted_file.original) assert os.path.exists(str(deleted_file.trash) + ".2") assert os.path.exists(str(deleted_file.info).replace(".trashinfo", ".2.trashinfo"))
def test_delete_and_undelete_file(trash, tmpfile): trash_manager.delete([tmpfile]) basename = os.path.basename(tmpfile) trash_manager.undelete([basename]) assert os.path.exists(tmpfile)
def test_delete_multiple_files(trash, tmpdir): files = [create_tmpfile(tmpdir, f"file_{i:d}") for i in range(2)] trash_manager.delete(files) for fname in files: assert not os.path.exists(fname)
def test_delete_file(trash, tmpfile): trash_manager.delete([tmpfile]) assert not os.path.exists(tmpfile)