Skip to content

Commit

Permalink
Replace send2trash with QFile.moveToTrash.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bzero committed Sep 18, 2024
1 parent 24ddee0 commit 93a2b1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies = [
"qtpy >= 2.3",
"pyside6 >= 6.4.0",
"pygments >= 2.18",
"send2trash >= 1.8.0"
]
dynamic = ["version"]

Expand Down
53 changes: 26 additions & 27 deletions tests/test_fs_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,29 @@ def test_rename(self, tmp_path, qtbot, caplog, monkeypatch):
assert from_path.exists()
assert to_path.exists()

# tmp_path creates a directory in /tmp where send2trash has no permission
# def test_delete(self, tmp_path, qtbot, monkeypatch):
# """Make sure a folder or file can be deleted."""
# fse = fs_explorer.FSExplorer()
# qtbot.addWidget(fse)
#
# folder_path = tmp_path / "test_folder"
# file_path = tmp_path / "test_file.dat"
#
# folder_path.mkdir()
# file_path.touch()
# assert file_path.exists()
# assert folder_path.exists()
#
# # Decline deletion
# monkeypatch.setattr(QtWidgets.QMessageBox, "question", lambda *args: QtWidgets.QMessageBox.StandardButton.No)
# fse.delete(str(folder_path))
# fse.delete(str(file_path))
# assert file_path.exists()
# assert folder_path.exists()
#
# # Accept deletion
# monkeypatch.setattr(QtWidgets.QMessageBox, "question", lambda *args: QtWidgets.QMessageBox.StandardButton.Yes)
# fse.delete(str(folder_path))
# fse.delete(str(file_path))
# assert not file_path.exists()
# assert not folder_path.exists()
def test_delete(self, tmp_path, qtbot, monkeypatch):
"""Make sure a folder or file can be deleted."""
fse = fs_explorer.FSExplorer()
qtbot.addWidget(fse)

folder_path = tmp_path / "test_folder"
file_path = tmp_path / "test_file.dat"

folder_path.mkdir()
file_path.touch()
assert file_path.exists()
assert folder_path.exists()

# Decline deletion
monkeypatch.setattr(QtWidgets.QMessageBox, "question", lambda *args: QtWidgets.QMessageBox.StandardButton.No)
fse.delete(str(folder_path))
fse.delete(str(file_path))
assert file_path.exists()
assert folder_path.exists()

# Accept deletion
monkeypatch.setattr(QtWidgets.QMessageBox, "question", lambda *args: QtWidgets.QMessageBox.StandardButton.Yes)
fse.delete(str(folder_path))
fse.delete(str(file_path))
assert not file_path.exists()
assert not folder_path.exists()
11 changes: 3 additions & 8 deletions typstwriter/fs_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import os

import send2trash

from typstwriter import util

from typstwriter import logging
Expand Down Expand Up @@ -323,9 +321,6 @@ def delete(self, path):
"""Delete a folder or file."""
msg = QtWidgets.QMessageBox.question(self, "Typstwriter", f"Should '{path}' be deleted?")
if msg == QtWidgets.QMessageBox.StandardButton.Yes:
try:
send2trash.send2trash(path)
except send2trash.TrashPermissionError:
logger.warning("Could not move {!r} to trash: Permission denied.", path)
except OSError:
logger.warning("Could not move {!r} to trash: Unknown Error.", path)
deletion_succeeded = QtCore.QFile.moveToTrash(path)
if not deletion_succeeded:
logger.warning("Could not move {!r} to trash.", path)

0 comments on commit 93a2b1a

Please sign in to comment.