diff --git a/send2trash/plat_other.py b/send2trash/plat_other.py index 6e1be2d..867de92 100644 --- a/send2trash/plat_other.py +++ b/send2trash/plat_other.py @@ -17,7 +17,6 @@ import sys import os import os.path as op -import logging from datetime import datetime import stat from urllib.parse import quote @@ -27,21 +26,17 @@ INFO_DIR = 'info' INFO_SUFFIX = '.trashinfo' # Default of ~/.local/share [3] -XDG_DATA_HOME = os.environ.get('XDG_DATA_HOME') or '~/.local/share' -HOMETRASH = op.expanduser(op.join(XDG_DATA_HOME,'Trash')) +XDG_DATA_HOME = op.expanduser(os.environ.get('XDG_DATA_HOME', '~/.local/share')) +HOMETRASH = op.join(XDG_DATA_HOME, 'Trash') uid = os.getuid() TOPDIR_TRASH = '.Trash' TOPDIR_FALLBACK = '.Trash-' + str(uid) def is_parent(parent, path): - path = op.abspath(path) - parent = op.abspath(parent) - while path != '/': - path = op.abspath(op.join(path, '..')) - if path == parent: - return True - return False + path = op.realpath(path) # In case it's a symlink + parent = op.realpath(parent) + return path.startswith(parent) def format_date(date): return date.strftime("%Y-%m-%dT%H:%M:%S") @@ -128,6 +123,8 @@ def find_ext_volume_trash(volume_root): def send2trash(path): if not isinstance(path, str): path = str(path, sys.getfilesystemencoding()) + if not op.exists(path): + raise OSError("File not found: %s" % path) try: trash_move(path, HOMETRASH, XDG_DATA_HOME) except OSError: diff --git a/test_plat_other.py b/test_plat_other.py index 85e7ebc..a3537ca 100644 --- a/test_plat_other.py +++ b/test_plat_other.py @@ -3,6 +3,10 @@ import os from os import path as op from send2trash.plat_other import send2trash from configparser import ConfigParser + +# XXX Although this unittest is better than no unit test at all, it would be better to mock +# os.path.mountpoint() rather than going through ramfs (and requiring admin rights). + # # Warning: This test will shit up your Trash folder with test.txt files. #